Using Unity – Part 3

The previous blog was about registering and invoking different types dynamically. In this one I’d like to show how Unity manages/disposes the instances – say hello to Lifetime Managers.

When a type gets registered, either through the config file or when RegisterType method is explicitly called, the default behavior is that the container uses a transient lifetime manager. In other words, the unity container creates a new instance of the type when Resolve or ResolveAll method is called. Whereas, when you register an existing object using the RegisterInstance method, the container uses a container controlled lifetime manager - a singleton pattern. It does this by storing the reference of the object and that means so as long as the container is ‘alive’, your registered instance does not go out of scope and will be disposed only after the container either goes out of scope or when the code explicitly disposes the container.

Let’s see how we can use these and test if something is a singleton or a transient instance. Continuing on the same solution used in the previous blogs, I have made the following changes:

First is to add typeAlias elements for TransientLifetimeManager type:

   1: <typeAlias alias="transient" type="Microsoft.Practices.Unity.TransientLifetimeManager, Microsoft.Practices.Unity"/>

You then need to tell what type(s) you want to be transient by nature:

   1: <type type="IProduct" mapTo="Product2">
   2:   <lifetime type="transient" />
   3: </type>
   4: <!--<type type="IProduct" mapTo="Product2" />-->

The lifetime element’s type attribute matches with the alias attribute of the typeAlias element. Now since ‘transient’ is the default behavior, you can have a concise version of the same as line 4 shows. Also note that I’ve changed the mapTo attribute from ‘Product’ to ‘Product2’. I’ve done this to help understand the transient nature of the instance of the type Product2. By making this change, you are basically saying when a type of IProduct needs to be resolved, Unity should create an instance of Product2 by default.

   1: public string WriteProductDetails()
   2: {
   3:     return string.Format("Name: {0}<br/>Category: {1}<br/>Mfg Date: {2}<br/>Hash Code: {3}", 
   4:         Name, Category, MfgDate.ToString("MM/dd/yyyy hh:mm:ss tt"), GetHashCode());
   5: }

Again, the above change is purely for the purpose of making the example more clear to understand. The display will show the full date and also displays the hash code of the current instance. The GetHashCode() method returns an integer when an instance gets created – a new integer for every instance.

When you run the application, you’ll see something like the below:


Now when you click on the ‘Get Product2 Instance’ button, you’ll see that the Mfg Date (which is set in the constructor) and the Hash Code are different from the one created on page load. This proves to us that a new instance is created every single time.


To make this a singleton, we need to add a type alias for the ContainerControlledLifetimeManager class and then change the type attribute of the lifetime element to singleton.

   1: <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
   2: ...
   3: <type type="IProduct" mapTo="Product2">
   4:   <lifetime type="singleton" />
   5: </type>

Running the application now gets me the following output:


Click on the button below and you’ll see that the Mfg Date and the Hash code remain unchanged => the unity container is storing the reference the first time it is created and then returns the same instance every time the type needs to be resolved.

Digging more deeper into this, Unity provides more than the two lifetime managers.

ExternallyControlledLifetimeManager – maintains a weak reference to type mappings and instances. Unity returns the same instance as long as the some code is holding a strong reference to this instance. For this, you need:

   1: <typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>
   2: ...
   3: <type type="IProduct" mapTo="Product2">
   4:   <lifetime type="external" />
   5: </type>

PerThreadLifetimeManager – Unity returns a unique instance of an object for each thread – so this effectively is a singleton behavior on a  per-thread basis.

   1: <typeAlias alias="perThread" type="Microsoft.Practices.Unity.PerThreadLifetimeManager, Microsoft.Practices.Unity"/>
   2: ...
   3: <type type="IProduct" mapTo="Product2">
   4:   <lifetime type="perThread" />
   5: </type>

One thing to note about this is that if you use RegisterInstance method to register an existing object, this instance will be returned for every thread, making this a purely singleton behavior. Needless to say, this type of lifetime management is useful in multi-threaded applications (duh!!).

I hope this blog provided some basics on lifetime management of objects resolved in Unity and in the next blog, I’ll talk about Injection. Please see the code used here.


  • Thanks for taking the time to share this, I feel strongly about it and love reading more on this topic. If possible, as you gain knowledge, would you mind updating your blog with more information? It is extremely useful for me.

  • Mark, Thanks for the words of appreciation. I'm in the process of writing the last part of this series in a couple of days. This one will contain handing arrays and generics. But that's as far as I've planned to take. And yes, if I get to know more, I'll definitely share.

    Kacey, Glad you found it informative.


  • Arun - sorry if this is embarrasing, but the two replies above are comments spam - they're not real comments. They contain links to a couple of websites that are trying to increase their google page-rank score. You need a decent catchpa!

  • Andy,

    If you're talking about the first two comments, I published them myself. Not because, they've provided their website, but because they've posted something about my blog article. I myself provide my website (click on my name) when I comment. So I guess I'm ok with this.

    Having said that, I AM in the lookout for a captcha feature to implement in my site. Thanks for taking the time to comment.


  • Thanks Suneeth.

  • @buyvenapro, glad you found it useful.


Comments have been disabled for this content.