Dependency Injection vs Service Locator - Best Practice

download Dependency Injection vs Service Locator - Best Practice

of 42

  • date post

    27-May-2015
  • Category

    Technology

  • view

    6.190
  • download

    4

Embed Size (px)

description

Lars-Erik Kindblad presentation at the Capgemini Microsoft User Group 2012.

Transcript of Dependency Injection vs Service Locator - Best Practice

  • 1. Dependency Injection vs. Service Locator- Best PracticeLars-Erik KindbladSenior ConsultantBlog: kindblad.com

2. Agenda Intro Inversion of Control Dependency Injection Patterns Service Locator Patterns Inversion of Control Containers Best Practice What patterns to use When to use the patterns Summary | Sector, Alliance, Offering 3. Inversion of Control Inversion of Control = IOC Make code loosely coupled Make unit testing possible How? Move creation of dependencies outside the class they arebeing used in A better name - Inversion of Dependency Creation | Sector, Alliance, Offering 4. Traditional CodeInversion of Control | Sector, Alliance, Offering 5. Traditional CodeInversion of Control Code| Sector, Alliance, Offering 6. Traditional Code | Sector, Alliance, Offering 7. Dependency Injection - Constructor Injection | Sector, Alliance, Offering 8. Dependency Injection - Interface Injection | Sector, Alliance, Offering 9. Dependency Injection - Setter Injection| Sector, Alliance, Offering 10. Static Service Locator | Sector, Alliance, Offering 11. Injected Service Locator | Sector, Alliance, Offering 12. Inversion of Control Container A framework that can automatically create a given type with all therequired dependencies Popular frameworks Unity Castle Windsor Ninject StructureMap etc.| Sector, Alliance, Offering 13. Inversion of Control Container Manual approachUsing an IOC Container | Sector, Alliance, Offering 14. | Sector, Alliance, Offering 15. Which patterns to use 16. Traditional Code | Sector, Alliance, Offering 17. Dependency Injection - Constructor Injection | Sector, Alliance, Offering 18. Dependency Injection - Interface Injection | Sector, Alliance, Offering 19. Dependency Injection - Setter Injection| Sector, Alliance, Offering 20. Static Service Locator | Sector, Alliance, Offering 21. Injected Service Locator | Sector, Alliance, Offering 22. When to use which patterns | Sector, Alliance, Offering 23. Always use Dependency Injection Constructor Injection...except | Sector, Alliance, Offering 24. FACADE CLASSES | Sector, Alliance, Offering 25. Facade Classes | Sector, Alliance, Offering 26. Dependency Injection | Sector, Alliance, Offering 27. Service Locator| Sector, Alliance, Offering 28. LOOPS| Sector, Alliance, Offering 29. Loops| Sector, Alliance, Offering 30. Dependency Injection | Sector, Alliance, Offering 31. Service Locator| Sector, Alliance, Offering 32. Dependency Injection using Func| Sector, Alliance, Offering 33. BASE CLASSES | Sector, Alliance, Offering 34. Base Classes | Sector, Alliance, Offering 35. Dependency Injection | Sector, Alliance, Offering 36. Service Locator| Sector, Alliance, Offering 37. UNKNOWN TYPES AT COMPILE TIME| Sector, Alliance, Offering 38. Unknown types at compile time| Sector, Alliance, Offering 39. Service Locator| Sector, Alliance, Offering 40. SummaryConstructor Injection Injected Service LocatorFacade classesX(WCF Services,MVC Controllers)Loops X (Func)XBase classesXUnknown types atXcompile timeAll other scenarios X| Sector, Alliance, Offering 41. QUESTIONS? | Sector, Alliance, Offering 42. www.capgemini.comThe information contained in this presentation is proprietary. 2010 Capgemini. All rights reserved