Unity in C#

Unity Application Block

I learned about the Unity Application Block recently so I thought I would share what I learned. Reading this article will help you to easily implement Unity (via the Enterprise Library from Microsoft). Unity Application Block is an easy way to implent IOC and then DI and Service Locator.

IOC is Inversion of Control, DI is Dependency Injection and service locator are design patterns.To have a better understanding and purpose of implementing Unity Application Block we must know IOC, DI and Service Locator.

Check this out:

Inversion of Control

Now, start with implementing Unity Application Block. Using the Nuget Package Manager install Unity version 2.1.505.2 for Visual Studio 2010. If you don't find it then use the Package Manager Console window and simply type 'Install-Package Unity -Version 2.1.505.2'.

I am using this version because it is supported by .Net 4.0 i.e in Visual Studio 2010 and that is what I have here ;)

Any version above this will need .Net 4.5 that is not supported by Visual Studio 2010.

For a better understanding think of a situation of when spawning/creating an object depends upon user's input or type of user.

In this situation you will need to create that object checking the user type.

  1. public class NormalCustomer  
  2. {  
  3.     public void Add(CustomerClass Obj)  
  4.     {  
  5.          .......  
  6.      }  
  7. }  
  8. public class PrivateCustomer  
  9. {  
  10.     public void Add(CustomerClass Obj)  
  11.     {  
  12.          .......  
  13.     }  
  14. }  
In your class where you create an object:
  1. if (usertype == "NormalCustomer")  
  2. {  
  3.     NormalCustomer ObjNormalCustomer = new NormalCustomer();  
  4. }  
  5. else if (usertype == "PrivateCustomer")  
  6. {  
  7.      PrivateCustomer ObjPrivateCustomer = new PrivateCustomer();  

Now if in the future another user type is added to your application and you will need to change the code in the class where the object is spawn apart from creating a new class for a newly added type.

  1. public class ProtectedCustomer  
  2. {  
  3.      public void Add(CustomerClass Obj)  
  4. {  
  5. .......  
  6. }  

By doing this you end up doing some hard coded stuff of checking the user type every time and accordingly creating objects for that type.
  1. if (usertype == "NormalCustomer")  
  2. {  
  3.     NormalCustomer ObjNormalCustomer = new NormalCustomer();  
  4. }  
  5. else if (usertype == "PrivateCustomer")  
  6. {  
  7.     PrivateCustomer ObjPrivateCustomer = new PrivateCustomer();  
  8. }  
  9. else if (usertype == "ProtectedCustomer")  
  10. {  
  11.     ProtectedCustomer ObjProtectedCustomer = new ProtectedCustomer();  
  12. }  
To overcome this, to provide the control of creating the object you will implement Unity that creates the object on your behalf. The question here is how does it know which object to create and when. For that we need to create an interface that will be implemented by your type.
  1. interface ICustomer  
  2. {  
  3.      void Add(ClientClass Obj);  
  4. }  
  5. implementing the interface in your class  
  6. public class NormalCustomer : ICustomer  
  7. {  
  8.     public void Add(CustomerClass Obj)  
  9.     {  
  10.          .......  
  11.      }  
  12. }  
  13. public class PrivateCustomer: ICustomer  
  14. {  
  15.      public void Add(CustomerClass Obj)  
  16.     {  
  17.          .......  
  18.     }  
  19. }  
  20. public class ProtectedCustomer: ICustomer  
  21. {  
  22.     public void Add(CustomerClass Obj)  
  23.     {  
  24.         .......  
  25.     }  
  26. }  
Now what? The Unity still doesn't know which instance of the Customer to create and when. Unity does that by mapping an interface to the type/class.

You do that in the config file.

Just add:

<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />

To the configSections of the configuration in your config file.

Then add:

 

  1. <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">  
  2. <container>  
  3. <register type="namspace.ICustomer, Projectname" mapTo="namspace.NormalCustomer,Projectname" name="NormalCustomer" />  
  4. <register type="namspace.ICustomer, Projectname" mapTo="namspace.PrivateCustomer,Projectname" name="PrivateCustomer" />  
  5. <register type="namspace.ICustomer, Projectname" mapTo="namspace.ProtectedCustomer,Projectname" name="ProtectedCustomer" />  
  6. </container>  
  7. </unity> 
To your configuration.

The container element holds all the mapping information. Here you register your types and map it to the object. This configuration itself is self-explanatory.

Note: Here we have set an attribute name for each register element that is the type of the customer.

Now that the Unity has all the mapping of the types you can use it to create an instance of the Customer.

This is how we do it.
  1. IUnityContainer container = new UnityContainer();  
  2. ICustomer ObjCustomer = container.LoadConfiguration().Resolve<ICustomer>(typeofuser);  
  3. Here typeofuser is of type string that is a name which you had set in the name attribute of register element in config file.  
  4. name="NormalCustomer"  
  5. name="PrivateCustomer"  
  6. name="ProtectedCustomer" 


Similar Articles