[Unity] DI with Unity

Unity” is another framework for DI and some more. It is part of MS Enterprise Library but can be installed separately through NuGet. The main purposes of “Unity” are late binding and loose coupling.

 

1. Installing Unity

Installing is pretty easy with NuGet. Search “Unity” and install it. You can find the “Unity.*” assemblies.

 

2. Factory Patterns vs. DI

Suppose your class A needs an instance of class B.

  • Factory: the “pull” model; class A is trying to get an instance of B using the factory class or method.
  • DI: the “push” model, another class C is responsible to create an instance of B and injects (pushes) it to A

[note] In Unity, the “UnityContainer” class plays a role of this DI controller.

 

3. Unity DI Lifecycle

  • Register: UnityContainer.RegisterType<from, to>()
  • Resolve: UnityContainer.Resolve<requestedType>()
  • Dispose:
// register var container = new UnityContainer();
container.RegisterType<IFrom, ConcreteTo>();

// resolve
// create an concrete object of "IFrom" type
// and injects it to "RequestedType"
var requestedInstance =  container.Resolve<RequestedType>();

 

4. DI Pattern 1 – Constructor

interface IAnimal
{
  void Cry();
}

class Dog : IAnimal
{
  public void Cry()
  {
    Console.WriteLine("Bark!" 16="Bark!");
  }
}

class Zoo
{
  IAnimal animal;

  [InjectionConstructor"]
  public Zoo(IAnimal a)
  {
    animal = a;
  }

  public void MakeCry()
  {
    animal.Cry();
  }
}

Unity” injects the instance of “IAnimal” to the constructor of the Zoo class. Note the [InjectionConstructor] attribute.

static void Main(string[] args)
{
  var container = new UnityContainer();
  container.RegisterType<IAnimal, Dog>();
  Zoo zoo = container.Resolve<Zoo>();

  zoo.MakeCry(); // "Bark! Bark!"
}

 

5. DI Pattern 2 – Method

Another DI pattern is a method injection.

class Zoo
{
  IAnimal animal;

  [InjectionMethod]
  public void SetAnimal(IAnimal a)
  {
    animal = a;
  }

  public void MakeCry()
  {
    animal.Cry();
  }
}

static void Main(string[] args)
{
  var container = new UnityContainer();
  container.RegisterType<IAnimal, Dog>();
  Zoo zoo = container.Resolve<Zoo>();

  zoo.MakeCry(); // "Bark! Bark!"
}

At first, the Setxxx() method are annotated by [InjectionMethod]. Also note that there is no change in the Unity code. Unity framework smartly figures out how to inject dependencies.

 

6. DI Pattern 3 – Setter

class Zoo
{
  [Dependency]
  public IAnimal Animal { get; set; }

  public void MakeCry()
  {
    Animal.Cry();
  }
}

static void Main(string[] args)
{
  var container = new UnityContainer();
  container.RegisterType<IAnimal, Dog>();
  Zoo zoo = container.Resolve<Zoo>();

  zoo.MakeCry(); // "Bark! Bark!"
}

Using properties is a common pattern. Unity works with setters as well. Make sure you need to decorate the property with the [Dependency] attribute.

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 )

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