Microsoft's Unity Application Block (Unity for short) is an Inversion of Control framework wich is part of the Microsoft Enterprise Library 5.0.

Unity is a general-purpose container for use in any type of Microsoft® .NET Framework-based application. It provides all of the features commonly found in dependency injection mechanisms, including methods to register type mappings and object instances, resolve objects, manage object lifetimes, and inject dependent objects into the parameters of constructors and methods and as the value of properties of objects it resolves.

The first step for using Unity is to configure the container with the required aliases, type registrations, mappings, and other information that it requires in order to resolve objects at run time and inject the appropriate objects and values into dependent objects.

The container can be configured by the application configuration file (or other xml file) or at run-time.

In the example below is shown how to configure the container at run-time or using the application configuration file. To run the example you must add a reference to the Microsoft.Practices.Unity and the Microsoft.Practices.Unity.Configuration assemblies.

 

Code sample

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;   
using Microsoft.Practices.Unity.Configuration;     

namespace UnityDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // create the unity container
            IUnityContainer container;
          
            //----------------------------------------------------------
            // Contatiner configured at run-time
            //----------------------------------------------------------
            Console.WriteLine("Container configured at run-time:");
            Console.WriteLine("---------------------------------");
            container = new UnityContainer();
            container.RegisterType(typeof(IVehicle), typeof(Jeep), "R1");
            container.RegisterType(typeof(IVehicle), typeof(Car), "R2");
            container.RegisterType(typeof(IVehicle), typeof(Moto), "R3");

            // resolve the object by type
            IVehicle myVehicle = container.Resolve<Jeep>();
            Console.WriteLine(myVehicle.Model);

            // resolve the object by reference name
            myVehicle = container.Resolve<IVehicle>("R3");
            Console.WriteLine(myVehicle.Model);

            Console.WriteLine();

            //----------------------------------------------------------
            // Contatiner configured by app.config
            //----------------------------------------------------------
            Console.WriteLine("Container configured by app.config:");
            Console.WriteLine("-------------------------------------");
            container = new UnityContainer();
            container.LoadConfiguration();

            // resolve the object by type
            myVehicle = container.Resolve<Car>();
            Console.WriteLine(myVehicle.Model);

            // resolve the object by reference name
            myVehicle = container.Resolve<IVehicle>("R2");
            Console.WriteLine(myVehicle.Model);

            Console.WriteLine();

            Console.WriteLine("Press [ENTER] to quit.");
            Console.ReadLine();
        }
    }


    /// <summary>
    /// IVeicle interface
    /// </summary>
    public interface IVehicle
    {
        string Model { get; }
        int WheelsDrive { get; }
        int Wheels { get; }
    }

    /// <summary>
    /// Car implementation
    /// </summary>
    public class Car : IVehicle
    {
        int IVehicle.WheelsDrive
        {
            get { return 2; }
        }

        int IVehicle.Wheels
        {
            get { return 4; }
        }

        string IVehicle.Model
        {
            get { return "Car 2WD"; }
        }
    }

    /// <summary>
    /// Jeep implementation
    /// </summary>
    public class Jeep : IVehicle
    {
        int IVehicle.WheelsDrive
        {
            get { return 4; }
        }

        int IVehicle.Wheels
        {
            get { return 4; }
        }

        string IVehicle.Model
        {
            get { return "Jeep 4WD"; }
        }
    }

    /// <summary>
    /// Moto implementation
    /// </summary>
    public class Moto : IVehicle
    {
        int IVehicle.WheelsDrive
        {
            get { return 1; }
        }

        int IVehicle.Wheels
        {
            get { return 2; }
        }

        string IVehicle.Model
        {
            get { return "Moto 600cc"; }
        }

    }
}


App.Config

 

<?xml version="1.0"?>
<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="IVehicle" type="UnityDemo.IVehicle, UnityDemo"/>
    <alias alias="Car" type="UnityDemo.Car, UnityDemo"/>
    <alias alias="Jeep" type="UnityDemo.Jeep, UnityDemo"/>
    <alias alias="Moto" type="UnityDemo.Moto, UnityDemo"/>
   
    <container>
      <register type="IVehicle" mapTo="Car" name="R1"/>
      <register type="IVehicle" mapTo="Jeep" name="R2"/>
      <register type="IVehicle" mapTo="Moto" name="R3"/>
    </container>

  </unity>
</configuration>