SlideShare a Scribd company logo
Inversion Of Control (IOC) and Unity FrameworkPresented by : M.M.Al-FarooqueShubhohttp://www.linkedin.com/in/thisisshubho
Dependency Injection in real worldStory 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 1Each 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 2The 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 objectsThe following class depicts the Story 1public 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 depencencyThe following class depicts the Story 2public 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 definedAs 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 DIMain 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=enAdd reference to  Microsoft.Practices.Unity, Microsoft.Practices.Unity.ConfigurationCreate 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 (20)

PPTX
Validate your entities with symfony validator and entity validation api
Raffaele Chiocca
 
PPT
Entity Persistence with JPA
Subin Sugunan
 
PPTX
Spring FrameWork Tutorials Java Language
Mahika Tutorials
 
PPT
Deployment
Roy Antony Arnold G
 
PDF
Java persistence api 2.1
Rakesh K. Cherukuri
 
PPTX
Евгений Капинос "Advanced JPA (Java Persistent API)"
Anna Shymchenko
 
ODP
JPA Best Practices
Carol McDonald
 
PDF
Asp dot-net core problems and fixes
sonia merchant
 
PPT
Java Persistence API (JPA) Step By Step
Guo Albert
 
PPTX
Dependency Inversion in large-scale TypeScript applications with InversifyJS
Remo Jansen
 
PDF
Dependency injection in Java, from naive to functional
Marian Wamsiedel
 
PPTX
Intro to React
Eric Westfall
 
PDF
New Features of JSR 317 (JPA 2.0)
Markus Eisele
 
PDF
Client-side JavaScript
Lilia Sfaxi
 
PPTX
Introduction to React JS for beginners
Varun Raj
 
PDF
iOS viper presentation
Rajat Datta
 
PPTX
Its time to React.js
Ritesh Mehrotra
 
PPTX
Sexy Architecting. VIPER: MVP on steroids
Dmytro Zaitsev
 
PPTX
Introduction to jQuery
Collaboration Technologies
 
PDF
An Introduction to Redux
NexThoughts Technologies
 
Validate your entities with symfony validator and entity validation api
Raffaele Chiocca
 
Entity Persistence with JPA
Subin Sugunan
 
Spring FrameWork Tutorials Java Language
Mahika Tutorials
 
Java persistence api 2.1
Rakesh K. Cherukuri
 
Евгений Капинос "Advanced JPA (Java Persistent API)"
Anna Shymchenko
 
JPA Best Practices
Carol McDonald
 
Asp dot-net core problems and fixes
sonia merchant
 
Java Persistence API (JPA) Step By Step
Guo Albert
 
Dependency Inversion in large-scale TypeScript applications with InversifyJS
Remo Jansen
 
Dependency injection in Java, from naive to functional
Marian Wamsiedel
 
Intro to React
Eric Westfall
 
New Features of JSR 317 (JPA 2.0)
Markus Eisele
 
Client-side JavaScript
Lilia Sfaxi
 
Introduction to React JS for beginners
Varun Raj
 
iOS viper presentation
Rajat Datta
 
Its time to React.js
Ritesh Mehrotra
 
Sexy Architecting. VIPER: MVP on steroids
Dmytro Zaitsev
 
Introduction to jQuery
Collaboration Technologies
 
An Introduction to Redux
NexThoughts Technologies
 

Viewers also liked (10)

PPT
Dependency Injection & IoC
Dennis Loktionov
 
PPT
Inversion of Control and Dependency Injection
Andriy Buday
 
PPT
Practical Inversion Of Control
mhinze
 
PDF
Agile and Frameworks
Sander Hoogendoorn
 
PPTX
Design patterns[observer and ioc]
ppdjango
 
PPT
20080531 Intro To Dependency Injection & Inversion Of Control
donnfelker
 
PPTX
The Spring Framework: A brief introduction to Inversion of Control
VisualBee.com
 
PPT
MVC
akshin
 
PPT
Aspect Oriented Programming
Anumod Kumar
 
PPTX
Inversion of Control - Introduction and Best Practice
Lars-Erik Kindblad
 
Dependency Injection & IoC
Dennis Loktionov
 
Inversion of Control and Dependency Injection
Andriy Buday
 
Practical Inversion Of Control
mhinze
 
Agile and Frameworks
Sander Hoogendoorn
 
Design patterns[observer and ioc]
ppdjango
 
20080531 Intro To Dependency Injection & Inversion Of Control
donnfelker
 
The Spring Framework: A brief introduction to Inversion of Control
VisualBee.com
 
MVC
akshin
 
Aspect Oriented Programming
Anumod Kumar
 
Inversion of Control - Introduction and Best Practice
Lars-Erik Kindblad
 
Ad

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

PDF
Mock Objects, Design and Dependency Inversion Principle
P Heinonen
 
PPTX
Introduction to Dependency Injection
SolTech, Inc.
 
PPTX
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Theo Jungeblut
 
PPTX
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Theo Jungeblut
 
PDF
Unity Framework
Krantisinh Deshmukh
 
PPTX
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Theo Jungeblut
 
PPTX
Design patterns fast track
Binu Bhasuran
 
PPTX
Clean Code Part II - Dependency Injection at SoCal Code Camp
Theo Jungeblut
 
PPTX
I gotta dependency on dependency injection
mhenroid
 
PDF
Dependency Injection
Giovanni Scerra ☃
 
PPTX
Dependency injection using Google guice
Aman Verma
 
PPTX
Application Architecture
Lars-Erik Kindblad
 
PPTX
Dependency Injection in .NET
ssusere19c741
 
PPTX
Dependency Inversion Principle
Shahriar Hyder
 
PPTX
Clearing confusion about IoC (Inversion of Control)
Mohammed Salah Eldowy
 
PDF
The Zen of Inversion of Control
christopherfairbairn
 
PPTX
Application Architecture April 2014
Lars-Erik Kindblad
 
PPT
Ef Poco And Unit Testing
James Phillips
 
PPT
Tdd,Ioc
Antonio Radesca
 
PPTX
Dependency Injection або Don’t call me, I’ll call you
Dmytro Mindra
 
Mock Objects, Design and Dependency Inversion Principle
P Heinonen
 
Introduction to Dependency Injection
SolTech, Inc.
 
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Theo Jungeblut
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Theo Jungeblut
 
Unity Framework
Krantisinh Deshmukh
 
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Theo Jungeblut
 
Design patterns fast track
Binu Bhasuran
 
Clean Code Part II - Dependency Injection at SoCal Code Camp
Theo Jungeblut
 
I gotta dependency on dependency injection
mhenroid
 
Dependency Injection
Giovanni Scerra ☃
 
Dependency injection using Google guice
Aman Verma
 
Application Architecture
Lars-Erik Kindblad
 
Dependency Injection in .NET
ssusere19c741
 
Dependency Inversion Principle
Shahriar Hyder
 
Clearing confusion about IoC (Inversion of Control)
Mohammed Salah Eldowy
 
The Zen of Inversion of Control
christopherfairbairn
 
Application Architecture April 2014
Lars-Erik Kindblad
 
Ef Poco And Unit Testing
James Phillips
 
Dependency Injection або Don’t call me, I’ll call you
Dmytro Mindra
 
Ad

More from rainynovember12 (8)

PPTX
Object Oriented Design SOLID Principles
rainynovember12
 
PPTX
Model View Presenter (MVP) In Aspnet
rainynovember12
 
PPTX
Introduction To REST
rainynovember12
 
PPTX
Sql Basics And Advanced
rainynovember12
 
PPTX
Mvc Brief Overview
rainynovember12
 
PPT
Sql Server Basics
rainynovember12
 
PPT
Optimizing Data Accessin Sq Lserver2005
rainynovember12
 
PPT
Aspnet Caching
rainynovember12
 
Object Oriented Design SOLID Principles
rainynovember12
 
Model View Presenter (MVP) In Aspnet
rainynovember12
 
Introduction To REST
rainynovember12
 
Sql Basics And Advanced
rainynovember12
 
Mvc Brief Overview
rainynovember12
 
Sql Server Basics
rainynovember12
 
Optimizing Data Accessin Sq Lserver2005
rainynovember12
 
Aspnet Caching
rainynovember12
 

Dependency Injection Inversion Of Control And Unity

  • 1. Inversion Of Control (IOC) and Unity FrameworkPresented by : M.M.Al-FarooqueShubhohttp://www.linkedin.com/in/thisisshubho
  • 2. Dependency Injection in real worldStory 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 1Each 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 2The 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 objectsThe following class depicts the Story 1public 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 depencencyThe following class depicts the Story 2public 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 definedAs 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 DIMain 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=enAdd reference to Microsoft.Practices.Unity, Microsoft.Practices.Unity.ConfigurationCreate 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)