Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Dependency Injection - Sorin Damian, Software Developer@RomSoft

1,477 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

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

×