SlideShare uma empresa Scribd logo
1 de 14
Dependency Inversion Principle: Intro 1
DIP: definition DEPEND ON ABSTRACTION
DIP: definition DO NOT DEPEND ON CONCRETE OBJECTS
DIP: definition IT’S CALLED INVERSION OF CONTROL .... .... ....
DIP: definition OR DEPENDENCY INJECTION TOO OUT-SOURCING INDIA EFFECTS 5
DIP: practical rules 6 No class should derive from a concrete class No variable should hold a reference to a concrete class No method should override an implemented method of any of its base class
DIP: Inversion of what ?? 7 before after Injector Consumer also high level component depends on an abstraction high level component depens on low level components (concrete classes) interface new() new() new() concrete classes depend on an abstraction Dep1 Dep2 Dep2 Dep1 Dep2 Dep2
DIP: a diagram 8 Take a look at the class diagram concrete classes depend on an abstraction
DIP: Who 9 This guy is not a preacher or a wizard:  Martin Fowler (*) He gave us three main styles(**) of dependency injection : Interface Injection  Setter Injection  Constructor Injection (*) but he is still waiting for a call from hollywood (**) and many other things...
DIP: Interface Injection (Type 1) 10 publicinterfaceIInjector     { voidInjectDependency(IDependentdependent);     } publicclassInjector : IInjector     { privateIDependent_dependent;         publicvoidInjectDependency(IDependentdependent)         { _dependent = dependent;          }         publicvoidDoSomething()         { _dependent.DoSomethingInDependent();         }    } With this technique we define and use interfaces Let’s define an interface to perform injection Injector implements the interface
DIP: Interface Injection (Type 1) 11 Get the correnct dependency based on config file Create our main class and inject the dependency the method references the dependency, so behaviour depends on the config file IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(); injector.InjectDependency(dependency); injector.DoSomething();  Configuration file: <?xml version="1.0"encoding="utf-8" ?> <configuration>   <appSettings>     <add key="ClassName"value="DIP.InterfaceInjection.DependentClass1" />   </appSettings> </configuration>
DIP: Setter Injection (Type 2) 12     publicclassInjector     { privateIDependent_dependent; publicIDependentDependent         { get { return_dependent; } set { _dependent = value; }         } publicvoidDoSomething()         { Dependent.DoSomethingInDependent();         }     } Client class has a property Create our main class and inject the dependency IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(); injector.Dependent = dependency; injector.DoSomething();
DIP: Constructor Injection (Type 3) 13     publicclassInjector     { privateIDependent_dependent; publicInjector(IDependentdependent)         { _dependent = dependent;         } publicvoidDoSomething()         { _dependent.DoSomethingInDependent();         }           } Client class has nothins else a constructor to inject dependency Create our main class and passing dependency through constructor IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(dependency); injector.DoSomething();
DIP: Links  and conclusions 14 Inversion of Control Containers and the Dependency Injection pattern  Introduction to Dependency Injection by Rich Newman  Images from Wacky.com

Mais conteúdo relacionado

Mais procurados

Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in JavaIonut Bilica
 
Solid principles
Solid principlesSolid principles
Solid principlesToan Nguyen
 
Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID PrinciplesGanesh Samarthyam
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesAndreas Enbohm
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade PatternMudasir Qazi
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#Aditya Kumar Rajan
 
Liscov substitution principle
Liscov substitution principleLiscov substitution principle
Liscov substitution principleGanesh Kunwar
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patternsAmit Kabra
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean ArchitectureRoc Boronat
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLIDPranalee Rokde
 
2012 the clean architecture by Uncle bob
2012 the clean architecture by Uncle bob 2012 the clean architecture by Uncle bob
2012 the clean architecture by Uncle bob GEORGE LEON
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General IntroductionAsma CHERIF
 

Mais procurados (20)

Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID Principles
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade Pattern
 
Facade Pattern
Facade PatternFacade Pattern
Facade Pattern
 
Design Pattern
Design PatternDesign Pattern
Design Pattern
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
Liscov substitution principle
Liscov substitution principleLiscov substitution principle
Liscov substitution principle
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
Python SOLID
Python SOLIDPython SOLID
Python SOLID
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLID
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
2012 the clean architecture by Uncle bob
2012 the clean architecture by Uncle bob 2012 the clean architecture by Uncle bob
2012 the clean architecture by Uncle bob
 
Clean code: SOLID
Clean code: SOLIDClean code: SOLID
Clean code: SOLID
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 

Destaque

Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close PrincipleThaichor Seng
 
Solid principles
Solid principlesSolid principles
Solid principlesViet Vu
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed PrincipleSam Hennessy
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principlesrainynovember12
 
Inversion of control
Inversion of controlInversion of control
Inversion of controlEmmet Irish
 

Destaque (10)

SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close Principle
 
Solid principles
Solid principlesSolid principles
Solid principles
 
jeas_0816_4883
jeas_0816_4883jeas_0816_4883
jeas_0816_4883
 
Inversion of control
Inversion of controlInversion of control
Inversion of control
 
JavaTalks: OOD principles
JavaTalks: OOD principlesJavaTalks: OOD principles
JavaTalks: OOD principles
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed Principle
 
JavaTalks.Patterns.Singleton
JavaTalks.Patterns.SingletonJavaTalks.Patterns.Singleton
JavaTalks.Patterns.Singleton
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
Inversion of control
Inversion of controlInversion of control
Inversion of control
 

Semelhante a Dependency Inversion Principle

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
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NETssusere19c741
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2Sanket Shah
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mockingmrjawright
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOSPablo Villar
 
Spring core module
Spring core moduleSpring core module
Spring core moduleRaj Tomar
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Simone Chiaretta
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignJames Phillips
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Dave Hulbert
 
TINTIN: demo of the tool
TINTIN: demo of the toolTINTIN: demo of the tool
TINTIN: demo of the toolErnest Teniente
 

Semelhante a Dependency Inversion Principle (20)

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
 
Dependency Injection in .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
 
Android Dagger 2
Android  Dagger 2Android  Dagger 2
Android Dagger 2
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mocking
 
Intro To AOP
Intro To AOPIntro To AOP
Intro To AOP
 
Inversion Of Control: Spring.Net Overview
Inversion Of Control: Spring.Net OverviewInversion Of Control: Spring.Net Overview
Inversion Of Control: Spring.Net Overview
 
C++ Constructs.pptx
C++ Constructs.pptxC++ Constructs.pptx
C++ Constructs.pptx
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
 
Spring core module
Spring core moduleSpring core module
Spring core module
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
 
Lab (1) installation of python
Lab (1) installation of pythonLab (1) installation of python
Lab (1) installation of python
 
Spring boot
Spring bootSpring boot
Spring boot
 
Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)Silex and Twig (PHP Dorset talk)
Silex and Twig (PHP Dorset talk)
 
TINTIN: demo of the tool
TINTIN: demo of the toolTINTIN: demo of the tool
TINTIN: demo of the tool
 
delphi-interfaces.pdf
delphi-interfaces.pdfdelphi-interfaces.pdf
delphi-interfaces.pdf
 
delphi-interfaces.pdf
delphi-interfaces.pdfdelphi-interfaces.pdf
delphi-interfaces.pdf
 
delphi-interfaces.pdf
delphi-interfaces.pdfdelphi-interfaces.pdf
delphi-interfaces.pdf
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 

Último

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Último (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Dependency Inversion Principle

  • 2. DIP: definition DEPEND ON ABSTRACTION
  • 3. DIP: definition DO NOT DEPEND ON CONCRETE OBJECTS
  • 4. DIP: definition IT’S CALLED INVERSION OF CONTROL .... .... ....
  • 5. DIP: definition OR DEPENDENCY INJECTION TOO OUT-SOURCING INDIA EFFECTS 5
  • 6. DIP: practical rules 6 No class should derive from a concrete class No variable should hold a reference to a concrete class No method should override an implemented method of any of its base class
  • 7. DIP: Inversion of what ?? 7 before after Injector Consumer also high level component depends on an abstraction high level component depens on low level components (concrete classes) interface new() new() new() concrete classes depend on an abstraction Dep1 Dep2 Dep2 Dep1 Dep2 Dep2
  • 8. DIP: a diagram 8 Take a look at the class diagram concrete classes depend on an abstraction
  • 9. DIP: Who 9 This guy is not a preacher or a wizard: Martin Fowler (*) He gave us three main styles(**) of dependency injection : Interface Injection Setter Injection Constructor Injection (*) but he is still waiting for a call from hollywood (**) and many other things...
  • 10. DIP: Interface Injection (Type 1) 10 publicinterfaceIInjector { voidInjectDependency(IDependentdependent); } publicclassInjector : IInjector { privateIDependent_dependent; publicvoidInjectDependency(IDependentdependent) { _dependent = dependent; } publicvoidDoSomething() { _dependent.DoSomethingInDependent(); } } With this technique we define and use interfaces Let’s define an interface to perform injection Injector implements the interface
  • 11. DIP: Interface Injection (Type 1) 11 Get the correnct dependency based on config file Create our main class and inject the dependency the method references the dependency, so behaviour depends on the config file IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(); injector.InjectDependency(dependency); injector.DoSomething(); Configuration file: <?xml version="1.0"encoding="utf-8" ?> <configuration> <appSettings> <add key="ClassName"value="DIP.InterfaceInjection.DependentClass1" /> </appSettings> </configuration>
  • 12. DIP: Setter Injection (Type 2) 12 publicclassInjector { privateIDependent_dependent; publicIDependentDependent { get { return_dependent; } set { _dependent = value; } } publicvoidDoSomething() { Dependent.DoSomethingInDependent(); } } Client class has a property Create our main class and inject the dependency IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(); injector.Dependent = dependency; injector.DoSomething();
  • 13. DIP: Constructor Injection (Type 3) 13 publicclassInjector { privateIDependent_dependent; publicInjector(IDependentdependent) { _dependent = dependent; } publicvoidDoSomething() { _dependent.DoSomethingInDependent(); } } Client class has nothins else a constructor to inject dependency Create our main class and passing dependency through constructor IDependentdependency = GetCorrectDependency(); Injectorinjector = newInjector(dependency); injector.DoSomething();
  • 14. DIP: Links and conclusions 14 Inversion of Control Containers and the Dependency Injection pattern Introduction to Dependency Injection by Rich Newman Images from Wacky.com

Notas do Editor

  1. Dependency inversion principle