2015-05-04

The post Dependency Injection using Unity container appeared first on codecompiled .

Dependency injection is one way to implement inversion of control.To understand dependency injection please refer Dependency Injection in .NET

Dependency injection injects the dependencies of a class at runtime.This creates a lose coupling

between the classes as changes in one class does not impact the other dependent classes
DI Containers:  to automatically inject dependencies we use a Dependency Injection(DI) container.We can also inject the dependencies manually but using a DI container provides the following benefits

Automatic Dependency Resolution When dependendencies are managed by the container there are less chances of error.Suppose if our application has a lot of dependencies then injecting those dependencies is also difficult to manage if we are injecting them without a DI container.

Decouples the client from the dependency If the client is directly injecting the dependency then client code is aware of the class dependencies.This tight coupling can be a problem if tomorrow the dependencies of the class changes.

Suppose if a X has a dependency on Y then without the container it is the responsibility of the client to create and inject the instance of class Y.

There are few popular .NET dependency injection containers such as:

Castle Windsor

StructureMap

Autofac

Unity

Ninject

In the following example we will be using Unity to manage the dependencies.It has a simple API and is also easy to configure.

We have the following Employee class which takes IDBAccess as a constructor dependency.To create loosely coupled architecture using dependency injection we use an interface

which removes the direct dependencies between the classes.So we use IDBAccess parameter in the Employee class constructor

Adding Unity in our application

To implement the dependency injection using the Unity DI container we add the Nuget package for Unity which adds the required references to the project.



Adding the above Nuget package adds the following references to the project:



Following references are added to the project

Microsoft.Practices.Unity and Microsoft.Practices.Unity.RegistrationByConvention   Main assemblies which implements Dependency Injection functioanlity
Microsoft.Practices.Unity.Configuration We can register the dependencies in a xml file and also in the code.This assembly is useful when we register the dependencies in xml file.

Using the Unity container

We register the objects in container and retrieve the objects from the container,so it is the container with which our client directly interacts.

There are two main steps to use Unity DI container in our application.

Register the dependencies   We register dependencies using the RegisterType method.It is a generic method in which provide the mapping between the interface type or the abstract class and the concrete type which needs to be instantiated when an object of the interface type is requested by the client.First we instantiate the container.Once we have the container object we use the ResgisterType<Interface,ConcreteType>() method to add the mapping between the interface and the Concrete types.

The above dependency adds a mapping between the IDBAccess and SQLDataAccess types.This means that whenever a dependency of type IDBAccess is required an instance of SQLDataAccess is created and is injected into the dependent type.

Resolve the dependencies  To create the object which takes a dependency using the constructor or property injection we use the resolve method.When we use the resolve method the necessary dependencies are automatically injected.So we are not concerned with providing the dependencies ourselves.We can get an object of Employee class using the Resolve() method as:

The above call to the Resolve() method will automatically inject the required dependencies in the Employee class.Since the Employee class depends on the IDBAccess interface and we have registered the mapping between the IDBAccess interface and the SQLDataAccess class ,an object of SQLDataAccess is automatically created by the container and is passed to the Employee class constructor.

Property Injection

To implement property injection we need to apply the [Dependency] attribute to the property in the class.

Suppose we have a class employee which exposes a property PersonalDetails then we can use the property injection as:

Now when we create the Employee object using the Unity container,the PersonalDetails object is automatically created and is assigned to the PersonalDetails property.

It is important to keep all the type registrations together .If we keep the type registrations together then we can manage the registrations from a single location in our code.We add the registrations to the Unity container at the application startup so that the types are available later on in the application life cycle.In the case of web applications this means registering the types in the container in the global.asax while for console applications we can register the dependencies in the Main() method.

Registering the dependencies in configuration file

Though registering the types with the container in the code is better approach since it allows to easily catch the errors.But it also means recompiling the code.Another approach for registering the types in the Unity container is using the configuration file.If we use the configuration file then we can easily change the registrations without recompiling the code.

Following is a sample XML configuration file which registers the dependencies in the unity container.We are registering the same types which we have registered in the code above.

We can configure the Unity container as:

By implementing Dependency Injection using Unity container we can easily register the dependencies ,in our application, in the Unity container at application startup.We can ask the Unity container to create objects for us and it will automatically resolve the dependencies.This is more useful when we have a complex application in which it is difficult for us to manage the dependencies between the types ourselves.

The post Dependency Injection using Unity container appeared first on codecompiled.

Show more