title: Unity Singleton Code
body: <p>I'm new to <a href="http://unity.codeplex.com/">Unity</a> 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. </p>
  
  <p>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.</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>Any help would be appreciated in getting this Unity code to work</p>
  
  <pre><code>    public static void Register(IUnityContainer container)
      {
          container
              .RegisterType&lt;IEmail, Email&gt;(
              new InjectionFactory(c =&gt; new Email(
                  "To Name", 
                  "to@email.com")));
  
          var email = container.Resolve&lt;IEmail&gt;();  
  
          container.RegisterType&lt;OperationEntity&gt;(
              "email", new ContainerControlledLifetimeManager(),
              new InjectionConstructor(email));
      }
  </code></pre>