Microsoft Unity2 Block and Generics into Configuration File


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" ?>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  <unity xmlns="">
    <alias alias="StringListContract" type="System.Collections.Generic.IList`1[[System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <alias alias="ConcreteStringList" type="System.Collections.Generic.List`1[[System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <register type="StringListContract" mapTo="ConcreteStringList">
        <lifetime type="ContainerControlledLifetimeManager" />
        <constructor />

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();
                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]);

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,

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.