Configure Unity Container

Mar 8, 2012 at 3:30 PM

Hi Guys,

            We are having a huge unity config file and it too cumbersome to merge these as we have several projects on the go. To resolve this problem we have now decided that we would have the registrations in code. This is fine for new mappings and registrations  but when i have to add a new mapping through code to the already existing mapping in the config file this is where i am getting stuck. Any ideas on how i can proceed.


 <type type="IService" mapTo="Service">
          <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,&#xA;Microsoft.Practices.Unity.Configuration">
              <param name="container" parameterType="IUnityContainer">
                <dependency />
            <property name="BeforeEvent" propertyType="IEventHandlerArray">
                <dependency name="D1" />
            <property name="AfterEvent" propertyType="IEventHandlerArray">
                <dependency name="D2" />
                <dependency name="D3" />
                <dependency name="D4" />

This is the existing mapping in config file. 

Now i need to another dependency d5 in Property "BeforeEvent"



Mar 9, 2012 at 4:35 AM

If I'm understanding correctly you want to keep your design time configuration which configures a property with an array of dependencies and at runtime add another dependency in addition to the dependencies in the configuration file.

I don't think that Unity supports this "merging" of dependency configuration between config file and code.  (It would be nice if someone can refute that!)

If you call RegisterType from code with a new array dependency it will overwrite the configured information with the new dependency.  The property array dependencies to inject into the object is stored deep within the container's private policies so there is no way to directly access and modify these values.  You could potentially use reflection to try to achieve that but that would be brittle and not recommended.

The only way I can think of to do what I think you want is to resolve the object, extract the injected objects, add the new array item and re-register the type with the container.  Something like:

var container = new UnityContainer().LoadConfiguration();
var service = container.Resolve<Service>();
var items = service.Items;

// this has 2 items
var newItems = new List<Base>(items);
// now add a 3rd

var prop = new InjectionProperty("Items", new ResolvedArrayParameter<Base>(newItems.ToArray()));

Only the Items property should have it's properties modified.  E.g. if there was another property called "Items2" in the config file it's configuration would not be lost..

With all that said, I believe the best approach would be to standardize on one approach to configure the container.  I realize that it could be a bit of work to convert configuration files to code but it should result in an application that is easier to understand, easier to maintain, and harder to break. 

Randy Levy
Enterprise Library support engineer 

Mar 13, 2012 at 2:14 PM

Thanks for the suggestion Randy.