Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking unity container type registrations programmatically

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?

like image 745
Jay Avatar asked May 10 '16 08:05

Jay


People also ask

How do I register a type with Unity container?

Before Unity resolves the dependencies, we need to register the type-mapping with the container, so that it can create the correct object for the given type. Use the RegisterType() method to register a type mapping. Basically, it configures which class to instantiate for which interface or base class.

Is Unity IoC container?

Unity is an IoC container released by Microsoft and is very simple, flexible and easy to use. Though there are many IoC containers available in the market much stronger than Unity, for simplicity and to understand the basic concepts, it's one of the best choices.

What is Unitycontainer C#?

Unity container is an open source IoC container for . NET applications supported by Microsoft. It is a lightweight and extensible IoC container. The source code for Unity container is available at https://github.com/unitycontainer/unity.

What is Hierarchicallifetimemanager?

A special lifetime manager which works like ContainerControlledLifetimeManager, except that in the presence of child containers, each child gets it's own instance of the object, instead of sharing one in the common parent.


1 Answers

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.

like image 66
Fish Avatar answered Nov 13 '22 01:11

Fish