Dependency Injection What, Why and How - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Dependency Injection What, Why and How

Description:

... years! A Real World Example? Web App. Stock Quotes. Authenticator ... quotes = new StockQuotes(); authenticator = new Authenticator(); database = new Database ... – PowerPoint PPT presentation

Number of Views:191
Avg rating:3.0/5.0
Slides: 20
Provided by: brad179
Category:

less

Transcript and Presenter's Notes

Title: Dependency Injection What, Why and How


1
Dependency InjectionWhat, Why and How
  • Peter Provost
  • peter.provost_at_microsoft.com
  • http//www.peterprovost.org/
  • Slides will be here

Brad Wilson bradwils_at_microsoft.com http//agilepro
grammer.com/dotnetguy
2
What is Dependency Injection?
  • DI is all about wiring up objects
  • Come on, that isnt hard. Weve been doing that
    for years!

3
A Real World Example?
Web App
Stock Quotes
Authenticator
Error Handler
Logger
Database
This example was originally created by Jim
Weirich in Ruby on his blog. See his original
article at http//onestepback.org/index.cgi/Tech/R
uby/DependencyInjectionInRuby.rdoc
4
The Old Way
  • public class WebApp
  • public WebApp()
  • quotes new StockQuotes()
  • authenticator new Authenticator()
  • database new Database()
  • logger new Logger()
  • errorHandler new ErrorHandler()
  • // More code here...

5
What about the child objects?
  • How does the StockQuotes find the Logger?
  • How does the Authenticator find the database?
  • Etc.?
  • Suppose you want to use a TestingLogger instead?
    Or a MockDatabase?

6
Service Locator Interface
  • public interface ILocator
  • TObject GetltTObjectgt()

7
Service Locator Example
  • public class MyLocator ILocator
  • protected DictionaryltType, objectgt dict new
    DictionaryltType,objectgt()
  • public MyLocator()
  • dict.Add(typeof(ILogger), new Logger())
  • dict.Add(typeof(IErrorHandler), new
    ErrorHandler(this))
  • dict.Add(typeof(IQuotes), new StockQuotes(this))
  • dict.Add(typeof(IDatabase), new Database(this))
  • dict.Add(typeof(IAuthenticator), new
    Authenticator(this))
  • dict.Add(typeof(WebApp), new WebApp(this))

8
StockQuotes with Locator
  • public class StockQuotes
  • public StockQuotes(ILocator locator)
  • errorHandler locator.GetltIErrorHandlergt()
  • logger locator.GetltILoggergt()
  • // More code here...

9
Good things
  • Classes are decoupled from explicit imlementation
    types
  • Easy to externalize the config

10
Bad things
  • Everyone takes a dependency on the ILocator
  • Hard to store constants and other useful
    primitives
  • Creation order is still a problem

11
Dependency Injection Containers
  • Gets rid of the dependency on the ILocator
  • Object is no longer responsible for finding its
    dependencies
  • The container does it for you

12
Then what?
  • Write your objects the way you want
  • Setup the container
  • Ask the container for objects
  • The container creates objects for you and
    fulfills dependencies

13
Setting Up the Container (Code)
  • DIContainer container new DIContainer()
  • container.RegisterltILogger, Loggergt()
  • container.RegisterltIDatabase, Databasegt()
  • container.RegisterltIErrorHandler,
    ErrorHandlergt()
  • container.RegisterltIQuotes, StockQuotesgt()
  • container.RegisterltIAuthenticator,
    Authenticatorgt()
  • container.RegisterltIWebApp, WebAppgt()

14
Setting Up the Container (XML)
  • ltDIContainergt
  • ltTypeMap fromILogger toLogger /gt
  • ltTypeMap fromIDatabase toDatabase /gt
  • ltTypeMap fromIErrorHandler toErrorHandler
    /gt
  • ltTypeMap fromIQuotes toStockQuotes /gt
  • ltTypeMap fromIAuthenticator toAuthenticator
    /gt
  • lt/DIContainergt

15
Creating Objects
  • Test
  • public void WebAppTest()
  • DIContainer container GetContainer()
  • IStockQuotes quotes container.GetltIStockQuotes
    gt()
  • IAuthenticator auth container.GetltIAuthenticat
    orgt()
  • Assert.IsNotNull( quotes.Logger )
  • Assert.IsNotNull( auth.Logger )
  • Assert.AreSame( quotes.Logger, auth.Logger )

16
The Possibilities are Endless?
  • To Singleton or Not to Singleton?
  • Nested Containers
  • Property Setter
  • Object Lifetime
  • Method Invocation
  • Event Wire-up
  • Instrumentation
  • Method Interception via Dynamic Proxies

17
Existing Frameworks
  • Java
  • Pico Container
  • Spring Framework
  • HiveMind
  • Ruby
  • Rico
  • Copland
  • Python
  • PyContainer
  • .NET
  • Pico.NET
  • Spring.NET
  • pp Object Builder

18
Demo
  • patterns practices Object Builder

19
Looking for a Job?
  • patterns practices is hiring
  • See one of us after the talk or e-mailDarrel
    Snow dsnow_at_microsoft.com
Write a Comment
User Comments (0)
About PowerShow.com