Microsoft Unity Framework - A Peek

In this article I will introduce you to the Microsoft Unity Framework and also take you through its dependency injection feature with the help of few sample C# code snippets.

Unity is one among the Microsoft Application Blocks. It is basically introduced as an IOC container by Microsoft, which helps in easy object creation and de-coupling the module dependencies in your project. To use it, just add the reference of the Unity DLLs to your project. The Unity application block can be downloaded from here. It can be useful in your applications to achieve the following.

1. Dependency Injection– Decouples the module dependencies in your application by creating and injecting the object at runtime.

2. Aspect Oriented Programming– Though Unity is primarily an IOC container, it also exposes a feature called interception, which allows the developers to tie the cross cutting concerns to method calls without modifying the actual target object.

In the following section let us look at C# sample codes for Dependency Injection and Interception using Unity.

Dependency Injection using Unity

The dependency of a class can be injected at runtime using the Dependency Injection mechanism. It is a two-step process. Register the dependency and then resolve the concrete business module class. RegisterType and RegisterInstance methods of the UnityContainer classes can be used to perform the Register operation and Resolve method can be used for creating the concrete business class instance.

There are three ways of injecting the dependency to a class or module, they are.

The mapping of abstraction and the concrete implementation of the dependency can be done through either configuration file entries or through code directly. The former approach will be a better one as it will not require any kind of code change.

Sample Code

Let us create a sample application and do dependency constructor injection using Unity. Create a Console application and download the Unity application block. Now add reference to the following Unity DLLs to the Console project.

1. Microsoft.Practices.Unity.dll

2. Microsoft.Practices.Unity.Configuration.dll – This one is needed since we go by the config file approach.

Create the class OrderController, which has a dependency to the DataAccess object instance (either be SQL or Oracle). OrderController is the business module concrete class and below is the code.

In the Program.cs add the following code to register the dependency and resolve the OrderController instance as shown below. This will automatically take care of injecting the DataAccess object to the OrderController constructor.

Interception using Unity

Interception in Unity is a feature that can intercept a target object call and execute a series of behaviors before or after the call. It is a feature that enables the Aspect Oriented Programming paradigm in C#. Mostly the cross-cutting concerns like logging, input validation, etc.

I will do a deep dive into the Interception feature of Unity in a future article since I feel this will also be a very useful feature for the C# developers to learn.