Oct 052011

In one of my recent projects I have reached the need to validate that I have all dependencies correctly registered in my UnityContainer. For those that are not familiar with Unity, it is a dependency injection container that allows constructor, property and method call dependency injection.

You can get more information about how Unity works and also download the latest release from Codeplex: http://unity.codeplex.com/

Shout it


The problem with Unity is that you have to have all dependencies registered in the container, otherwise when you will try to resolve a type that depends on other non-registered types you will get an exception. Other containers like StructureMap (http://structuremap.net/structuremap/index.html) have a method of validating the entire configuration so that you can use it in an automated test.

Since Unity lacks this functionality, I have decided to create an extension method that will validate all mappings. Basically this method goes through all registered types and tries to resolve them. If a type is not properly registered, it will fail and the exception you get from Unity lets you know which mapping is invalid.

You can get the code for the extension here: http://codepaste.net/hqf45s