I have been going through implementing some IoC pattern using Unity and so I decided to share my learnings (I know that’s not an English word, but you get the point).
Ok, so I have an ASP.net project named ProductWeb and a class library called ProductModel. In the model library, I have a class called Product:
In the Page_Load event of the default.aspx, I’ll need something like:
Now, let’s go ‘Unity’fy this application. I assume you have all the bits for the pattern. If not, get it from here.
I found this schematic representation of Unity pattern from the above link.
This image might not make much sense to you now, but as we proceed, things will get better.
The first step to implement the Inversion of Control pattern is to create interfaces that your types will implement. An IProduct interface is added to the ProductModel project.
Let’s make our Product class to implement the IProduct interface. The application will compile and run as before despite the changes made.
Add the following references to your web project:
We need to add a few lines to the web.config file. The line below tells what version of Unity pattern we’ll be using.
Add another block with the same name as the section name declared above – ‘unity’.
From the Unity Configuration schematic shown above, you see that the ‘unity’ block has a ‘typeAliases’ and a ‘containers’ segment.
The typeAlias element gives a ‘short-name’ for a type. This ‘short-name’ can be used to point to this type any where in the configuration file (web.config in our case, but all this information could be coming from an external xml file as well).
The container element holds all the mapping information. This container is referenced through its name attribute in the code and you can have multiple of these container elements in the containers segment.
The ‘type’ element in line 10 basically says: ‘When Unity requests to resolve the alias IProduct, return an instance of whatever the short-name of Product points to’. This is the most basic piece of Unity pattern and all of this is accomplished purely through configuration. So, in future you have a change in your model, all you need to do is
- implement IProduct on the new model class and
- either add a typeAlias for the new type and point the mapTo attribute to the new alias declared
- or modify the mapTo attribute of the type element to point to the new alias (as the case may be).
Now for the calling code. It’s a good idea to store your unity container details in the Application cache, as this is rarely bound to change and also adds for better performance. The Global.asax.cs file comes for our rescue:
All this says is: create an instance of UnityContainer() and read the ‘unity’ section from the configSections segment of the web.config file. Then get the container named ‘unityContainer’ and store it in the Application cache.
In my code-behind file, I’ll make use of this UnityContainer to create an instance of the Product type.
Looking the ‘else’ block, I’m asking the unityContainer object to resolve the IProduct type. All this does, is to look at the matching type in the container, read its mapTo attribute value, get the full name from the alias and create an instance of the Product class.
Fabulous!! I’ll go more in detail in the next blog. The code for this blog can be found here.