Share via


Configuration Support for Arrays

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Unity Application Block information can be found at the Unity Application Block site.

Unity supports parameter arrays at design time and in the API at run time. The following sections of this topic describe array support in more detail:

  • Configuration Support for Arrays at Design Time
  • Configuration Support for Arrays at Run Time

Configuration Support for Arrays at Design Time

Use the <array> element to reference List arrays at design time. The array element specifies that an array containing the registered instances of a type parameter should be resolved. You can nest arrays, and the array element has the following child elements:

  • array
  • dependency
  • value

For more information, see array Element.

The following example results in a configuration on the container that resolves all the named instances of the specified type that are registered in the container.

      <typeAlias alias="ILoggerArray" type="Microsoft.Practices.Unity.TestSupport.ILogger[], TestSupport.Unity" />
      <typeAlias alias="ArrayConstructor" type="Microsoft.Practices.Unity.TestSupport.ObjectArrayConstructorDependency, Tests.Unity.Configuration" />
    </typeAliases>

    <containers>
      <container name="emptyArray">
        <types>
          <type type="ArrayConstructor">
            <typeConfig >
              <constructor>
                <param name="loggers" parameterType="ILoggerArray">
                  <array />
                </param>
              </constructor>
            </typeConfig>
          </type>
        </types>
      </container>

The following example uses the <dependency> child element to specify to resolve for specific instances of an array.

    <typeAliases>
      <typeAlias alias="ILogger" type="Microsoft.Practices.Unity.TestSupport.ILogger, TestSupport.Unity" /
    <typeAliases>

      <container name="populatedArrayWithValues">
        <types>
          <type type="ILogger" mapTo="MockLogger" name="logger1">
            <lifetime type="singleton" />
          </type>
          <type type="ILogger" mapTo="SpecialLogger" name="logger2">
            <lifetime type="singleton" />
          </type>
          <type type="ArrayConstructor">
            <typeConfig>
              <constructor>
                <param name="loggers" parameterType="ILoggerArray">
                  <array>
                      <dependency name="logger2"/>
                      <dependency name="logger1"/>
                  </array>
                </param>
              </constructor>
            </typeConfig>
          </type>
        </types>
      </container>

For more information about run-time support, see Configuration Support for Arrays at Run Time.

Configuration Support for Arrays at Run Time

Unity provides API configuration support for arrays by providing a resolver object that resolves all the named instances of the type registered in a container and an InjectionParameterValue that is used to configure parameters for constructor or method injection. The implementation for the ResolvedArray API support relies on the ResolveAll method in IUnityContainer. The ResolveAll method enables you to specify the type and returns all instances of all registered types requested even after you register multiple typenames of the same type.

Configuration support for ResolvedArray is provided with the <array> configuration element.

You can inject an entire array or just specific instances in the array as follows:

  • To inject an array with all the registered objects in the container (these can be either RegisteredTypes or RegisteredInstances, as long as a non-null name is used), use the non-array-specific ResolvedParameter for the type array or the <dependency/> element in the configuration. Attribute support for injecting all register members of an array is provided by using the [Dependency] attribute. Using the [Dependency] attribute produces the equivalent results for injection.
  • To inject an array with specific instances, you must to use the ResolvedArrayParameter or the <array/> element in the configuration. There is no attribute support in this case. Use InjectionParameterValues or the <array/> element to specify how each element in the array should be resolved.

The following code describes a class.

class Foot{
    public Foot(IBar[] bars) { }
}
'Usage
Class Foot
    Public  Sub New(ByVal bars() As IBar)
    End Sub
End Class

The following example code shows the API you would use to configure the class in the preceding code for injection.

container.Configure<InjectedMembers>()
    .ConfigureInjectionFor<Foot>(
        new InjectionConstructor(new ResolvedArrayParameter<IBar>());
'Usage
container.Configure(Of InjectedMembers)()
    .ConfigureInjectionFor(Of Foot)(
        New InjectionConstructor(New ResolvedArrayParameter(Of IBar)())

To get all the registered instances of the specified array, use ResolvedArrayParameter. In this example, you would use ResolvedArrayParameter<IBar[]>. At resolve time, the container calls ResolveAll<IBar> and injects the resulting array into the constructor.

Alternatively, you could use the following.

container.Configure<InjectedMembers>()
    .ConfigureInjectionFor<Foot>(
        new InjectionConstructor(new ResolvedArrayParameter<IBar>(
            typeof(IBar),
            new ResolvedParameter<IBar>("some named bar"),
            new BarImpl()))
...
'Usage
container.Configure(Of InjectedMembers)()
    .ConfigureInjectionFor(Of Foot)(
        New InjectionConstructor(New ResolvedArrayParameter(Of IBar)(
            Type.GetType(IBar),
            new ResolvedParameter(Of IBar)("some named bar"),
            New BarImpl()))
...