IUnityContainer.Configure

Mar 6, 2012 at 7:01 PM

I want to set Properties to some classes and avoid using Attributes. You can do all using the Configure method of the unity container:

First register the type

unityContainer.RegisterType<MyInterface,MyImpl>(
                     
new ContainerControlledLifetimeManager());

If you have multiple constructors you'll have to to this so Unity invokes the parameterless constructor (if none set Unity will go for the fattest one)

unityContainer.Configure<InjectedMembers>()
                           
.ConfigureInjectionFor<MyImpl>(
                               
new InjectionConstructor());

Setting property dependency

unityContainer.Configure<InjectedMembers>()
                   
.ConfigureInjectionFor<MyImpl>(
                         
new InjectionProperty(
                             
"SomePropertyName",
                               
new ResolvedParameter<MyOtherInterface>()));

Configuring method dependency

unityContainer.Configure<InjectedMembers>()
                   
.ConfigureInjectionFor<MyImpl>(
                       
new InjectionMethod(
                           
"SomeMethodName",
                           
new ResolvedParameter<YetAnotherInterface>()));

 

But i don't know to to do it using xml configuration???

Mar 7, 2012 at 6:25 AM

Here are the configuration file equivalents of the posted code.

For the code:

unityContainer.RegisterType<MyInterface,MyImpl>(
                     new ContainerControlledLifetimeManager());
<register type="UnityConfigFileInjections.MyInterface, UnityConfigFileInjections" mapTo="UnityConfigFileInjections.MyImpl, UnityConfigFileInjections">
  <lifetime type="singleton"/> <!-- default so not really necessary -->
</register>

To invoke the parameterless constructor:

unityContainer.Configure<InjectedMembers>()
                            .ConfigureInjectionFor<MyImpl>(
                                new InjectionConstructor());
<register type="UnityConfigFileInjections.MyInterface, UnityConfigFileInjections" mapTo="UnityConfigFileInjections.MyImpl, UnityConfigFileInjections">
  <lifetime type="singleton"/> <!-- default so not really necessary -->
  <constructor/>
</register>

Setting property dependency

unityContainer.Configure<InjectedMembers>()
                    .ConfigureInjectionFor<MyImpl>(
                         new InjectionProperty(
                             "SomePropertyName",
                                new ResolvedParameter<MyOtherInterface>()));
<register type="UnityConfigFileInjections.MyInterface, UnityConfigFileInjections" mapTo="UnityConfigFileInjections.MyImpl, UnityConfigFileInjections">
  <lifetime type="singleton"/><!-- default so not really necessary -->
  <constructor />
  <property name="SomePropertyName" 
            dependencyType="UnityConfigFileInjections.MyOtherInterface, UnityConfigFileInjections" />
</register>

Configuring method dependency

unityContainer.Configure<InjectedMembers>()
                    .ConfigureInjectionFor<MyImpl>(
                        new InjectionMethod(
                            "SomeMethodName",
                            new ResolvedParameter<YetAnotherInterface>()));
<register type="UnityConfigFileInjections.MyInterface, UnityConfigFileInjections" mapTo="UnityConfigFileInjections.MyImpl, UnityConfigFileInjections">
  <lifetime type="singleton"/><!-- default so not really necessary -->
  <constructor />
  <method name="SomeMethodName">
    <param name="someOtherInterface"
            dependencyType="UnityConfigFileInjections.YetAnotherInterface, UnityConfigFileInjections"/>
  </method>
</register>

For the dependencyType this assumes that those interfaces are also registered with the container so they can be resolved.

Specifying Values for Injection provides some other examples.

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