The Dependency Injection - Sorin Damian, Software Developer@RomSoft

1,369 views
1,282 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
1,369
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • http://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5-year-old
  • 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??
  • ExemplusimpluCOPIER
  • The Dependency Injection - Sorin Damian, Software Developer@RomSoft

    1. 1. The Dependency |---####>----- Sorin DAMIAN
    2. 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. 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. 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. 5. Tight coupling
    6. 6. Loose coupling & high cohesion
    7. 7. Example
    8. 8. Example
    9. 9. 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)
    10. 10. Dependency InjectionDEMO
    11. 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. 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. 13. Configuration• Sources – XML – Attributes – Registry DSL• Object lifetime• Custom instances• Named instances• Auto registration and type scanning – Registries – Conventions• Interception
    14. 14. Object Lifetime• PerRequest• Unique• Singleton• HttpContext• ThreadLocal• Hybrid• HttpSession• HybridHttpSession
    15. 15. ConfigurationDEMO
    16. 16. Anti-patterns• Control Freak• Bastard Injection• Constrained construction• Service locator
    17. 17. Service Locator anti-pattern
    18. 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. 19. Auto-mocking containersDEMO
    20. 20. Done done

    ×