Unity Container

Mar 14, 2012 at 4:12 PM

How does the Unity Container resolve the controller classes?  I do not see them registered anywhere.

Mar 14, 2012 at 6:24 PM
Edited Mar 14, 2012 at 6:35 PM


As explained in the Chapter 11: Server-Side Implementation - Dependency Injection, the Unity container is registered as the MVC dependency resolver of the application. Doing this, when a controller is need, the web server will use the Unity container to obtain it, which will resolve the dependencies of the controller class.

As far as I know, Unity can resolve classes that are not manually registered in the container as long as the specific type that you want to compose can be "accessed." For example, if you would want to compose a VehicleController through the container in a class inside the MileageStats.Web namespace, you could simply do this without requiring to register the VehicleController class:

VehicleController controller = ServiceLocator.Current.GetInstance<Controllers.VehicleController>();

So, based on my understanding, when a controller is needed the Unity container can locate and resolve it without problems as it's an specific type within the assembly.

If instead of composing a specific type you want to use an interface, then you will need to map the interface to a specific type registering it in the container. For example, in the MileageStats RI this mappings are registered in the container using the unity.config file.

You can find more information about Unity's capabilities in its codeplex site:

I hope you find this useful,

Damian Cherubini