5
votes

I use the unity container to resolve dependencies within an application.

The dependencies and their dependencies (and so on) are registered in the app.config as I need to be able to change the way the application behaves in production.

Sometimes, type registrations for the dependencies are missed, and this only comes to light when an instance of a type is resolved during the application's life time, which means that there may be problems that can only be picked up during integration testing - which is not ideal.

I want to be able to programmatically check (maybe as part of a CI build process) that the unity type registrations have been made correctly. By this I mean that if I resolve an instance of a type, I can have confidence that that type's dependencies (via constructor injection) are also registered and will be resolved.

I only need to check the default built configuration, changes made on live sites are not a consideration here. Also - I don't want to use hard-coded unity registrations.

The only way I can think of doing this at the moment is to parse the unity config file and try to resolve each instance of the type's found...

Is there an easier way of validating that unity registrations are ALL present?

3

3 Answers

2
votes

I use unity and often face this problem.

You could write an application which uses reflection to get Constructor parameters. Something like this: ConstructorInfo.GetParameters(); and recursively obtain each returned parameter's constructor parameters. If you make that list distinct, that would at least give you a list of expected types which should be registered.

Hope this helps.

2
votes

This unit test seems to be enough to validate my configuration so far. Following the instructions at https://msdn.microsoft.com/en-us/library/dn507504(v=pandp.30).aspx I was able to load my container and verify all registrations were resolved.

    [TestMethod]
    public void TestContainer()
    {
        IUnityContainer container = new UnityContainer().LoadConfiguration();
        foreach (var registration in container.Registrations)
        {
            Assert.IsNotNull(container.Resolve(registration.RegisteredType, registration.Name));
        }
    }
1
votes

With regards to

I want to be able to programatically check (maybe as part of a CI build process) that the unity type registrations have been made correctly. By this I mean that if I resolve an instance of a type, I can have confidence that that type's dependencies (via constructor injection) are also registered and will be resolved.

We had the same situation in one of our project and we ended up writing integration tests and reading the Unity.config, then look for registrations typed object as string. Very similar to an approach described below https://blogs.msdn.microsoft.com/miah/2009/04/03/testing-your-unity-xml-configuration/

The problem with this is you have to maintain both registration and tests, but that's the only way you can verify all types are registered correctly.

Simply comparing the xml to expected values are more effective than re-registering all your types (based on your config) and resolving them in tests.