Configure Unity Through Code

Mar 13, 2012 at 4:08 PM
Edited Mar 13, 2012 at 4: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">
            <constructor>
              <param name="container" parameterType="IUnityContainer">
                <dependency />
              </param>
            </constructor>
            <property name="BeforeEvent" propertyType="IEventHandlerArray">
              <array>
                <dependency name="D1" />
               </array>
            </property>
            <property name="AfterEvent" propertyType="IEventHandlerArray">
              <array>
                <dependency name="D2" />
                <dependency name="D3" />
                <dependency name="D4" />
              </array>
            </property>            
          </typeConfig>
        </type>
 <type type="IDependency" mapTo="Dependency" name="D1" />
	<property name="Events" propertyType="IEventHandlerArray">
              <array>
                <dependency name="Prepare" />
                <dependency name="Pack" />                
	      </array>
        </property>
 </type>
 <type type="IDependency" mapTo="Dependency" name="D2" />
   <property name="Events" propertyType="IEventHandlerArray">
              <array>
                <dependency name="Print" />
                <dependency name="Email" />
	      </array>
     </property>
 </type>
........
   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" />
  Thanks
Bhupendra 
Mar 14, 2012 at 6:59 AM
Edited Mar 16, 2012 at 12: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>(
        container.Resolve<ILogger>("DLogger"),
        container.Resolve<ILogger>("Logger1"),
        container.Resolve<ILogger>("Logger2")
        ));

container.RegisterType<Service>(prop);

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

Hopefully that helps.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com