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.
Dependency Injection vs. Service Locator                      - Best PracticeLars-Erik KindbladSenior ConsultantBlog: kind...
Agenda Intro  •   Inversion of Control  •   Dependency Injection Patterns  •   Service Locator Patterns  •   Inversion of...
Inversion of Control Inversion of Control = IOC Make code loosely coupled Make unit testing possible How? Move creatio...
Traditional CodeInversion of Control                       | Sector, Alliance, Offering
Traditional CodeInversion of Control Code                            | Sector, Alliance, Offering
Traditional Code                   | Sector, Alliance, Offering
Dependency Injection - Constructor Injection                                               | Sector, Alliance, Offering
Dependency Injection - Interface Injection                                             | Sector, Alliance, Offering
Dependency Injection - Setter Injection                                          | Sector, Alliance, Offering
Static Service Locator                         | Sector, Alliance, Offering
Injected Service Locator                           | Sector, Alliance, Offering
Inversion of Control Container A framework that can automatically create a given type with all the  required dependencies...
Inversion of Control Container         Manual approach      Using an IOC Container                                 | Secto...
| Sector, Alliance, Offering
Which patterns to use
Traditional Code                   | Sector, Alliance, Offering
Dependency Injection - Constructor Injection                                               | Sector, Alliance, Offering
Dependency Injection - Interface Injection                                             | Sector, Alliance, Offering
Dependency Injection - Setter Injection                                          | Sector, Alliance, Offering
Static Service Locator                         | Sector, Alliance, Offering
Injected Service Locator                           | Sector, Alliance, Offering
When to use which patterns                   | Sector, Alliance, Offering
Always use Dependency Injection – Constructor Injection                      ...except                                    ...
FACADE CLASSES                 | Sector, Alliance, Offering
Facade Classes                 | Sector, Alliance, Offering
Dependency Injection                       | Sector, Alliance, Offering
Service Locator                  | Sector, Alliance, Offering
LOOPS        | Sector, Alliance, Offering
Loops        | Sector, Alliance, Offering
Dependency Injection                       | Sector, Alliance, Offering
Service Locator                  | Sector, Alliance, Offering
Dependency Injection using Func<>                                    | Sector, Alliance, Offering
BASE CLASSES               | Sector, Alliance, Offering
Base Classes               | Sector, Alliance, Offering
Dependency Injection                       | Sector, Alliance, Offering
Service Locator                  | Sector, Alliance, Offering
UNKNOWN TYPES AT COMPILE TIME                                | Sector, Alliance, Offering
Unknown types at compile time                                | Sector, Alliance, Offering
Service Locator                  | Sector, Alliance, Offering
Summary                      Constructor Injection   Injected Service LocatorFacade classes                               ...
QUESTIONS?             | Sector, Alliance, Offering
www.capgemini.comThe information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved
Upcoming SlideShare
Loading in …5
×

Dependency Injection vs Service Locator - Best Practice

9,798 views

Published on

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

Published in: Technology, Business

Dependency Injection vs Service Locator - Best Practice

  1. 1. Dependency Injection vs. Service Locator - Best PracticeLars-Erik KindbladSenior ConsultantBlog: kindblad.com
  2. 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. 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 are being used in A better name - Inversion of Dependency Creation | Sector, Alliance, Offering
  4. 4. Traditional CodeInversion of Control | Sector, Alliance, Offering
  5. 5. Traditional CodeInversion of Control Code | Sector, Alliance, Offering
  6. 6. Traditional Code | Sector, Alliance, Offering
  7. 7. Dependency Injection - Constructor Injection | Sector, Alliance, Offering
  8. 8. Dependency Injection - Interface Injection | Sector, Alliance, Offering
  9. 9. Dependency Injection - Setter Injection | Sector, Alliance, Offering
  10. 10. Static Service Locator | Sector, Alliance, Offering
  11. 11. Injected Service Locator | Sector, Alliance, Offering
  12. 12. Inversion of Control Container A framework that can automatically create a given type with all the required dependencies Popular frameworks • Unity • Castle Windsor • Ninject • StructureMap • etc. | Sector, Alliance, Offering
  13. 13. Inversion of Control Container Manual approach Using an IOC Container | Sector, Alliance, Offering
  14. 14. | Sector, Alliance, Offering
  15. 15. Which patterns to use
  16. 16. Traditional Code | Sector, Alliance, Offering
  17. 17. Dependency Injection - Constructor Injection | Sector, Alliance, Offering
  18. 18. Dependency Injection - Interface Injection | Sector, Alliance, Offering
  19. 19. Dependency Injection - Setter Injection | Sector, Alliance, Offering
  20. 20. Static Service Locator | Sector, Alliance, Offering
  21. 21. Injected Service Locator | Sector, Alliance, Offering
  22. 22. When to use which patterns | Sector, Alliance, Offering
  23. 23. Always use Dependency Injection – Constructor Injection ...except | Sector, Alliance, Offering
  24. 24. FACADE CLASSES | Sector, Alliance, Offering
  25. 25. Facade Classes | Sector, Alliance, Offering
  26. 26. Dependency Injection | Sector, Alliance, Offering
  27. 27. Service Locator | Sector, Alliance, Offering
  28. 28. LOOPS | Sector, Alliance, Offering
  29. 29. Loops | Sector, Alliance, Offering
  30. 30. Dependency Injection | Sector, Alliance, Offering
  31. 31. Service Locator | Sector, Alliance, Offering
  32. 32. Dependency Injection using Func<> | Sector, Alliance, Offering
  33. 33. BASE CLASSES | Sector, Alliance, Offering
  34. 34. Base Classes | Sector, Alliance, Offering
  35. 35. Dependency Injection | Sector, Alliance, Offering
  36. 36. Service Locator | Sector, Alliance, Offering
  37. 37. UNKNOWN TYPES AT COMPILE TIME | Sector, Alliance, Offering
  38. 38. Unknown types at compile time | Sector, Alliance, Offering
  39. 39. Service Locator | Sector, Alliance, Offering
  40. 40. Summary Constructor Injection Injected Service LocatorFacade classes X(WCF Services,MVC Controllers)Loops X (Func<>) XBase classes XUnknown types at Xcompile timeAll other scenarios X | Sector, Alliance, Offering
  41. 41. QUESTIONS? | Sector, Alliance, Offering
  42. 42. www.capgemini.comThe information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved

×