Dependency Injection vs Service Locator - Best Practice
-
Upload
lars-erik-kindblad -
Category
Technology
-
view
6.192 -
download
4
description
Transcript of Dependency Injection vs Service Locator - Best Practice
Lars-Erik KindbladSenior ConsultantBlog: kindblad.com
Dependency Injection vs. Service Locator- Best Practice
| Sector, Alliance, Offering
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
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
Traditional Code
Inversion of Control
| Sector, Alliance, Offering
Traditional Code
Inversion 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
Popular frameworks• Unity• Castle Windsor• Ninject• StructureMap• etc.
| Sector, Alliance, Offering
Inversion of Control Container
Manual approach
Using an IOC Container
| Sector, Alliance, Offering
Which patterns to use
| 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
When to use which patterns
| Sector, Alliance, Offering
Always use Dependency Injection – Constructor Injection
...except
| Sector, Alliance, Offering
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 Locator
Facade classes(WCF Services,MVC Controllers)
X
Loops X (Func<>) X
Base classes X
Unknown types at compile time
X
All other scenarios X
| Sector, Alliance, Offering
QUESTIONS?
www.capgemini.com
The information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved