Microsoft Unity2 Block and Generics into Configuration File


Hi,

I saw a question in stackoverflow about using Microsoft Unity Block to resolve generic types. At first I was surprised because I didn’t know Unity could specify Generics into the configuration file. So, after some research I realize some facts, that I’d like to share with you.

Microsoft Enterprise Libraries 5 is using Unity. The developers were planning to include a editor for the Unity Block, which was available during the beta, but it was removed from the final version. This editor is not the final, but it’s good enough to help you to write your mappings.
Sample of the Unity config file
Now we’re ready to start solving our problem of how to use Unity to resolve interfaces (or contracts) which are using generics. All this by using a configuration file. Lets take a look to the configuration file:

<pre><code><?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="StringListContract" type="System.Collections.Generic.IList`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <alias alias="ConcreteStringList" type="System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <container>
      <register type="StringListContract" mapTo="ConcreteStringList">
        <lifetime type="ContainerControlledLifetimeManager" />
        <constructor />
      </register>
    </container>
  </unity>
</configuration></code></pre>

As you can see, configuration is simple. A quick note is that when it’s registered the type resolution IList => List. This resolution specifies a default default constructor. This is because of Unity block cannot determine the best constructor for the List. Additionally this sample is using signed dlls, so the references of IList, List and String are signed.

The last part of this sample, is consuming this configuration file.

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace UnityTerminal
{


    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            container.LoadConfiguration();
            {
                var concreteStringList = container.Resolve<System.Collections.Generic.IList<System.String>>();
                concreteStringList.Add("Hello World");
            }
            {
                var concreteStringList = container.Resolve<System.Collections.Generic.IList<System.String>>();
                Console.WriteLine("{0}", concreteStringList[0]);
            }
            Console.ReadKey();
        }
    }
}</code></pre>

So, by doing this the first call to resolve will retrieve resolve the type based on the configuration file, the second call is used to verify the implemented behavior by printing the previous stored value.

Best regards,
Herber

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s