Source Schema for the Unity Application Block

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.

This topic lists the elements and attributes used to configure the Unity Application Block. The configuration file has the following section-handler declaration.

<configSections>
  <section name="unity"
           type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                 Microsoft.Practices.Unity.Configuration, Version=1.2.0.0,
                 Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>

The section-handler declaration contains the name of the configuration settings section and the name of the section-handler class that processes configuration data in that section. The convention used in this documentation is to name the configuration settings section unity. Because you must retrieve the configuration settings section, you can use any name. The following example shows how you declare Unity's configuration section in a .NET configuration file:

Microsoft.Practices.Unity.Configuration.UnityConfigurationSection

unity Element

The unity element specifies the Unity Application Block configuration. This element is required.

<unity>

  <typeAliases>
    ...
  </typeAliases>

  <containers>
    <container name="containerOne">
      <types>
        ...
      </types>
      <instances>
        ...
      </instances>
      <extensions>
        ...
      </extensions>
      <extensionConfig>
        ...
      </extensionConfig>
    </container>
  </containers>

</unity>

Child Elements

The following sections describe the child elements of the unity element. The unity element has the following child elements:

  • typeAliases
  • containers

typeAliases Element

The typeAliases element holds a collection of optional type aliases that allows you to more easily specify the types required in mappings, lifetimes, instances, extensions, and elsewhere in the configuration for Unity containers. You cannot use type aliases when specifying element types in extensibility points such as typeConfig and custom injection values. The only available child element is a collection of typeAlias elements.

<typeAliases>

  <!-- Lifetime manager types -->
  <typeAlias alias="singleton"
       type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
             Microsoft.Practices.Unity" />
  <typeAlias alias="external"
       type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,
             Microsoft.Practices.Unity" />
  <typeAlias alias="perThread"
       type="Microsoft.Practices.Unity.PerThreadLifetimeManager,
             Microsoft.Practices.Unity" />
  <!-- User-defined type aliases -->
  <typeAlias alias="IMyInterface"
       type="MyApplication.MyTypes.MyInterface, MyApplication.MyTypes" />
  <typeAlias alias="MyRealObject" 
       type="MyApplication.MyTypes.MyRealObject, MyApplication.MyTypes" />
  <typeAlias alias="MyCustomLifetime" 
       type="MyApplication.MyLifetimeManager, MyApplication.MyTypes" />

</typeAliases>

typeAlias Element

The typeAlias element defines a single alias between a specific type and a name that you can use to refer to the type elsewhere in the configuration.

Attributes

The following table lists the attributes for the typeAlias element.

Attribute

Description

alias

The name of the alias and the shorthand name to use elsewhere in the configuration to refer to the specified type. This attribute is required.

type

The full type name of the type for this alias. This attribute is required.

containers Element

The containers element holds a collection of Unity containers. The only child element is a collection of container elements.

container Element

The container element holds details of an individual container.

<container name="containerOne">
  <types>
    ...
  </types>
  <instances>
    ...
  </instances>
  <extensions>
    ...
  </extensions>
  <extensionConfig>
    ...
  </extensionConfig>
</container>

Attributes and Child Elements

The following table lists the attributes for the container element.

Attribute

Description

name

The name of the container. This attribute is optional.

The container element has the following child elements:

  • types Element
  • instances Element
  • extensions Element
  • extensionConfig Element

types Element

The types element holds a collection of the registered type mappings for this container. The types element contains the following:

  • It contains a series of type elements that add individual types.
  • It contains the specification that describes how to perform injection.

Child Elements

The only child element of the types element is the type element.

type Element

The type element defines a type mapping for the Unity container. If you specify a name, that name is used for the type mapping. If you do not specify a name, it creates a default mapping for the specified types. You can specify a lifetime manager for each mapping. If no explicit lifetime manager is configured for a type, transient lifetime management is exercised.

<types>

  <!-- Type mapping with no lifetime — defaults to "transient" -->  
  <type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass" />

  <!-- Type mapping using aliases -->  
  <type type="IMyInterface" mapTo="MyRealObject" />

  <!-- Lifetime managers specified using the type aliases -->
  <type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
    <lifetime type="singleton" /> 
  </type>
  <type type="IMyInterface" mapTo="MyRealObject" name="RealObject">
    <lifetime type="external" />
  </type>
  <type type="IMyInterface" mapTo="MyRealObject" name="RealObject">
    <lifetime type="perThread" />
  </type>

  <!-- Lifetime manager specified using the full type name -->
  <!-- Any initialization data specified for the lifetime manager -->
  <!-- will be converted using the default type converter -->
  <type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
    <lifetime value="sessionKey"
              type="MyApplication.MyTypes.MyLifetimeManager,
                    MyApplication.MyTypes" />
  </type>

  <!-- Lifetime manager initialization using a custom TypeConverter -->
  <type type="IMyInterface" mapTo="MyRealObject" name="CustomSession">
    <lifetime type="MyCustomLifetime" value="ReverseKey"
              typeConverter="MyApplication.MyTypes.MyTypeConverter,
                             MyApplication.MyTypes" />
  </type>

  <!-- type with injection parameters define in configuration -->
  <!-- Type mapping using aliases defined above -->  
  <type type="IMyService" mapTo="MyDataService" name="DataService">
    <typeConfig>
      <constructor>
        <param name="connectionString" parameterType="string">
          <value value="AdventureWorks"/>
        </param>
        <param name="dataService" parameterType="IMyService">
          <dependency />
        </param>
      </constructor> 
      <property name="MyRealObject" propertyType="IMyInterface" />
      <method name="Initialize">
        <param name="connectionString" parameterType="string">
          <value value="contoso"/>
        </param>
        <param name="dataService" parameterType="IMyService">
          <dependency />
        </param>
      </method>
    </typeConfig>
  </type>

</types>

Attributes and Child Elements

The following table lists the attributes for the type element.

Attribute

Description

name

The name to use when registering this type. This attribute is optional.

type

The source type to configure in the container. The type of the object to map from if this is a mapping registration or the type of the object if this is a singleton registration. Can be a user-defined alias specified in the typeAliases section of the configuration. This attribute is required.

mapTo

The destination type for type mapping. The type of the object to map to if this is a mapping registration. This attribute is optional.

The type element has the following child elements:

  • lifetime Element
  • typeConfig Element

lifetime Element

The lifetime element contains details of the lifetime manager to use with the type mapping.

<!-- Standard singleton lifetime manager specified using a type alias -->
<type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
  <lifetime type="singleton" /> 
</type>

<!-- Custom lifetime manager specified using a type alias -->
<!-- and initialized using a custom TypeConverter  -->
<type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
  <lifetime type="MyCustomLifetime" value="ReverseKey"
            name="CustomSessionLifetime"
            typeConverter="MyApplication.MyTypes.MyTypeConverter, MyApplication.MyTypes" />
</type>

Attributes

The following table lists the attributes for the lifetime element.

Attribute

Description

name

The name to use when registering this lifetime manager. This attribute is optional.

type

The type of the lifetime manager to use for this mapping. Can be a user-defined alias specified in the typeAliases section of the configuration or one of the default aliases singleton or external. This attribute is required.

value

Any value required to initialize the lifetime manager. This attribute is optional.

typeConverter

The type converter to use to convert the value provided to match the type of the instance. If not specified, the default converter for the specified type is used. Aliases are allowed. This attribute is optional.

typeConfig Element

The typeConfig element specifies general configuration for a type/name combination. Injection is the default configuration and is the only option available without any modification. The injection extension class applies dependency injection requirements you define within the element. You can use this element to configure custom dependency injection settings for the container. Unity includes the configuration element class TypeInjectionElement that you can use to specify dependency injection requirements for a type based on the constructor, property, and method injection capabilities that Unity supports, as shown in the following example, where the type in the <type/> element is a generic type with a T1 generic type parameter.

<typeConfig name=""
            extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                          Microsoft.Practices.Unity.Configuration">

  <constructor>
    <param name="connectionString" parameterType="string">
      <value value="AdventureWorks"/>
    </param>
    <param name="dataService" parameterType="IMyService">
      <dependency />
    </param>
    <param name="myGenericType" genericParameterName="T1[]">
      <array />
    </param>
  </constructor> 

  <property name="MyRealObject" propertyType="IMyInterface" />

  <method name="Initialize">
    <param name="connectionString" parameterType="string">
      <value value="contoso"/>
    </param>
    <param name="dataService" parameterType="IMyService">
      <dependency />
    </param>
    <param name="myGenericType" genericParameterName="T1[]">
      <array />
    </param>
  </method>

</typeConfig>

Note

The type in this <type/> element must be a generic type with a T1 generic type parameter.

Attributes and Child Elements

The following table lists the attributes for the typeConfig element.

Attribute

Description

name

The name to use when registering the configuration of dependency injection requirements for this type. This attribute is optional only when the configuration contains a single typeConfig element. If you include more than one typeConfig element, you must differentiate them with unique values for the name attribute.

extensionType

The type that will interpret the dependency configuration for this object. The default type is TypeInjectionElement. Allows developers to create and specify custom dependency configuration sections for objects. Must be a fully qualified type name. This attribute is optional.

When you specify the TypeInjectionElement section handler and use the default extension type, the typeConfig element contains the following child elements:

  • constructor Element
  • property Element
  • method Element

After you specify the TypeInjectionElement section handler and define your custom extension, you define the optional child elements.

constructor Element

The constructor element contains details of the constructor injection requirements for this object type. The constructor element has no attributes. You can include only one constructor element in each typeConfig section.

<constructor>
  <param name="connectionString" parameterType="string">
    <value value="AdventureWorks"/>
  </param>
  <param name="dataService" parameterType="IMyService">
    <dependency />
  </param>
  <param name="myGenericType" genericParameterName="T1[]">
    <array />
  </param>
</constructor> 

The preceding configuration corresponds to a constructor that has three parameters, as shown here.

public class MyCustomClass
{
  public MyCustomClass(string connectionString, IMyService dataService, T1[] mygenericType)
  { 
    ... 
  }
} 
'Usage
Public Class MyCustomClass
  Public Sub New(connectionString As String, dataService As IMyService, T1() mygenericType)
    ... 
  End Sub
End Class 

The configuration settings specify that the value of the connectionString parameter will be "AdventureWorks" and that the value of the dataService parameter will be the result of any mapping for the IMyService interface stored in the container (effectively a call to the Resolve method for the class IMyService).

Child Elements

The constructor element has one or more child elements named param. These correspond to the parameters of the constructor you want to specify. For more information, see param Element.

property Element

The property element contains details of the property (setter) injection requirements for this object type. You can include one or more property elements in each typeConfig element.

<!-- using default dependency injection through container -->
<property name="MyRealObject" propertyType="IMyInterface" />

<!-- using a specific value for the property -->
<property name="MyProjectName" propertyType="String">
  <value value="MyNewProject" />
</property>

<!-- using a specific value and type converter for that value -->
<property name="myObject" propertyType ="IMyObject">
  <value type="MyRealObjectInstance" typeConverter="MyTypeConverter" />
</property>

<!-- using a default dependency through the container -->
<property name="dataService" propertyType ="IMyService">
  <dependency />
</property>

<!-- using a specific named (non-default) dependency -->
<property name="dataService" propertyType ="IMyService">
  <dependency name="MyMappingName" />

<!-- specifying the name of a generic parameter -->
<property name="myGenericType" genericParameterName="T1">
</property>

Attributes and Child Elements

The following table lists the attributes for the property element.

Attribute

Description

name

The name of the property to apply dependency injection to. This attribute is required.

genericParameterName

The name of a generic type parameter in the generic type for which injection is configured. You cannot set both propertyType and genericParameterName. If both attributes are set, a ConfigurationErrorException error is raised when the configuration is read from a file.

propertyType

The data type of this property. Can be a user-defined alias specified in the typeAliases section of the configuration. This attribute is required. You cannot set both propertyType and genericParameterName.

If you specify a value for the propertyType attribute, the property element can have no child elements or one child element. You can have a single value child element, a single dependency child element, or an array child element. Use a value element to specify the value for the property or use a dependency element to specify a non-default dependency that Unity should apply when setting the property. If you do not include a child element, Unity attempts to resolve the value of the property using default dependency injection resolved through the container.

There are two ways to specify a value for the genericParameterName attribute:

  • Use just the generic parameter name, for example "T", from the configured type's generic type parameters. In this case, you can only have an optional <dependency/> child. The result is a configuration on the container that causes an instance of whatever "T" is in the closed generic type to be resolved based on this open generic configuration.
  • Add an array designator suffix, "[]", to the generic parameter name, for example "T[]". In this case, you can only have the <array/> child with no elements. This results in a configuration on the container that will ResolveAll() the instances of whatever "T" is in the closed generic type to be resolved based on this open generic configuration.

For more information about the child elements, see value Element and dependency Element.

method Element

The method element contains details of the method injection requirements for this object type. You can include one or more method elements in each typeConfig section.

<method name="Initialize">
  <param name="connectionString" parameterType="string">
    <value value="contoso"/>
  </param>
  <param name="dataService" parameterType="IMyService">
    <dependency />
  </param>
  <param name="myGenericType" genericParameterName="T1">
    <array />
  </param>
</method>

The preceding configuration corresponds to a method named Initialize that takes three parameters, as shown here.

public void Initialize(string connectionString, IMyService dataService, T1[] mygenericType)
{ 
  ... 
}
'Usage
Public Sub Initialize(connectionString As String, dataService As IMyService, T1() mygenericType)
  ... 
End Sub

The configuration settings specify that the value of the connectionString parameter will be "AdventureWorks" and that the value of the dataService parameter will be the result of any mapping for the IMyService interface stored in the container (effectively a call to the Resolve method for the class IMyService).

Attributes and Child Elements

The following table lists the attributes for the method element.

Attribute

Description

name

The name of the method to apply dependency injection to. This attribute is required.

key

This attribute is used when you have multiple overloads of an injection method with the same name or when you want to call the same injection method multiple times. Every key attribute's actual value can be anything but it must be unique.

The method element has one or more child elements named param. For more information, see param Element.

param Element

The param element specifies a parameter for constructor or method injection.

<!-- using a specific value for the parameter -->
<param name="connectionString" parameterType="string">
  <value value="contoso"/>
</param>

<!-- using a specific value and type converter for that value -->
<param name="myObject" parameterType="IMyObject">
  <value type="MyRealObjectInstance" typeConverter="MyTypeConverter" />
</param>

<!-- using a default dependency through the container -->
<param name="dataService" parameterType="IMyService">
  <dependency />
</param>

<!-- using a specific named (non-default) dependency -->
<param name="dataService" parameterType="IMyService">
  <dependency name="MyMappingName" />
</param>

<!-- using a generic parameter name -->
<param name="myGenericType" genericParameterName="T1">

</param>Attributes and Child Elements

The following table lists the attributes for the param element.

Attribute

Description

name

The name of the parameter. This attribute is required.

genericParameterName

The name of a generic type parameter in the generic type for which injection is configured. You cannot set both propertyType and genericParameterName. If both attributes are set, a ConfigurationErrorException error is raised when the configuration is read from a file.

parameterType

The data type of this parameter. Can be a user-defined alias specified in the typeAliases section of the configuration. You cannot set both propertyType and genericParameterName. This attribute is required.

For more information about usage, see the property Element.

For more information about the child elements, see value Element, dependency Element, and array Element.

value Element

The value element specifies a type to be read and applied to a property or parameter. These are not resolved types. The value can be one of the standard .NET Framework data types such as String or Integer, and Unity will use the corresponding built-in .NET Framework type converter to evaluate the value. Alternatively, you can use a custom object type and provide a reference to a type converter for that type.

Note

For configuration files, there is an additional step required to get the value from a "serialized" string in the configuration file.

Attributes

The following table lists the attributes for the value element.

Attribute

Description

type

The type of the value. Can be a user-defined alias specified in the typeAliases section of the configuration, or one of the standard .NET Framework data types. This attribute is optional. If you do not specify a type, the value is assumed to be of type String.

value

A string representation of the value for this element. This attribute is required.

typeConverter

The type converter to use to convert the value provided to match the type of the instance. If not specified, the default converter for the specified type is used. This attribute is optional.

The value element has no child elements.

dependency Element

The dependency element specifies that Unity should resolve the value for this parameter or property using the types and mappings registered in the container. If you do not specify a type, it uses the type defined for the parameter or property. If you do not specify the name of a mapping or registration, it uses the default (un-named) mapping or registration.

If the param or property element was configured with a genericParameterName, the <dependency/> element cannot have a type, and whatever the actual type for the parent element's generic type parameter is in the closed type being resolved will be used to resolve the dependency.

For more information about using genericParameterName and dependency elements with arrays, see Configuration Support for Generics and array Element.

Attributes

The following table lists the attributes for the dependency element.

Attribute

Description

name

The name of the named type or mapping registered in the container to use to resolve this dependency. This attribute is optional.

type

The type to use to resolve a dependency mapping or registration. This attribute is optional. If you do not specify a type, Unity resolves the type based on the type of the parent parameter or property.

The dependency element has no child elements.

array Element

The array element specifies that an array containing the registered instances of a type parameter should be resolved.The array element has the following child elements:

  • array
  • dependency
  • value

You can nest arrays so the array can be an array of arrays.

Use the dependency element when you want only specific member instances. For more information, see dependency Element. The following XML example resolves the array for two members, "logger2" and "logger1".

Use the value element to specify a value, such as one of the standard .NET Framework data types to apply to a property or parameter. For more information, see value Element.

      <container name="AContainer">
        <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="ILogger" mapTo="SpecialLogger" name="logger3">
            <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>

The array element without the dependency child will be resolved for all members of the array.

                <param name="loggers" parameterType="ILoggerArray">
                  <array />
                </param>

instances Element

The instances element holds a collection of the existing object instances for this container. These are objects registered with the container using the RegisterInstance method. You can register anything with RegisterInstance. Instances added through configuration tend to be simple because it is hard to represent them with a string value, but they could be arbitrarily complex if there is a type converter that can handle the conversion from a string. The instances element contains a series of add elements that add individual instances.

<instances>
  <add name="MyInstance1" type="System.String" value="Some value" />
  <add name="MyInstance2" type="System.DateTime" value="2008-02-05T17:50:00" />
</instances>

Child Elements

The only child element of the instances element is a collection of the add element for instances.

add Element for Instances

The add element for instances defines an instance mapping to insert into the Unity container.

Attributes

The following table lists the attributes for the add element for instances.

Attribute

Description

name

The name to use when registering this instance. This attribute is optional.

type

The type of this instance. This attribute is optional. Can be a user-defined alias specified in the typeAliases section of the configuration. If omitted, the assumed type is System.String.

value

A string representation of the desired instance that is used to convert from one instance to another. This attribute is required.

typeConverter

The type converter to use to convert the value provided to match the type of the instance. If not specified, the default converter for the specified type is used. This attribute is optional.

extensions Element

The extensions element holds the list of extensions to register with the Unity container. The extensions element contains a series of add elements that add individual extensions.

<extensions>
  <add type="MyApp.MyExtensions.SpecialOne" />
</extensions>

Child Elements

The only child element of the extensions element is a collection of the add element for extensions.

add Element for Extensions

The add element for extensions defines an extension to register with the Unity container.

Attributes

The following table lists the attribute for the add element for extensions.

Attribute

Description

type

The type of extension to add to the container. Can be a user-defined alias specified in the typeAliases section of the configuration. This attribute is required.

extensionConfig Element

The extensionConfig element holds a collection of the extension configuration elements for extensions you add to the container. The extensionConfig element contains a series of add elements that add individual types.

<extensionConfig>
  <add name="MyExtensionConfigHandler"
       type="MyApp.MyExtensions.SpecialOne.ConfigHandler" />
</extensionConfig>

Child Elements

The only child element of the extensionConfig element is a collection of the add element for extension configurations.

add Element for Extension Configurations

The add element for extension configurations contains the name and the type name of a custom class (or the root class of a series of custom classes) that can read the configuration information contained in the add element. For more information about creating custom container extensions and the configuration mechanism, see Creating Container Extensions.

Attributes

The following table lists the attributes for the add element for types.

Attribute

Description

name

The name to use when registering this extension configuration. This attribute is required.

type

The source type of the extension configuration. Must be a fully qualified type name. The extension must contain a class that can read the contents of the extension configuration. This attribute is required.

interceptors Element

The <interceptors> element encloses the list of interceptors in the configuration and contains a series of <interceptor> elements that specify the individual interceptors.

interceptor Element

Each <interceptor> element within the <interceptors> element specifies the configuration for a single interceptor. The <interceptor> element has the following child elements:

  • key
  • default

You define interceptors in the same way you define lifetime managers.

Attributes for the interceptor Element

The following table lists the attributes for the interceptor element for types.

Attribute

Description

name

The name to use when registering this interceptor. This attribute is required only when there are two or more entries for the same interceptor type.

type

The type of the interceptor. This attribute is required.

typeConverter

The type converter to use to convert the value provided to match the type of the instance. If not specified, the default converter for the specified type is used. Aliases are allowed. This attribute is optional.

Value

Any value required to initialize the interceptor. This attribute is optional

default Element

The <default> child element sets the default interceptor for a type. It has only the type attribute. The type attribute specifies the type of the interceptor.

key Element

The <key> child element for <interceptor> is used to set the interceptor for a specific build key. The name attribute is is used if the target key has a name.

Attributes for the Key Element

The following table lists the attributes for the key element.

Attribute

Description

name

The name to use when registering the build key. This attribute is optional.

type

The type for the build key. The type of the object to map from if this is a mapping registration or the type of the object if this is a singleton registration. Can be a user-defined alias specified in the typeAliases section of the configuration. This attribute is required.

policies Element

The <policies> element encloses the list of policies in the configuration and contains a series of <policy> elements that specify the individual policies.

The following XML example shows the policies element and its children.

            <policies>
              <policy name="foot">
                <matchingRules>
                  <matchingRule name="rule1">
                    <lifetime type="singleton" />
                  </matchingRule>
                </matchingRules>
                <callHandlers>
                  <callHandler name="handler1" type="GlobalCountCallHandler">
                    <lifetime type="singleton" />
                  </callHandler>
                  <callHandler name="handler2" type="GlobalCountCallHandler">
                  </callHandler>
                </callHandlers>
              </policy>
            </policies>

policy Child Element of the policies Element

Each <policy> element within the <policies> element specifies the complete configuration for a single policy. You are not required to specify policies here. Policies can be specified in the general-purpose injection section.

policy Element

Each <policy> element within the <policies> element specifies the complete configuration for a single policy through two child elements named <matchingRules> and <callHandlers>.

Attributes for the policy Element

The following table lists the attributes for the policy element for policies.

Attribute

Description

name

The name to use when registering this policy. This attribute is required.

type

The type of the policy. Can be a user-defined alias specified in the typeAliases section of the configuration. The extension must contain a class that can read the contents of the policy configuration. This attribute is required.

matchingRules Element

The <matchingRules> element encloses the list of matching rules for a policy and contains a series of <matchingRule> elements that specify the individual matching rules. The combination of matching rules within a <matchingRules> element must all evaluate to True for a policy to apply.

matchingRule Element

Each <matchingRule> element within the <matchingRules> element specifies details of a single matching rule. The <matchingRule> element has the <injection> and <lifetime> child elements.

Attributes of the matchingRule Element for Matching Rules

The following table describes the attributes of each <matchingRule> element that can occur within the <matchingRules> element to describe a matching rule.

Attribute

Description

name

The name by which code in the application block and the configuration tools will refer to this matching rule.

type

The name of the matching rule class, the name of the containing assembly, the version, the culture information, and the public key token.

Child Elements for the matchingRule Element

The <matchingRule> element has the following optional child elements:

  • injection
  • lifetime

The <lifetime> element specifies the lifetime behavior of the object. It has one attribute, type, which specifies the lifetime manager. For imformation about lifetime management, see Using Lifetime Managers.

The following example XML shows the matchingRule element with its child element.

                <matchingRules>
                  <matchingRule name="rule1">
                    <lifetime type="singleton" />
                  </matchingRule>
                </matchingRules>

injection Element

The <injection> element describes how the specified element, in this case the <matchingRule> element, is created. It is much like the <typeConfig> element, except the <injection> element configures injection of an instance, whereas typConfig is an extension point. The <injection> element has the same child elements as the default for <typeConfig>.

callHandlers Element

The <callHandlers> element encloses the list of call handlers for a policy and contains a series of <callHandler> elements that specify the individual call handlers.

callHandler Element

Each <callHandler> element within the <callHandlers> element specifies details of a single call handler. The callHandler element has the <injection> and <lifetime> child elements.

Attributes of the callHandler Element for callHandlers

The following table describes the attributes of the <callHandler> element.

Attribute

Description

name

The name by which code in the application block and the configuration tools will refer to this call handler.

type

The type of the callhandler. Can be a user-defined alias specified in the typeAliases section of the configuration..

Child Elements for the callHandler Element

The <callHandler> element has the following child elements:

  • injection
  • lifetime

The following example XML shows the callHandler element with its injection child element. This example injects a property.

                <callHandlers>
                  <callHandler name="handler1" type="GlobalCountCallHandler">
                    <injection>
                      <constructor>
                        <param name="name" parameterType="string">
                          <value value="handler1" />
                        </param>
                      </constructor>
                      <--sets the position of the callhandlers 
                      <-- within the policy handler chain/>
                      <property name="Order" propertyType="int">
                        <value value="10" type="int"/>
                      </property>
                    </injection>
                  </callHandler>
                </callHandlers>

The Unity Application Block is designed to support a range of common scenarios for resolving instances of objects that, themselves, depend on other objects or services. When adding your application code, refer to the scenarios in the Key Scenarios sections and select the ones that best suit your situation. Use the code that accompanies the scenario either as-is or adapt it as necessary.

First, prepare your application to use the Unity Application Block. The following procedure describes how to include the necessary assemblies and elements in your code.

To prepare your application

  1. Add a reference to the Unity Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.Unity.dll assembly. Select the assembly, and then click OK to add the reference.

  2. Use the same procedure to set a reference to the ObjectBuilder assembly, named Microsoft.Practices.ObjectBuilder2.dll.

  3. (Optional) To use elements from the Unity Application Block without fully qualifying the element reference, add the following using statements (C#) or Imports statements (Visual Basic) to the top of your source code file as required.

    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    using Microsoft.Practices.Unity.StaticFactory;
    
    'Usage
    Imports Microsoft.Practices.Unity
    Imports Microsoft.Practices.Unity.Configuration
    Imports Microsoft.Practices.Unity.StaticFactory
    
  4. Add your application code. For more information about how you can use the Unity Application Block in your own applications, see Key Scenarios.

Note

For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click Properties on the Project menu. When the Project Designer appears, click the References tab.