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.
- public class NormalCustomer
- {
- public void Add(CustomerClass Obj)
- {
- .......
- }
- }
- public class PrivateCustomer
- {
- public void Add(CustomerClass Obj)
- {
- .......
- }
- }
- if (usertype == "NormalCustomer")
- {
- NormalCustomer ObjNormalCustomer = new NormalCustomer();
- }
- else if (usertype == "PrivateCustomer")
- {
- PrivateCustomer ObjPrivateCustomer = new PrivateCustomer();
- }
- public class ProtectedCustomer
- {
- public void Add(CustomerClass Obj)
- {
- .......
- }
- }
- if (usertype == "NormalCustomer")
- {
- NormalCustomer ObjNormalCustomer = new NormalCustomer();
- }
- else if (usertype == "PrivateCustomer")
- {
- PrivateCustomer ObjPrivateCustomer = new PrivateCustomer();
- }
- else if (usertype == "ProtectedCustomer")
- {
- ProtectedCustomer ObjProtectedCustomer = new ProtectedCustomer();
- }
- interface ICustomer
- {
- void Add(ClientClass Obj);
- }
- implementing the interface in your class
- public class NormalCustomer : ICustomer
- {
- public void Add(CustomerClass Obj)
- {
- .......
- }
- }
- public class PrivateCustomer: ICustomer
- {
- public void Add(CustomerClass Obj)
- {
- .......
- }
- }
- public class ProtectedCustomer: ICustomer
- {
- public void Add(CustomerClass Obj)
- {
- .......
- }
- }
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:
- <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
- <container>
- <register type="namspace.ICustomer, Projectname" mapTo="namspace.NormalCustomer,Projectname" name="NormalCustomer" />
- <register type="namspace.ICustomer, Projectname" mapTo="namspace.PrivateCustomer,Projectname" name="PrivateCustomer" />
- <register type="namspace.ICustomer, Projectname" mapTo="namspace.ProtectedCustomer,Projectname" name="ProtectedCustomer" />
- </container>
- </unity>
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.
- IUnityContainer container = new UnityContainer();
- ICustomer ObjCustomer = container.LoadConfiguration().Resolve<ICustomer>(typeofuser);
- Here typeofuser is of type string that is a name which you had set in the name attribute of register element in config file.
- name="NormalCustomer"
- name="PrivateCustomer"
- name="ProtectedCustomer"
Comments
Join the conversation! Your thoughts help the community grow.