Modularize Unity Configuration

Aug 19, 2011 at 10:08 PM

We have a large application with several modules. Each module expose WCF services.

We have developed a custom InstanceProvider which is assigned in a custom IServiceBehavior (in the ApplyDispatchBehavior method).
A Singleton UnityContainer is used so the configuration code is executed just once. There are several examples of this in the web.

This works for a single small application, single module, etc.

But, How could we split the Unity registration code in several configuration classes per module or plugin?

Other questions arise:

Should we have a single static UnityContainer and add each module configuration or create a static UnityContainer per module? (using code, not config files).

Is it posible to isolate the code responsible of IoC aspect from each module (Each module just would store the registration of dependencies)?

Thanks for your help

Aug 23, 2011 at 10:48 AM

You could use custom classes derived from UnityContainerExtension to modularize the configuration. One of these classes per module can bundle the configuration of all of the module's dependencies in its Initialize() method.

How do you inject the custom InstanceProvider into the WCF pipeline? If you are using a custom ServiceHostFactory I think it would make sense to reuse one container for all of the modules instantiated via the factory. Or you could use child containers per module where each child is initialized with its own configuration. If each module is instantiated on its own (via a console application maybe) I would go for a one independent container per module approach.

If you are using some kind of naming convention it should not be too hard to find the appropriate configuration classes in each module and apply them to a container.

Aug 23, 2011 at 6:00 PM

I found the following to be very helpful:

Aug 24, 2011 at 3:41 PM

Thanks Damien.

I now use a custom ServiceHostFactory, and yes, I have decide to use a unique static Container which I get from a "framework" library. My custom IInstanceProvider ServiceBehavior uses this unique Container to get the instance, and configuration is done during WCF service setup.

So each service adds its own dependencies in a new class that is called in an AppInitialize static method.

This way all custom service factories, host and behaviors can be reused in each module and tests can be added to ensure that all facade IServices are resolved properly.

Best regards.

Aug 29, 2011 at 7:17 AM

We have a simple IModule

void Register();

in the host app we have a bootstrapper.cs where we instantiate our concrete modules into a list and iterate though the list calling Register().