Dependency Inversion Principle   Depend on abstractions, not on concretions.                          Shahriar Hyder     ...
Dependency Inversion Principle   In object-oriented    programming, the dependency inversion    principle refers to a spe...
Dependency Inversion PrincipleHigh level modules should not depend onlow level modules. Both should depend onabstractions....
Depend on abstractions, not onconcretions
Inversion of control   In software engineering, Inversion of    Control (IoC) is an abstract principle    describing an a...
Inversion of control   Also known as the Hollywood Principle     Don’t   call us, we’ll call you!   Objects rely on the...
Dependency Injection   Dependency injection (DI) is a design    pattern in object-oriented computer    programming whose ...
Dependency Injection   Dependency injection is = IoC + Dependency    Inversion:     Dependencies are provided externally...
The Service Locator Pattern   The service locator pattern is a design    pattern used in software development to    encap...
The Service Locator Pattern   For some reasons many people think that    there is no relationship between Service    Loca...
Plugin   Use your brains!
DIP Violations   Use of “new”
DIP Violations   Use of “static”public class SecurityService{    public static User GetCurrentUser()    {        // do so...
DIP Violations   Use of singletons using “static”public class ProductCache{    private static readonly _instance = new Pr...
What is a DI (IoC) Container?   Creates objects that are ready for you to use   Knows how to create objects and their   ...
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 InjectionInjecting a ICustomerRepository as well as an integer dependency.
Type 2 or Setter Injection   Injecting a ICustomerRepository through the setter.
Type 3 or Constructor InjectionInjecting a ICustomerRepository and a ICustomerDTOMapper throughthe constructor.Note: This ...
Dependency Injection Pros &Cons   Pros       Loosely Coupled       Increases Testability (A LOT!)       Separates comp...
*ILITIES    Agility    Testability    Reversibility    Changeability    Flexibility    Maintainability
Resource   Probably the best place to read is Martin    Fowler - Inversion of Control Containers and    the Dependency In...
Upcoming SlideShare
Loading in...5
×

Dependency Inversion Principle

3,243

Published on

Dependency Inversion - Design Principle

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,243
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
55
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Dependency Inversion Principle

  1. 1. Dependency Inversion Principle Depend on abstractions, not on concretions. Shahriar Hyder Kaz Software Ltd. 29th September, 2011
  2. 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. 3. Dependency Inversion PrincipleHigh level modules should not depend onlow level modules. Both should depend onabstractions.Abstractions should not depend on details.Details should depend on abstractions.
  4. 4. Depend on abstractions, not onconcretions
  5. 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. 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. 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. 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. 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. 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. 11. Plugin Use your brains!
  12. 12. DIP Violations Use of “new”
  13. 13. DIP Violations Use of “static”public class SecurityService{ public static User GetCurrentUser() { // do something }}
  14. 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. 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. 16. DI Containers Spring Framework Unity (Microsoft) Windsor (Castle Project) Ninject StructureMap
  17. 17. Types of Dependency Injection  Constructor (Most popular)  Setter  Method
  18. 18. Type 1 or interface injection /Method InjectionInjecting a ICustomerRepository as well as an integer dependency.
  19. 19. Type 2 or Setter Injection Injecting a ICustomerRepository through the setter.
  20. 20. Type 3 or Constructor InjectionInjecting a ICustomerRepository and a ICustomerDTOMapper throughthe constructor.Note: This is the most popular type of injection.
  21. 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. 22. *ILITIES  Agility  Testability  Reversibility  Changeability  Flexibility  Maintainability
  23. 23. Resource Probably the best place to read is Martin Fowler - Inversion of Control Containers and the Dependency Injection pattern
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×