Monday, February 8, 2010

Unity Application Block (Dependency Injection)

Writing a stable and efficient software has been always a challenge for developers. In a team environment , working on a application and collaborating with other team member poses its own set of challenges. To tackle all these issues we as a developer adopt wide range of standard and industry approved design patterns . The focus is on designing the application that has modularity and is loosely coupled . Various components of the application have to be designed, so that they do not have dependencies on each other and should wire up efficiently with each other. These components should be able to fit with the other application where they are required, enhancing the code reusability .

Various types of design techniques are used for various types of projects like Model View Controller for web projects, Presenter Model and Model View ViewModel for WPF projects etc . Patterns are in place for providing dependency free and loosely coupled applications like Unity Framework , Object Builder , Ninject, Spring Net etc.

Dependency Injection resolves wide range of issues while implementing a effective design for components building. With DI our classes do not refer any dependent class directly , but via interface that hides the concrete type from our class. For example if the Class A have direct reference of the Class B , then our Class A is dependent on Class B. Now Class B concrete type dependency have to be available at the compile time .

Any changes in Classs B will result in the changes in classes that have direct reference of the Class B. Testing of the Class A now is impossible in isolation mode bcoz of dependency on the Class B. There is now repetitive code to create , locate and manage the dependency for every classes that directly reference the Class B.

Implementing DI in the given scenario will mean to have an Interface for the Class B . Now Class A refers to the interface instead of the concrete type Class B. Concrete type of the dependency is now hidden from our class and at compile time there is now no concrete dependency in the Class A. Testing in isolation is now possible as we can create mock types that implement the interface and provide there instances to our Class A. Initialize, locate and manage logic of the dependencies can now be provided from centralized location. In DI we inject the dependent objects in the class , in comparison to allowing the class to create the objects of the dependent class themself.

Now the code that uses the Class A have to configure the Class A , that means resolving the dependencies and injecting them in Class A. But all the client code that uses the Class A will have to perform configuration resulting in some repetitive code . We can leverage the .Net reflection to resolve our dependencies dynamically , that can be done by the use of Containers . Containers provide the way to resolve the dependencies dynamically and configure the classes in comparison to being configured by the client code .

Unity Application Block provide us with tools and framework to implement DI within our applications. In this blog we will look into the Unity Application Block and simple example how to configure a sample application to implement DI using Unity Application Block.

Unity Application Block is the part of Microsoft Enterprise Library .For our sample we will be creating a simple console application and adding the reference of the assemblies Microsoft.Practices.Unity, Microsoft.Practices.Unity.Configurationn and Microsoft.Practices.ObjectBuilder2. These assemblies are available to you once you have installed Microsoft Enterprise Library (4.1 is latest version at the time of writing this blog.). Once we have a console application and referenced assemblies we are ready to build the Unity implemented application . Sample application has a component that uses a service, this component is consumed by the parent component. A simple working of the DI using Unity is implemented in this sample.

Dependency Injection are of three type:- Method Dependency Injection, Property Dependency Injection and Constructor Dependency Injection. In this sample all the type of dependency injection method are used.

We start by creating the interfaces to implement our sample architecture. We create two interface IMarketService for the service and IComponent for the sub-component.


















Then we create concrete types that implement these interfaces -ServiceA and ComponentA . ServiceA implements the constructor injection and ComponentA implements property injection. Implementing the injection on the property, method and constructor is simple, we simply define the attribute on the respective dependency injection points.

  • [Dependency] Attribute for the Property Injection.
  • [InjectionConstructor] Attribute for Constructor injection.
  • [InjectionMethod] Attribute for the Method Injection.













Next there is class for the consumption of the Component :- ParentComponent.







This class implement the method injection. Now we have three concrete classes ;- ServiceA, ComponentA and ParentComponent. ParentComponent consumes the ComponentA, ComponentA uses the ServiceA , but all of them are not tightly coupled and there dependencies are resolved at run time.

Now the code to cofigure the whole setup and make it work.


We use UnityContainer class to configure whole setup.IUnityContainer works for us as a container that dynamically resolves the dependencies and handles the initialization logic for whole setup. We use the RegisterType method of the Unity Container to register type for injection mechanism and mapping with the container.

Next we use the UnityContainer's Configure method to provide the constructor parameter value for the ServiceA injected type. Finally we use the Resolve method of the UnityContainer to resolve all the dependencies and to inject them for parent container . This method returns the instance of the parent container and creates the instances of the dependency types and injects them.

Throughout the whole process we never used 'new' to create dependency instance for injection. UnityContainer took care of all that and also injected them at proper places (where the attributes were defined.)

No comments:

Post a Comment