I've been working with Microsoft's Unity IOC container. There are a bunch of overloads for the RegisterType()
method all looking similar to
IUnityContainer RegisterType(Type t, params InjectionMember[] injectionMembers);
I'm wondering when the injectionMembers
parameters are for? I couldn't find any documentation for the them (even though they're in every overload) and none of the sample code I looked at use them.
Am I missing something here? Are they not commonly used or did I just miss the examples?
The overload with the InjectionMember
array is used, when you do not provide a configuration file that Unity tells how to create an instance of the given type or if you want to create an instance by another way than defined in the configuration file.
The overloads are used, when you want to configure an unity container without a configuration file. An InjectionMember
can be a constructor, property or method call.
The following code, taken from the Unity help, shows how to use InjectionMembers
through the fluent interface of the container.
IUnityContainer myContainer = new UnityContainer();
myContainer.Configure<InjectedMembers>()
.ConfigureInjectionFor<MyObject>(
new InjectionConstructor(12, "Hello Unity!"),
new InjectionProperty("MyStringProperty", "SomeText"));
The following XML configuration is equivalent to the code above.
<type type="MyObject" mapTo="MyObject" name="MyObject">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
<constructor>
<param name="someInt" parameterType="int">
<value value="12"/>
</param>
<param name="someText" parameterType="string">
<value value="Hello Unity!"/>
</param>
</constructor>
<property name="MyStringProperty" propertyType="string">
<value value="SomeText"/>
</property>
</typeConfig>
</type>