Thursday, December 24, 2009

Unity Application Block – Understanding configuration file schema


The Unity Application Block can read configuration information from an XML configuration file. By default, this is the App.config or Web.config file for your application. However, you can load configuration information from any other XML format file or from other sources. In this post we’ll see the list of elements and attributes used to configure the Unity Application Block and how each section is used to meet your application requirements.
The configuration file that belongs to the application contains multiple configuration sections. E.g. Sections for data access configuration information, Exception handling and logging information, Application settings etc. The unity configuration information is defined in the unity configuration section, which will be specified under element of your application configuration file.
<configuration>
  <configSections>
    <section name="unity"
             type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                   Microsoft.Practices.Unit.Configuration,
                   Version=1.2.0.0,
                   Culture=neutral,
                   PublicKeyToken=31bf3856ad364e35" />
  configSections>
configuration>
The section handler declaration above specifies the name of the unity configuration section as unity. You can use any name as your convention. The samples in this articles series will use the name as unity. As mentioned before the unity element specifies the application block configuration information. The unity element has the following child elements.
·         typeAliases
·         containers
typeAliases Element
This element holds a collection of aliases that you can use to specify the types required in mappings, instances etc. for unity containers. The typeAliases element contains the typeAlias child elements, which have alias and type attributes that can be referred to the type elsewhere in the configuration.
<unity>
  <typeAliases>  
    <typeAlias alias="myTypeAlias" type="MyNamespace.TypeName, MyAssembly" />
  typeAliases>
unity>
containers Element
This element holds details of an individual container. The container element has the following child elements.
·         types
·         instances
·         extensions
·         extensionConfig
<containers>
  <container name="containerOne">
    <types>
     
    types>
    <instances>
     
    instances>
    <extensions>
     
    extensions>
    <extensionConfig>
     
    extensionConfig>
  container>
containers>
types Element
This element holds a collection of the registered type mappings for the container. This element holds the type child element that defines a type mapping for the unity container.
<types>
  <type type="Blogsprajeesh.Blogspot.Infrastructure.IRepository"
        mapTo="BlogsPrajeesh.Blogspot.DataAccess.SQLRepository"
        name="Repository">
  type>
types>
You can also specify the lifetime manager for each mapping. The default value will be a transient lifetime manager which creates a new instance of the registered, mapped or requested type each time a call to resolve the type is made. You can specify a lifetime to be singleton, per thread or external.
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<types>
  <type type="Blogsprajeesh.Blogspot.Infrastructure.IRepository"
        mapTo="BlogsPrajeesh.Blogspot.DataAccess.SQLRepository"
        name="Repository">
    <lifetime type="singleton" />
  type>
types>

The typeConfig element of the type child element specifies the general configuration for a type/ name configuration. You can use this element to specify or configure custom dependency injection settings for the container.
<types>
  <type type="Blogsprajeesh.Blogspot.Infrastructure.IRepository"
        mapTo="BlogsPrajeesh.Blogspot.DataAccess.SQLRepository"
        name="Repository">
    <lifetime type="singleton" />
    <typeConfig>
      <constructor>
        <param name="connectionString" parameterType="string">
          <value value="MyConnectionString" />
        param>
      constructor>
    typeConfig>
  type>
types>
instances Element
This element holds a collection of the existing object instances for the container.
<instances>
  <add name="MyConnectionString" type="System.String" value="connection string value" />
instances>
Next part of the series we’ll see how to load the configuration information setup in the config file into the container and working with that information.

No comments: