Microsoft releases Unity, an IoC library

February 17th, 2008

Here is a bit of interesting news out of Microsoft.  They have released their first IoC container named Unity.  It is out on CodePlex now: http://www.codeplex.com/unity

If you are looking for a similar product with a longer lineage, you could look up: StructureMap, Spring.Net, and Castle Windsor.  StructureMap is maintained by Jeremy Miller, who is a favorite blogger of mine (everyone should read his series on Build your own CAB).  Spring.Net is a port of an existing Java project, and Castle Windsor seems to have a lot of mind share.  Currently, Spring.Net is the only IoC library that I have used.

OK, definitions:
IoC: Inversion of Control, also called Dependency Injection.
This is based on a pattern found in the Gang Of Four books on Design Patterns.
You can read more about this pattern at Martin Fowlers web site:
http://martinfowler.com/articles/injection.html
Or on Wikipedia:
http://en.wikipedia.org/wiki/Inversion_of_control

The basic principle that IoC is used for is Loose Coupling.  If you have a multi-layered application, each layer should not have to know specific details about the other layers.  So each object in a specific layer will implement an interface that the IoC provider knows about.  Then, when a specific dependency is needed, you ask the IoC container for it (note: the IoC container is Tightly coupled) for the object to do the work.   If you look at the Asp.Net Membership Provider, you can see a simple example of that at work.

One of the ways that this can help you is that now all of your middle pieces can be swapped out without having to make massive code changes.  This also makes testing easier, as now you can more easily stub/mock/fake dependencies and just test your application logic.

Also, if you really get into the whole IoC thing, you will find that there are a number of other cool Computer Sciency things are possible.  Like Aspect Oriented Programming (unknown if Unity supports that or not).

Now with all of the other IoC libraries out there, why would I like the fact that Microsoft has created their own? 
One of the current issues with a lot of Microsoft software, especially the samples, is that they are all VERY tightly coupled (again, tight coupling is not good), but it is a pain to get around without an IoC solution.  And, as per Microsoft’s own structure, they will not use any software that they didn’t write or own.   So I’m hopeful that now they can start producing some software with loose coupling in mind, and make all of our lives easier.

All right, there are still a lot of definitions and ideas that could leave you puzzled, that happens.  That is also why we have google.  If you see something you don’t understand, leave a comment and I’ll try to clarify, or point you to someone else with a better explanation than I can give.

Finally, if you are looking for more information on this sort of thing, check out the session on Castle Windsor at Boise Code Camp on March 8.