The Dependency |---####>-----

         Sorin DAMIAN
Agenda
•   Dependency injection explained
•   SOLID again 
•   Loose coupling & high cohesion
•   Example
•   Containers
•   Configuration
•   Patterns and anti-patterns
•   Unit testing with auto-mocking containers
Dependency Injection &
             Inversion of Control
• Pt copii de 5 ani
   – Nu te bagi direrect in frigider
   – Ceri exact ce ai nevoie si altcineva (mami si tati) iti
     ofera de mancare
• Pt adulti
   – The mafia principle: Don’t call us.. We’ll call U!
• Pt programatori
   – O metoda care calculeaza totalul unei facturi nu
     trebuie sa extraga fiecare item din baza de date. Este
     de ajuns doar sa ceara pretul pentru fiecare item.
     Responsabilitatea citirii din db revine caller-ului..
SOLID
•   S – Single Responsibility Principle
•   O – Open/Closed Principle
•   L – Liskov Substitution Principle
•   I – Interface Segregation Principle
•   D – dependency inversion
       • High level modules should not depend upon low level
         modules. Both should depend upon abstractions.
       • Abstractions should not depend upon details. Details
         should depend upon abstractions.
Tight coupling
Loose coupling & high cohesion
Example
Example
Dependency Injection
• Dependency Injection is Inversion of Control
  applied to dependency management
  – Ask for dependencies using constructor
    parameters, properties and method arguments
  – Always leave the responsibility of instantiating
    dependencies to the caller
• Side effect
  – Nobody wants to be the top caller (dependency
    injection hell)
Dependency Injection




DEMO
The DI container
• Solution: the DI container
• Step1 - Unlearning StructureMap
  – If you think it’s a factory on steroids, you’re using
    it the wrong way!
• Step 2 – wire-up the container
• Step.. DONE 
DI/IoC Containers
•   Poor man’s dependency injection
•   Do it yourself
•   StructureMap
•   Castle Windsor
•   Spring.NET
•   LinFu
•   Unity
•   Autofac
•   NInject
•   Managed Extensibility Framework (MEF)
Configuration
• Sources
    – XML
    – Attributes
    – Registry DSL
•   Object lifetime
•   Custom instances
•   Named instances
•   Auto registration and type scanning
    – Registries
    – Conventions
• Interception
Object Lifetime
•   PerRequest
•   Unique
•   Singleton
•   HttpContext
•   ThreadLocal
•   Hybrid
•   HttpSession
•   HybridHttpSession
Configuration




DEMO
Anti-patterns
•   Control Freak
•   Bastard Injection
•   Constrained construction
•   Service locator
Service Locator anti-pattern
Patterns
•   Constructor injection
•   Property injection
•   Method injection
•   What to inject and what NOT!
    – Unstable dependencies
       • Doesn’t yet exist
       • Machine/environment specific
       • Nondeterministic
    – No need to inject stable dependencies
       • Already exist, behave deterministically, will not be replaced
Auto-mocking containers




DEMO
Done done

The Dependency Injection - Sorin Damian, Software Developer@RomSoft

  • 1.
  • 2.
    Agenda • Dependency injection explained • SOLID again  • Loose coupling & high cohesion • Example • Containers • Configuration • Patterns and anti-patterns • Unit testing with auto-mocking containers
  • 3.
    Dependency Injection & Inversion of Control • Pt copii de 5 ani – Nu te bagi direrect in frigider – Ceri exact ce ai nevoie si altcineva (mami si tati) iti ofera de mancare • Pt adulti – The mafia principle: Don’t call us.. We’ll call U! • Pt programatori – O metoda care calculeaza totalul unei facturi nu trebuie sa extraga fiecare item din baza de date. Este de ajuns doar sa ceara pretul pentru fiecare item. Responsabilitatea citirii din db revine caller-ului..
  • 4.
    SOLID • S – Single Responsibility Principle • O – Open/Closed Principle • L – Liskov Substitution Principle • I – Interface Segregation Principle • D – dependency inversion • High level modules should not depend upon low level modules. Both should depend upon abstractions. • Abstractions should not depend upon details. Details should depend upon abstractions.
  • 5.
  • 6.
    Loose coupling &high cohesion
  • 7.
  • 8.
  • 9.
    Dependency Injection • DependencyInjection is Inversion of Control applied to dependency management – Ask for dependencies using constructor parameters, properties and method arguments – Always leave the responsibility of instantiating dependencies to the caller • Side effect – Nobody wants to be the top caller (dependency injection hell)
  • 10.
  • 11.
    The DI container •Solution: the DI container • Step1 - Unlearning StructureMap – If you think it’s a factory on steroids, you’re using it the wrong way! • Step 2 – wire-up the container • Step.. DONE 
  • 12.
    DI/IoC Containers • Poor man’s dependency injection • Do it yourself • StructureMap • Castle Windsor • Spring.NET • LinFu • Unity • Autofac • NInject • Managed Extensibility Framework (MEF)
  • 13.
    Configuration • Sources – XML – Attributes – Registry DSL • Object lifetime • Custom instances • Named instances • Auto registration and type scanning – Registries – Conventions • Interception
  • 14.
    Object Lifetime • PerRequest • Unique • Singleton • HttpContext • ThreadLocal • Hybrid • HttpSession • HybridHttpSession
  • 15.
  • 16.
    Anti-patterns • Control Freak • Bastard Injection • Constrained construction • Service locator
  • 17.
  • 18.
    Patterns • Constructor injection • Property injection • Method injection • What to inject and what NOT! – Unstable dependencies • Doesn’t yet exist • Machine/environment specific • Nondeterministic – No need to inject stable dependencies • Already exist, behave deterministically, will not be replaced
  • 19.
  • 20.

Editor's Notes

  • #4 http://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5-year-old
  • #5 ISP - Clients should not be forced to depend upon interfaces that they don't use. - Many client specific interfaces are better than one general purpose interface??
  • #10 ExemplusimpluCOPIER