Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity Singleton Code

I'm new to Unity and am trying to write some Unity logic which initialises and register/resolves a singleton instance of the Email object so that it can be used across several other objects, one example below being OperationEntity.

So when it's registered it populates the Email singleton with some values from a config file, then whenever an instance of OperationEntity is created (in my case it's being deserialized) it uses that same Email singleton. So all my client logic needs to do is deserialize OperationEntity and call PerformAction() - with the email instance taken care of by Unity.

public interface IEmail {     string FromName { get; set; }     string FromEmailAddress { get; set; } }  public class Email : IEmail {     public string FromName { get; set; }     public string FromEmailAddress { get; set; }      public Email(string fromName, string fromEmailAddress)     {         FromName = fromName;         FromEmailAddress = fromEmailAddress;     } }  public class OperationEntity {     private readonly IEmail _email;      public int OperationId { get; set; }     public string OperationName { get; set; }     public string ToAddress { get; set; }      public OperationEntity(IEmail email)     {         _email = email;     }      public void PerformAction()     {         _email.ToAddress = ToAddress;         _email.Body = "Some email body";         _email.Deliver();     } } 

Any help would be appreciated in getting this Unity code to work

    public static void Register(IUnityContainer container)     {         container             .RegisterType<IEmail, Email>(             new InjectionFactory(c => new Email(                 "To Name",                  "[email protected]")));          var email = container.Resolve<IEmail>();            container.RegisterType<OperationEntity>(             "email", new ContainerControlledLifetimeManager(),             new InjectionConstructor(email));     } 
like image 596
Bern Avatar asked May 30 '13 12:05

Bern


People also ask

What is Singleton pattern in C# Unity?

A Singleton is a programming pattern that allows you to access a class script without using a GetComponent reference. It is an excellent tool to use for creating Manager Classes because it makes the code contained within them easy to reference and access from anywhere.

Are singletons good practice?

The truth is that singletons aren't inherently bad if they're used correctly. The goal of the singleton pattern is to ensure only one instance of a class is alive at any one time. That, however, is not the goal many developers have in mind when using singletons.


2 Answers

First, you need a proper lifetime manager the ContainerControlledLifetimeManager is for singletons.

For custom initialization, you could probably use InjectionFactory

This lets you write any code which initializes the entity.

Edit1: this should help

public static void Register(IUnityContainer container) {     container         .RegisterType<IEmail, Email>(         new ContainerControlledLifetimeManager(),         new InjectionFactory(c => new Email(             "To Name",              "[email protected]"))); } 

and then

var opEntity = container.Resolve<OperationEntity>(); 

Edit2: To support serialization, you'd have to rebuild dependencies after you deserialize:

public class OperationEntity {    // make it public and mark as dependency       [Dependency]    public IEmail _email { get; set;}  } 

and then

OperationEntity entity = somehowdeserializeit;  // let unity rebuild your dependencies container.BuildUp( entity ); 
like image 64
Wiktor Zychla Avatar answered Sep 29 '22 09:09

Wiktor Zychla


You could use:

container.RegisterType<IEmail, Email>(new ContainerControlledLifetimeManager()); 
like image 23
aquaraga Avatar answered Sep 29 '22 08:09

aquaraga