Configure Unity Through Code

Mar 13, 2012 at 5:08 PM
Edited Mar 13, 2012 at 5:08 PM


This is follow up to the previous question. Now we are in the process of using only the code to configure UNITY. However, i have hit 
a roadblock. While configuring using the config file i was able to name the mapping and then use the name to assign to property array's.
I have looked around on MSDN but i did not come across any example where you can do it. The XML in question is as below
<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" />
 <type type="IDependency" mapTo="Dependency" name="D1" />
	<property name="Events" propertyType="IEventHandlerArray">
                <dependency name="Prepare" />
                <dependency name="Pack" />                
 <type type="IDependency" mapTo="Dependency" name="D2" />
   <property name="Events" propertyType="IEventHandlerArray">
                <dependency name="Print" />
                <dependency name="Email" />
   The sample code which i used for configuring the unity is as below
 container.RegisterType<IInvoicingService, InvoicingService>(
                new InjectionProperty("Loggers", 
                    new ResolvedArrayParameter(typeof(ILogger),new ResolvedParameter(typeof(DefaultLogger),"DLogger"),
                                                               new ResolvedParameter(typeof(Logger1),"Logger1"),
                                                               new ResolvedParameter(typeof(Logger2),"Logger2"))),
                new InjectionProperty("Priority", 10));
So i can add dependencies but if i want to configure them once and then reuse to add the same dependencies again i would have to 
configure it again. And as we can see the properties are array of Some type which in turn contain array. 
My question now is how do i have the mapping done in code so that i can use the names just as we can do it in XML.
              <param name="container" />
Mar 14, 2012 at 7:59 AM
Edited Mar 16, 2012 at 1:55 AM

In this example I register 3 concrete logger types to the ILogger interface with each type associated to a name.  Then I configure Unity to inject the appropriate ILogger array into the service:

var container = new UnityContainer();

container.RegisterType<ILogger, DefaultLogger>("DLogger");
container.RegisterType<ILogger, Logger1>("Logger1");
container.RegisterType<ILogger, Logger2>("Logger2");

var prop = new InjectionProperty("Loggers", 
    new ResolvedArrayParameter<ILogger>(


var service = container.Resolve<Service>();

Hopefully that helps.

Randy Levy
Enterprise Library support engineer