Dependency Inversion
Principle
PRESENTED BY:
M QAMAR UL MUSTAFA
Definition:
 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.
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.
Cont.…
 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 (DI):
 A design pattern in object-oriented computer programming whose purpose is to
reduce the coupling between software components.
 With DI, the object does not need to know in advance about how the other part
of the system works.
The Service Locator Pattern
 A design pattern used in software development to summarize the processes
involved in obtaining a service with a strong abstraction layer. This uses a central
registry known as the "service locator" which on request returns the information
necessary to perform a certain task.
DIP Violations:
 Use of “new”
DIP Violations (cont…)
 Use of “static”
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
Method Injection
Setter Injection
Constructor Injection
Dependency Injection (Pros):
 Loosely Coupled
 Increases Testability (A LOT!)
 Separates components cleanly
 Allows for use of Inversion of Control Container
Dependency Injection (Cons):
 Increases code complexity
 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
Dip(dependency inversion principle) presentation

Dip(dependency inversion principle) presentation

  • 1.
  • 2.
    Definition:  High levelmodules should not depend on low level modules. Both should depend on abstractions.  Abstractions should not depend on details. Details should depend on abstractions.
  • 3.
    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.
  • 4.
    Cont.…  Also knownas 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.
  • 5.
    Dependency Injection (DI): A design pattern in object-oriented computer programming whose purpose is to reduce the coupling between software components.  With DI, the object does not need to know in advance about how the other part of the system works.
  • 6.
    The Service LocatorPattern  A design pattern used in software development to summarize the processes involved in obtaining a service with a strong abstraction layer. This uses a central registry known as the "service locator" which on request returns the information necessary to perform a certain task.
  • 7.
  • 8.
    DIP Violations (cont…) Use of “static”
  • 9.
    What is aDI (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”
  • 10.
    DI Containers  SpringFramework  Unity (Microsoft)  Windsor (Castle Project)  Ninject  StructureMap
  • 11.
    Types of DependencyInjection  Constructor (Most popular)  Setter  Method
  • 12.
  • 13.
  • 14.
  • 15.
    Dependency Injection (Pros): Loosely Coupled  Increases Testability (A LOT!)  Separates components cleanly  Allows for use of Inversion of Control Container
  • 16.
    Dependency Injection (Cons): Increases code complexity  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