Unity Application Block - Dependency Injection Container
Read: 4384 times    -     Avarage Rate: 4





2010 03 May

After exploring the Dependency Injection and the problem that it solves, we need to implement the Dependency Injection Container in order to be able to use the design pattern.

There are a wide variety of DI containers (Castle Windsor, Spring.NET, StructureMap, Unity, Autofac, and Ninject) and all work fine and provide good number of features, so the decision will not be based on technical criteria, I personally, may make my choice based on some of the following reasons:

  • You prefer one fluent interface or XML schema over another.
  • You respect another developer who uses or develops a particular framework.
  • You are a big fan of a certain 3rd party library that has wonderful integration with a particular IoC Container.
  • Your client is insistent on a particular dependency injection framework.

As you can see clearly I am a fan of Microsoft technologies and I prefer using them over all other technology providers, Patterns and Practices is my second home.

Microsoft Enterprise Library provides Unity Application Block (Unity) a lightweight extensible dependency injection container with support for constructor, property, and method call injection. Unity is the next version of ObjectBuilder which was part of the Enterprise Library 3.1.

Unity DI container will be my choice and it’s yours too as you are still reading this post, so let’s attack the concept and see some codes.

Unity has no dependency on Enterprise Library core and can be used without having to install Enterprise Library on the host system. To use Unity in your application you need to add reference to some DLLs only.

There are key scenarios to use the Unity container:

  • Setting up the Unity Container: Before Unity can supply your application types at runtime, you have to tell Unity WHAT you want it to supply and where to find it.

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<IMyService, CustomerService>();

  • Resolving an Object by Type:  How you can retrieve concrete instances of registered object from a Unity container Register Types?

IMyService result = myContainer.Resolve<IMyService>();

  • Resolving All Objects of a Particular Type: ResolveAll

IEnumerable<IMyObject> objects = myContainer.ResolveAll<IMyObject>();

  • Configuring and Using Matching Rules:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity"
             type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
                   Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity>
    <containers>
      <container>
        <types>
          <type type="UnitySample.ILogger,UnitySample"
                mapTo="UnitySample.ConsoleLogger,UnitySample" />
        </types>
      </container>
    </containers>
  </unity>
</configuration>

In order to show more the details please check out my Dependency Injection Presentation that I conducted last week for ArabiaGIS and ACT technical teams.

caio

Note that this post will be edited simultaneously to make it clearer and helpful.

Comments