Dependency Injection Inversion Of Control And Unity
1. Inversion Of Control (IOC) and Unity Framework Presented by : M.M.Al-FarooqueShubho http://www.linkedin.com/in/thisisshubho
2. Dependency Injection in real world Story 1: All employees in Jaxara has to pay yearly income tax and each employee knows the tax lawyer to communicate to (Via phone) for handling tax matters. Story 2: All employees in Jaxara has to pay yearly income tax and the HR manager handles the tax issue of each employee by communicating with a lawyer. For tax related matters, the HR manager communicates with Employees about who the tax lawyer. When an employee needs to communicate with the tax lawyer, he/she does that via the HR Manager (Because, he knows how to communicate with the lawyer).
3. What if there is a change? What if For any reason The tax lawyer is changed, or, The tax lawyer changes his/her communication mechanism
4. What is the change impact? Story 1 Each employee needs to update themselves with the new layer info and also needs to change their communication mechanism to the lawyer (Instead of phone, communicate via a web site). Story 2 The HR manager will update the lawyer info and notify each employee with defining the updated communication mechanism. So, when each employee will need to communicate with the lawyer, they don’t need to change their communication mechanism.
5. The Problem : Tight coupling of dependent objects The following class depicts the Story 1 public class Employee { private TaxLawyer_taxLawyer; public Employee() { _ taxLawyer= new TaxLawyer (); } } The Employee class controls the creation of TaxLawyer objects and the TaxLawyer class is directly referred within the Employee. So, if the TaxLawyer class is replaced sometimes with another class (Say, IncomeTaxLawyer class), the Employee class needs a change.
6. The Solution: Inject the depencency The following class depicts the Story 2 public class Employee { private ITaxLawyer_taxLawyer; public Employee(ITaxLawyer_taxLawyer) { this. _ taxLawyer= _ taxLawyer; } } The Employee class controls the creation of ITaxLawyer object, rather, an external class will instantiate the ITaxLawyer object and set it within the Employee object. Note that, the ITaxLawyer is an interface, rather than a concrete class. So, if for any reason, the TaxLawyer is changed (Say, IncomeTaxLawyer) , there will be no change in the Employee class (Because, both TaxLawyer and the IncomeTaxLawyer implements ITaxLawyer) . Even if the changed class (IncomeTaxLawyer) has a different communication method implementation, Employee class need not to worry about any change. It knows only about the Interface, not any concrete implementation class.
7. The Dependency Injection defined As we have seen already, if the object creation control is not implemented within the class, rather, if this control is assigned to an external class or entity, we get: Flexibility. Least change impact. Cleaner code. So, the principle of Dependency Injection can be defined as follows: “Do not call us we will call you” (Hollywood principle) Its like TaxLawyer class saying to the Employee class, do not create me, rather, I will create myself using some one else.
8. Principles of DI Main classes aggregating other classes should not depend on the direct implementation of the aggregated classes, rather, should depend on abstract implementation (Interface). Abstraction (Interface) should not depend on detail (Class), rather, the detail should depend on the abstraction.
9. The Inversion of Control (IOC) As we have seen, in the Dependency Injection, there should be an external entity (Class) who should handle the object creation and inject those in the dependent object. This idea is called Inversion of Control (IOC). The external class is called IOC Container. The IOC container allows to register objects within it and also handles instantiation of the object with their dependent objects (The object graph) and allows to retrieve objects from the container.
10. Unity, the IOC Framework/Container The Unity Application Block (Unity) is a lightweight, extensible dependency injection container that supports constructor injection, property injection, and method call injection. How to use Unity? Download Unity Application Block from http://www.microsoft.com/downloads/details.aspx?FamilyId=6A9E363C-8E0A-48D3-BBE4-C2F36423E2DF&displaylang=en Add reference to Microsoft.Practices.Unity, Microsoft.Practices.Unity.Configuration Create a Factory class that would initialize the UnityContainer and expose type/object registration and retrieval functionality within the Unity container.
11. Unity, the IOC Framework/Container Within the Factory class, do the followings: Create a new instance of the UnityContainer class or use a reference to an existing instance (Possibly, in the constructor) IUnityContainermyContainer = new UnityContainer(); Create a method to register objects within the container. Use RegisterType method of container class to register a type into the container. myContainer.RegisterType<typeof(ITaxLawyer), typeof(IncomeTaxlawyer)>(); The lifetime of the object it builds will correspond to the life time specified in the parameters of the method. If lifetime is not specified then the type will registered for a transient lifetime, which means that a new instance will be created on each call to Resolve. ( It is method , which will be discussed later)
12. Unity, the IOC Framework/Container Create a method that accept a Type parameter for retrieving object from the container. Within the method, use the Resolve method to retrieve object instances from the container. ITaxLawyer lawyer = _container.Resolve(ITaxLawyer) To get exact concrete object use: IncomeTaxLawyer lawyer = _container.Resolve(IncomeTaxLawyer)