Home IT Info News Today IDG Contributor Network: Working with the Unity Application Bloc…

IDG Contributor Network: Working with the Unity Application Bloc…

263

Similar to Castle Windsor and StructureMap, Unity Application Block is also an IoC (Inversion of Control) container. The Unity Application Block from Microsoft is a light weight extensible dependency injection container. It provides support for constructor injection, property injection and also method call injection. Incidentally, the Unity Application Block was introduced as part of the Enterprise Library.

In case you are not familiar with what Dependency Injection and Inversion of Control is all about, here’s a quick explanation. Dependency Injection is a realization of the IoC principle. Both inversion of control and dependency injection are ways that enable you to break the dependencies between the components in your application. The Dependency Injection principle states that the high level modules in an application should not depend on the low level modules; rather, both should depend on abstractions.

Unity Application Block design goals

The Unity Application Block is a Dependency Injection (DI) container. Note that the Unity Application Block doesn’t have any dependency on the Enterprise Library configuration system. Hence, you can use it as a stand – alone dependency injection container sans the Enterprise Library being installed in your system. The design goals of the Unity Application Block include the following:

  1. Promoting a modular design through decoupling
  2. Providing a fast, extensible, light weight dependency injection container
  3. Provide support for extensibility through extensions
  4. Provide support for attribute driven injection
  5. Provide support for an intuitive API to connect to and work with the dependency injection container

Getting started

In this section we will explore on how we can get started using the Unity Application Block in our applications. The first step should be to get the Unity Application Block installed in your system. The easiest way to install this library is through NuGet. For the purposes of this illustration we will use a console application project here. To build the first application using the Unity Application Block, follow these steps:

  1. Open Visual Studio IDE
  2. Create a Console Project and save it with a name
  3. Right click on the project in the Solution Explorer Window
  4. Select “Manage NuGet Packages…”
  5. Install Unity NuGet Package Manager

That’s all you would need to do to set the stage for you to start using Unity. You are now ready to use Unity Application Block in your project.

Creating and resolving object dependencies using Unity container

You can use the Unity container to resolve dependencies on a particular object easily as shown in the code snippet that follows.

IUnityContainer container = new UnityContainer();

container.RegisterType<IRespository, ProductRepository>();

container.RegisterType<ILogger, DatabaseLogger>();

When you register the type of an object with the Unity container, you can specify the lifetime. If you don’t specify any, the default lifetime is used. A lifetime manager controls the lifetime of a registered object. The types of the lifetime managers supported by the Unity Application Block include: TransientLifetimeManager, ContainerControlledLifetimeManager, HierarchicalLifetimeManager, PerThreadLifetimeManager and ExternallyControlledLifetimeManager.

Consider the following interface called ILogger.

public interface ILogger

   {

       string GetLogTypeName();

   }

The ILogger interface contains the declaration of one method named, GetLogTypeName(). The FileLoger, DatabaseLogger and EventLogger classes (given below) implement the ILogger interface.

public class FileLogger : ILogger

   {

       public string GetLogTypeName()

       {

           return "File Logger";

       }

   }

   public class DatabaseLogger: ILogger

   {

       public string GetLogTypeName()

       {

           return "Database Logger";

       }

   }

   public class EventLogger: ILogger

   {

       public string GetLogTypeName()

       {

           return "Event Logger";

       }

   }

The following code listing shows how you can resolve dependencies using UnityContainer.

static void Main(string[] args)

{

IUnityContainer container = new UnityContainer();

container.RegisterType<ILogger, FileLogger>();

ILogger iLogger = container.Resolve();

string logType = iLogger.GetLogTypeName();

Console.WriteLine(logType);

Console.Read();

}

Note that the “Container” in Unity Application Block is the object that can be used to create and inject dependencies. You can register types or type mappings with the Unity container using the RegisterType method. The Resolve<T>() method is used to return a concrete instance of the type that is registered for the generic type mentioned using T. In the code example given above, the Resolve() method will return an instance of the FileLogger class.

An alternative approach to specify the Unity integration is through configuration. Assuming that you have specified a container named IDGContainer in your Unity configuration, the following code snippet illustrates how you can call the LoadConfiguration method on the container instance in your code.

string containerName = "IDGContainer";

IUnityContainer container = new UnityContainer().LoadConfiguration(containerName);

This article is published as part of the IDG Contributor Network. Want to Join?

Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here