Oct 20, 2009 at 8:30 PM
Edited Oct 20, 2009 at 8:33 PM
I am guessing that your question is not only asking for reasons to use Unity but for reasons to use any DI framework.
All the major DI frameworks will essentially allow you the same benefit which is loose / configurable coupling between application modules.
So the idea is that you want higher level modules to be able to configure lower level modules. The lower level modules have no idea that the higher level modules even exist, so how can you configure it for a module that it has no knowledge of? The answer
is in the container model that DI frameworks provide you with. So there is a common “container” where you can register types to be instantiated. Then all modules can go to this common container to retrieve the desired(configured) type.
Let’s say I have a lower level data access module that I will build off of to make an application. I want this module to be able to instantiate a data access object based on the type of data store needed in the application I am building. So
instead of hard coding the type of data access object to instantiate, I can instantiate the data access object by retrieving it through the container. Then my higher level module that I am building can configure the container to return the type of data access
object I need.
For this all to work you need to build your types based on abstractions, so you can then make different implementations of those abstractions and in your code work with the abstract types only. Also, in order to wire up all the modules you need to be able
to have global way to get at the container, so using a Service Locator of some sort will be essential.