This topic explains how to configure the information required so that Unity will automatically populate constructor and method parameters and property values when it resolves instances of types. The and
For more information about the format of the Unity configuration file, see Using Design-Time Configuration.
This topic contains the following sections describing values for injection:
- Resolving Values from the Container
- Giving Values in Configuration
- Configuring Array Values
Resolving Values from the Container
Probably the most common use is the case when you want the value for a parameter or property to be resolved from the container. You can use the
There are several shorthand attributes to expedite the configuration process. If you want to resolve the default registration for the type of the parameter or property, you can simply leave out the
<register type="ILogger" mapTo="SerialPortLogger">
<property name="Settings" />
</register>
Similarly, since specifying a dependency is so common, instead of using the child
<!-- Explicit dependency element -->
<register type="ILogger" mapTo="SerialPortLogger">
<property name="Settings">
<dependency type="SpecialPortSettings" name="highSpeedSettings" />
</property>
</register>
The following example uses attributes instead of the
<!--Use attributes instead of the dependency element -->
<register type="ILogger" mapTo="SerialPortLogger">
<property name="Settings" dependencyType="SpecialPortSettings”
dependencyName="highSpeedSettings" />
</register>
The previous examples use the
Optional values, specified by using the
Giving Values in Configuration
The other most common scenario is to specify a particular value to be injected, such as a string or integer. The explicit way to do is by using the
Like the
<register type="ILogger" mapTo="NetworkLogger">
<constructor>
<!-- Explicit value child element -->
<param name="logHost">
<value value="loghost.example.org" />
</param>
<!-- using shorthand attribute -->
<param name="header" value="Log Entry:" />
<!-- custom typeconverter, no shorthand available -->
<param name="connectionSettings">
<value value="port=123;protocol=tcp;timeout=30"
typeConverter="ConnectionSettingsTypeConverter" />
</param>
</constructor>
</register>
Configuring Array Values
The container handles parameter or properties of array types uniquely. There are several options for how to configure array values; the choice depends on what you want to accomplish. See Registering Injected Parameter and Property Values.
The simplest approach for arrays is the default behavior of the container for arrays. In that case simply use the
If you wish to have only explicit values injected into the array, then you can use the
If you want an empty array, use
<register type="ILogger" mapTo="NetworkLogger">
<!-- Empty array -->
<property name="noSettings">
<array />
</property>
<!-- Type NetworkSettings[], get default array injection -->
<property name="allSettings" />
<!--Type NetworkSettings[], get only the specified three values -->
<property name="someSettings">
<array>
<dependency name="fastSettings" />
<optional name="turboSettings" />
<value value="port=1234;protocol=tcp;timeout=60"
typeConverter="ConnectionSettingsTypeConverter" />
</array>
</property>
</register>