SlideShare a Scribd company logo
1 of 12
Inversion Of Control (IOC) and Unity Framework Presented by : M.M.Al-FarooqueShubho http://www.linkedin.com/in/thisisshubho
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).
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
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.
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.
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.
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.
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.
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.
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.
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)
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)

More Related Content

What's hot

Validate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation apiValidate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation apiRaffaele Chiocca
 
Entity Persistence with JPA
Entity Persistence with JPAEntity Persistence with JPA
Entity Persistence with JPASubin Sugunan
 
Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language Mahika Tutorials
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Anna Shymchenko
 
Asp dot-net core problems and fixes
Asp dot-net core problems and fixes Asp dot-net core problems and fixes
Asp dot-net core problems and fixes sonia merchant
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Dependency Inversion in large-scale TypeScript applications with InversifyJS
Dependency Inversion in large-scale TypeScript applications with InversifyJSDependency Inversion in large-scale TypeScript applications with InversifyJS
Dependency Inversion in large-scale TypeScript applications with InversifyJSRemo Jansen
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalMarian Wamsiedel
 
New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)Markus Eisele
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScriptLilia Sfaxi
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
iOS viper presentation
iOS viper presentationiOS viper presentation
iOS viper presentationRajat Datta
 
Sexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroidsSexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroidsDmytro Zaitsev
 

What's hot (20)

Validate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation apiValidate your entities with symfony validator and entity validation api
Validate your entities with symfony validator and entity validation api
 
Entity Persistence with JPA
Entity Persistence with JPAEntity Persistence with JPA
Entity Persistence with JPA
 
Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language Spring FrameWork Tutorials Java Language
Spring FrameWork Tutorials Java Language
 
Deployment
DeploymentDeployment
Deployment
 
Java persistence api 2.1
Java persistence api 2.1Java persistence api 2.1
Java persistence api 2.1
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"Евгений Капинос "Advanced JPA (Java Persistent API)"
Евгений Капинос "Advanced JPA (Java Persistent API)"
 
JPA Best Practices
JPA Best PracticesJPA Best Practices
JPA Best Practices
 
Asp dot-net core problems and fixes
Asp dot-net core problems and fixes Asp dot-net core problems and fixes
Asp dot-net core problems and fixes
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Dependency Inversion in large-scale TypeScript applications with InversifyJS
Dependency Inversion in large-scale TypeScript applications with InversifyJSDependency Inversion in large-scale TypeScript applications with InversifyJS
Dependency Inversion in large-scale TypeScript applications with InversifyJS
 
Dependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functionalDependency injection in Java, from naive to functional
Dependency injection in Java, from naive to functional
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)
 
Client-side JavaScript
Client-side JavaScriptClient-side JavaScript
Client-side JavaScript
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
iOS viper presentation
iOS viper presentationiOS viper presentation
iOS viper presentation
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
Sexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroidsSexy Architecting. VIPER: MVP on steroids
Sexy Architecting. VIPER: MVP on steroids
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 

Viewers also liked

Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoCDennis Loktionov
 
Inversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionInversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionAndriy Buday
 
Practical Inversion Of Control
Practical Inversion Of ControlPractical Inversion Of Control
Practical Inversion Of Controlmhinze
 
Design patterns[observer and ioc]
Design patterns[observer and ioc]Design patterns[observer and ioc]
Design patterns[observer and ioc]ppdjango
 
20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Controldonnfelker
 
The Spring Framework: A brief introduction to Inversion of Control
The Spring Framework:A brief introduction toInversion of ControlThe Spring Framework:A brief introduction toInversion of Control
The Spring Framework: A brief introduction to Inversion of ControlVisualBee.com
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented ProgrammingAnumod Kumar
 
Inversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best PracticeInversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best PracticeLars-Erik Kindblad
 

Viewers also liked (10)

Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoC
 
Inversion of Control and Dependency Injection
Inversion of Control and Dependency InjectionInversion of Control and Dependency Injection
Inversion of Control and Dependency Injection
 
Practical Inversion Of Control
Practical Inversion Of ControlPractical Inversion Of Control
Practical Inversion Of Control
 
Agile and Frameworks
Agile and FrameworksAgile and Frameworks
Agile and Frameworks
 
Design patterns[observer and ioc]
Design patterns[observer and ioc]Design patterns[observer and ioc]
Design patterns[observer and ioc]
 
20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control20080531 Intro To Dependency Injection & Inversion Of Control
20080531 Intro To Dependency Injection & Inversion Of Control
 
The Spring Framework: A brief introduction to Inversion of Control
The Spring Framework:A brief introduction toInversion of ControlThe Spring Framework:A brief introduction toInversion of Control
The Spring Framework: A brief introduction to Inversion of Control
 
MVC
MVCMVC
MVC
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Inversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best PracticeInversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best Practice
 

Similar to Dependency Injection Inversion Of Control And Unity

How to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxHow to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxBOSC Tech Labs
 
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra LohikaDependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra LohikaAlex Tumanoff
 
React state
React  stateReact  state
React stateDucat
 
Assignment Instructions 2_7aExplain the interrelationships bet.docx
Assignment Instructions 2_7aExplain the interrelationships bet.docxAssignment Instructions 2_7aExplain the interrelationships bet.docx
Assignment Instructions 2_7aExplain the interrelationships bet.docxssuser562afc1
 
The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...Katy Slemon
 
Behavioral pattern 4
Behavioral pattern 4Behavioral pattern 4
Behavioral pattern 4Naga Muruga
 
Dependency Injection або Don’t call me, I’ll call you
Dependency Injection або Don’t call me, I’ll call youDependency Injection або Don’t call me, I’ll call you
Dependency Injection або Don’t call me, I’ll call youDmytro Mindra
 
Android Application Development - Level 2
Android Application Development - Level 2Android Application Development - Level 2
Android Application Development - Level 2Isham Rashik
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
Programming II hiding, and .pdf
 Programming II hiding, and .pdf Programming II hiding, and .pdf
Programming II hiding, and .pdfaludin007
 
Inversion of control using dependency injection in Web APIs using Unity Conta...
Inversion of control using dependency injection in Web APIs using Unity Conta...Inversion of control using dependency injection in Web APIs using Unity Conta...
Inversion of control using dependency injection in Web APIs using Unity Conta...Akhil Mittal
 
Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...Akhil Mittal
 
Assignment 4- Implementing a Banking Application Due See course shel.pdf
 Assignment 4- Implementing a Banking Application Due See course shel.pdf Assignment 4- Implementing a Banking Application Due See course shel.pdf
Assignment 4- Implementing a Banking Application Due See course shel.pdfamarhandi
 
Cis247 i lab 5 inheritance
Cis247 i lab 5 inheritanceCis247 i lab 5 inheritance
Cis247 i lab 5 inheritancesdjdskjd9097
 
Cis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritanceCis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritanceccis224477
 
Cis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritanceCis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritancecis247
 

Similar to Dependency Injection Inversion Of Control And Unity (20)

How to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptxHow to Detect a Click Outside a React Component.pptx
How to Detect a Click Outside a React Component.pptx
 
ax2012
 ax2012 ax2012
ax2012
 
Dependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra LohikaDependency injection with unity 2.0 Dmytro Mindra Lohika
Dependency injection with unity 2.0 Dmytro Mindra Lohika
 
React state
React  stateReact  state
React state
 
Assignment Instructions 2_7aExplain the interrelationships bet.docx
Assignment Instructions 2_7aExplain the interrelationships bet.docxAssignment Instructions 2_7aExplain the interrelationships bet.docx
Assignment Instructions 2_7aExplain the interrelationships bet.docx
 
iOS Development (Part 2)
iOS Development (Part 2)iOS Development (Part 2)
iOS Development (Part 2)
 
The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...The battle between the states (all about flutter stateless & stateful widgets...
The battle between the states (all about flutter stateless & stateful widgets...
 
Behavioral pattern 4
Behavioral pattern 4Behavioral pattern 4
Behavioral pattern 4
 
Dependency Injection або Don’t call me, I’ll call you
Dependency Injection або Don’t call me, I’ll call youDependency Injection або Don’t call me, I’ll call you
Dependency Injection або Don’t call me, I’ll call you
 
Salary advanceworkflow
Salary advanceworkflowSalary advanceworkflow
Salary advanceworkflow
 
Android Application Development - Level 2
Android Application Development - Level 2Android Application Development - Level 2
Android Application Development - Level 2
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
Programming II hiding, and .pdf
 Programming II hiding, and .pdf Programming II hiding, and .pdf
Programming II hiding, and .pdf
 
Inversion of control using dependency injection in Web APIs using Unity Conta...
Inversion of control using dependency injection in Web APIs using Unity Conta...Inversion of control using dependency injection in Web APIs using Unity Conta...
Inversion of control using dependency injection in Web APIs using Unity Conta...
 
Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...Resolve dependency of dependencies using Inversion of Control and dependency ...
Resolve dependency of dependencies using Inversion of Control and dependency ...
 
Assignment 4- Implementing a Banking Application Due See course shel.pdf
 Assignment 4- Implementing a Banking Application Due See course shel.pdf Assignment 4- Implementing a Banking Application Due See course shel.pdf
Assignment 4- Implementing a Banking Application Due See course shel.pdf
 
Cis247 i lab 5 inheritance
Cis247 i lab 5 inheritanceCis247 i lab 5 inheritance
Cis247 i lab 5 inheritance
 
Lab1-android
Lab1-androidLab1-android
Lab1-android
 
Cis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritanceCis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritance
 
Cis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritanceCis247 a ilab 5 inheritance
Cis247 a ilab 5 inheritance
 

More from rainynovember12

Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principlesrainynovember12
 
Model View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In AspnetModel View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In Aspnetrainynovember12
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005rainynovember12
 

More from rainynovember12 (8)

Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
Model View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In AspnetModel View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In Aspnet
 
Introduction To REST
Introduction To RESTIntroduction To REST
Introduction To REST
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
Mvc Brief Overview
Mvc Brief OverviewMvc Brief Overview
Mvc Brief Overview
 
Sql Server Basics
Sql Server BasicsSql Server Basics
Sql Server Basics
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005
 
Aspnet Caching
Aspnet CachingAspnet Caching
Aspnet Caching
 

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)