SlideShare une entreprise Scribd logo
1  sur  23
Dependency Inversion Principle


   Depend on abstractions, not on concretions.



                          Shahriar Hyder
                          Kaz Software Ltd.
                          29th September, 2011
Dependency Inversion Principle
   In object-oriented
    programming, the dependency inversion
    principle refers to a specific form
    of decoupling where
    conventional dependency
    relationships established from high-
    level, policy-setting modules to low-
    level, dependency modules are inverted (i.e.
    reversed) for the purpose of rendering high-
    level modules independent of the low-level
    module implementation details. The principle
Dependency Inversion Principle
High level modules should not depend on
low level modules. Both should depend on
abstractions.

Abstractions should not depend on details.
Details should depend on abstractions.
Depend on abstractions, not on
concretions
Inversion of control
   In software engineering, Inversion of
    Control (IoC) is an abstract principle
    describing an aspect of some software
    architecture designs in which the flow of
    control of a system is inverted in comparison
    to procedural programming.
Inversion of control
   Also known as the Hollywood Principle
     Don’t   call us, we’ll call you!
   Objects rely on their environment to provide
    dependencies rather than actively obtaining
    them.
   Inversion of Control can make the difference
    between a library and a framework.
Dependency Injection
   Dependency injection (DI) is a design
    pattern in object-oriented computer
    programming whose purpose is to reduce
    the coupling between software components. It is
    similar to the factory method pattern. Frequently
    an object uses (depends on) work produced by
    another part of the system. With DI, the object
    does not need to know in advance about how the
    other part of the system works. Instead, the
    programmer provides (injects) the relevant system
    component in advance along with a contract that it
    will behave in a certain way.
Dependency Injection
   Dependency injection is = IoC + Dependency
    Inversion:
     Dependencies are provided externally so we
      enforce the dependency inversion principle
     The container sets the dependencies (not us) so
      we speak of inversion of control
   Aspect-oriented programming is one technique
    to implement IoC.
   Dependency Injection - one example of IoC
    design principle.
The Service Locator Pattern
   The service locator pattern is a design
    pattern used in software development to
    encapsulate the processes involved in
    obtaining a service with a strong abstraction
    layer. This pattern uses a central registry
    known as the "service locator" which on
    request returns the information necessary to
    perform a certain task.
The Service Locator Pattern
   For some reasons many people think that
    there is no relationship between Service
    Locator and DI. The Service Locator is yet
    another implementation of the Dependency
    Inversion Principle. And yes Service Locator is
    competitor for the Dependency Injection
    Pattern! But they still can co-exist.
Plugin
   Use your brains!
DIP Violations
   Use of “new”
DIP Violations
   Use of “static”

public class SecurityService
{
    public static User GetCurrentUser()
    {
        // do something
    }
}
DIP Violations
   Use of singletons using “static”

public class ProductCache
{
    private static readonly _instance = new ProductCache();
    public static ProductCache Instance
    {
        get { return _instance; }
    }
}
What is a DI (IoC) Container?
   Creates objects that are ready for you to use
   Knows how to create objects and their
    dependencies
   Knows how to initialize objects when they are
    created (if necessary)
   Provide lifecycle support
   A container is nothing more than a fancy
    dictionary of interfaces and their implementing
    types
   Inversion Of Control containers act like “super
    factories”
DI Containers
   Spring Framework
   Unity (Microsoft)
   Windsor (Castle Project)
   Ninject
   StructureMap
Types of Dependency Injection
  Constructor   (Most popular)
  Setter

  Method
Type 1 or interface injection /
Method Injection




Injecting a ICustomerRepository as well as an integer dependency.
Type 2 or Setter Injection




   Injecting a ICustomerRepository through the setter.
Type 3 or Constructor Injection




Injecting a ICustomerRepository and a ICustomerDTOMapper through
the constructor.

Note: This is the most popular type of injection.
Dependency Injection Pros &
Cons
   Pros
       Loosely Coupled
       Increases Testability (A LOT!)
       Separates components cleanly
       Allows for use of Inversion of Control Container
   Cons
       Increases code complexity
       Some Jr. Developers find it difficult to understand at First
       Can Complicate Debugging at First
       Complicates following Code Flow
       Architecture is less straight-forward
       Overkill for smaller or throwaway applications
       Easy to abuse
       Be careful to avoid “XML Hell” in configuration files
*ILITIES
    Agility
    Testability
    Reversibility
    Changeability
    Flexibility
    Maintainability
Resource
   Probably the best place to read is Martin
    Fowler - Inversion of Control Containers and
    the Dependency Injection pattern

Contenu connexe

Tendances

Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlArjun Thakur
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptionsmyrajendra
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesAndreas Enbohm
 
What is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaWhat is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaEdureka!
 
Interface segregation principle
Interface segregation principleInterface segregation principle
Interface segregation principlemuhammadali0014
 
Constructors in java
Constructors in javaConstructors in java
Constructors in javachauhankapil
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 
Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design PatternAsif Tayef
 
Solid principles
Solid principlesSolid principles
Solid principlesToan Nguyen
 

Tendances (20)

Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Java exception
Java exception Java exception
Java exception
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 
7.data types in c#
7.data types in c#7.data types in c#
7.data types in c#
 
Design patterns
Design patternsDesign patterns
Design patterns
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
What is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaWhat is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | Edureka
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Spring boot
Spring bootSpring boot
Spring boot
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Interface segregation principle
Interface segregation principleInterface segregation principle
Interface segregation principle
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
LINQ in C#
LINQ in C#LINQ in C#
LINQ in C#
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Presentation on Template Method Design Pattern
Presentation on Template Method Design PatternPresentation on Template Method Design Pattern
Presentation on Template Method Design Pattern
 
Solid principles
Solid principlesSolid principles
Solid principles
 

En vedette

Inversion of control
Inversion of controlInversion of control
Inversion of controlEmmet Irish
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar Hyder
 
Dependency injection
Dependency injectionDependency injection
Dependency injectionhousecor
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
 
SOLID Principles Part 3
SOLID Principles Part 3SOLID Principles Part 3
SOLID Principles Part 3Maulik Soni
 
Dependency Injection with Unity Container
Dependency Injection with Unity ContainerDependency Injection with Unity Container
Dependency Injection with Unity ContainerMindfire Solutions
 
Functional Dependency Injection in C#
Functional Dependency Injection in C#Functional Dependency Injection in C#
Functional Dependency Injection in C#Thomas Jaskula
 
Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID PrinciplesGanesh Samarthyam
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kataPaul Blundell
 
Overview of adult traumatic
Overview of adult traumaticOverview of adult traumatic
Overview of adult traumaticHung Pham Thai
 
Cách lọc nước bị ô nhiễm
Cách lọc nước bị ô nhiễmCách lọc nước bị ô nhiễm
Cách lọc nước bị ô nhiễmHung Pham Thai
 
Technology In The Classroom
Technology In The ClassroomTechnology In The Classroom
Technology In The Classroomhales4
 
Burnout and engagement in university students
Burnout and engagement in university studentsBurnout and engagement in university students
Burnout and engagement in university studentsLaboralyorganizacional
 
The peruvian asparagus industry
The peruvian asparagus industryThe peruvian asparagus industry
The peruvian asparagus industryHung Pham Thai
 

En vedette (20)

Inversion of control
Inversion of controlInversion of control
Inversion of control
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Dependency Injection and Autofac
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
 
SOLID Principles Part 3
SOLID Principles Part 3SOLID Principles Part 3
SOLID Principles Part 3
 
Dependency Injection with Unity Container
Dependency Injection with Unity ContainerDependency Injection with Unity Container
Dependency Injection with Unity Container
 
Functional Dependency Injection in C#
Functional Dependency Injection in C#Functional Dependency Injection in C#
Functional Dependency Injection in C#
 
Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID Principles
 
SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kata
 
Overview of adult traumatic
Overview of adult traumaticOverview of adult traumatic
Overview of adult traumatic
 
Cách lọc nước bị ô nhiễm
Cách lọc nước bị ô nhiễmCách lọc nước bị ô nhiễm
Cách lọc nước bị ô nhiễm
 
Technology In The Classroom
Technology In The ClassroomTechnology In The Classroom
Technology In The Classroom
 
Burnout and engagement in university students
Burnout and engagement in university studentsBurnout and engagement in university students
Burnout and engagement in university students
 
The peruvian asparagus industry
The peruvian asparagus industryThe peruvian asparagus industry
The peruvian asparagus industry
 
Excel 2007 bai 2-1
Excel 2007   bai 2-1Excel 2007   bai 2-1
Excel 2007 bai 2-1
 
K51.24.05.2009
K51.24.05.2009K51.24.05.2009
K51.24.05.2009
 
earth
earthearth
earth
 
Calendasexy Viet Nam
Calendasexy Viet NamCalendasexy Viet Nam
Calendasexy Viet Nam
 
japan boy
japan boyjapan boy
japan boy
 

Similaire à Dependency Inversion Principle

Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkASG
 
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
 
Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast trackBinu Bhasuran
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoCDennis Loktionov
 
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 .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NETssusere19c741
 
Dependency injection explained (Zbigniew Lukasiak)
Dependency injection explained (Zbigniew Lukasiak) Dependency injection explained (Zbigniew Lukasiak)
Dependency injection explained (Zbigniew Lukasiak) Ontico
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckTheo Jungeblut
 
Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012Timothy Spann
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsJuan Lopez
 
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019UA Mobile
 
The Power of Enterprise Java Frameworks
The Power of Enterprise Java FrameworksThe Power of Enterprise Java Frameworks
The Power of Enterprise Java FrameworksClarence Ho
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptxNourhanTarek23
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesQamar Abbas
 
Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)Mohammed Salah Eldowy
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleP Heinonen
 

Similaire à Dependency Inversion Principle (20)

Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
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
 
Design patterns fast track
Design patterns fast trackDesign patterns fast track
Design patterns fast track
 
Swiz DAO
Swiz DAOSwiz DAO
Swiz DAO
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoC
 
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 .NET
Dependency Injection in .NETDependency Injection in .NET
Dependency Injection in .NET
 
Dependency injection explained (Zbigniew Lukasiak)
Dependency injection explained (Zbigniew Lukasiak) Dependency injection explained (Zbigniew Lukasiak)
Dependency injection explained (Zbigniew Lukasiak)
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group OsnabrueckCut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
 
Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012Brownbag001 spring ioc from 2012
Brownbag001 spring ioc from 2012
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and Patterns
 
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
Ідіоматична ін'єкція залежностей на Kotlin без фреймворків - UA Mobile2019
 
The Power of Enterprise Java Frameworks
The Power of Enterprise Java FrameworksThe Power of Enterprise Java Frameworks
The Power of Enterprise Java Frameworks
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptx
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelines
 
Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)
 
Mock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion PrincipleMock Objects, Design and Dependency Inversion Principle
Mock Objects, Design and Dependency Inversion Principle
 

Plus de Shahriar Hyder

Effective Communication Skills for Software Engineers
Effective Communication Skills for Software EngineersEffective Communication Skills for Software Engineers
Effective Communication Skills for Software EngineersShahriar Hyder
 
A JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFsA JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFsShahriar Hyder
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design PatternShahriar Hyder
 
Taking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocketTaking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocketShahriar Hyder
 
Object Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQLObject Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQLShahriar Hyder
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language InnovationsShahriar Hyder
 

Plus de Shahriar Hyder (8)

Effective Communication Skills for Software Engineers
Effective Communication Skills for Software EngineersEffective Communication Skills for Software Engineers
Effective Communication Skills for Software Engineers
 
A JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFsA JavaScript Master Class - From the Wows to the WTFs
A JavaScript Master Class - From the Wows to the WTFs
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Taking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocketTaking a Quantum Leap with Html 5 WebSocket
Taking a Quantum Leap with Html 5 WebSocket
 
Object Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQLObject Relational Mapping with LINQ To SQL
Object Relational Mapping with LINQ To SQL
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
 
Introduction to Linq
Introduction to LinqIntroduction to Linq
Introduction to Linq
 

Dernier

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
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
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 

Dernier (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 

Dependency Inversion Principle

  • 1. Dependency Inversion Principle  Depend on abstractions, not on concretions. Shahriar Hyder Kaz Software Ltd. 29th September, 2011
  • 2. Dependency Inversion Principle  In object-oriented programming, the dependency inversion principle refers to a specific form of decoupling where conventional dependency relationships established from high- level, policy-setting modules to low- level, dependency modules are inverted (i.e. reversed) for the purpose of rendering high- level modules independent of the low-level module implementation details. The principle
  • 3. Dependency Inversion Principle High level modules should not depend on low level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
  • 4. Depend on abstractions, not on concretions
  • 5. Inversion of control  In software engineering, Inversion of Control (IoC) is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming.
  • 6. Inversion of control  Also known as the Hollywood Principle  Don’t call us, we’ll call you!  Objects rely on their environment to provide dependencies rather than actively obtaining them.  Inversion of Control can make the difference between a library and a framework.
  • 7. Dependency Injection  Dependency injection (DI) is a design pattern in object-oriented computer programming whose purpose is to reduce the coupling between software components. It is similar to the factory method pattern. Frequently an object uses (depends on) work produced by another part of the system. With DI, the object does not need to know in advance about how the other part of the system works. Instead, the programmer provides (injects) the relevant system component in advance along with a contract that it will behave in a certain way.
  • 8. Dependency Injection  Dependency injection is = IoC + Dependency Inversion:  Dependencies are provided externally so we enforce the dependency inversion principle  The container sets the dependencies (not us) so we speak of inversion of control  Aspect-oriented programming is one technique to implement IoC.  Dependency Injection - one example of IoC design principle.
  • 9. The Service Locator Pattern  The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the "service locator" which on request returns the information necessary to perform a certain task.
  • 10. The Service Locator Pattern  For some reasons many people think that there is no relationship between Service Locator and DI. The Service Locator is yet another implementation of the Dependency Inversion Principle. And yes Service Locator is competitor for the Dependency Injection Pattern! But they still can co-exist.
  • 11. Plugin  Use your brains!
  • 12. DIP Violations  Use of “new”
  • 13. DIP Violations  Use of “static” public class SecurityService { public static User GetCurrentUser() { // do something } }
  • 14. DIP Violations  Use of singletons using “static” public class ProductCache { private static readonly _instance = new ProductCache(); public static ProductCache Instance { get { return _instance; } } }
  • 15. What is a DI (IoC) Container?  Creates objects that are ready for you to use  Knows how to create objects and their dependencies  Knows how to initialize objects when they are created (if necessary)  Provide lifecycle support  A container is nothing more than a fancy dictionary of interfaces and their implementing types  Inversion Of Control containers act like “super factories”
  • 16. DI Containers  Spring Framework  Unity (Microsoft)  Windsor (Castle Project)  Ninject  StructureMap
  • 17. Types of Dependency Injection  Constructor (Most popular)  Setter  Method
  • 18. Type 1 or interface injection / Method Injection Injecting a ICustomerRepository as well as an integer dependency.
  • 19. Type 2 or Setter Injection Injecting a ICustomerRepository through the setter.
  • 20. Type 3 or Constructor Injection Injecting a ICustomerRepository and a ICustomerDTOMapper through the constructor. Note: This is the most popular type of injection.
  • 21. Dependency Injection Pros & Cons  Pros  Loosely Coupled  Increases Testability (A LOT!)  Separates components cleanly  Allows for use of Inversion of Control Container  Cons  Increases code complexity  Some Jr. Developers find it difficult to understand at First  Can Complicate Debugging at First  Complicates following Code Flow  Architecture is less straight-forward  Overkill for smaller or throwaway applications  Easy to abuse  Be careful to avoid “XML Hell” in configuration files
  • 22. *ILITIES  Agility  Testability  Reversibility  Changeability  Flexibility  Maintainability
  • 23. Resource  Probably the best place to read is Martin Fowler - Inversion of Control Containers and the Dependency Injection pattern