Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dependency Injection What, Why and How Peter Provost Slides will be here ^^^ Brad Wilson

Similar presentations


Presentation on theme: "Dependency Injection What, Why and How Peter Provost Slides will be here ^^^ Brad Wilson"— Presentation transcript:

1 Dependency Injection What, Why and How Peter Provost peter.provost@microsoft.com http://www.peterprovost.org/ Slides will be here ^^^ Brad Wilson bradwils@microsoft.com http://agileprogrammer.com/dotnetguy

2 What is Dependency Injection? DI is all about wiring up objects Come on, that isn’t hard. We’ve been doing that for years!

3 A Real World Example? Web App Stock QuotesAuthenticator Error HandlerLoggerDatabase This example was originally created by Jim Weirich in Ruby on his blog. See his original article at http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdochttp://onestepback.org/index.cgi/Tech/Ruby/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 Get (); }

7 Service Locator Example public class MyLocator : ILocator { protected Dictionary dict = new Dictionary (); 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.Get (); logger = locator.Get (); } // 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.Register ();

14 Setting Up the Container (XML)

15 Creating Objects [Test] public void WebAppTest() { DIContainer container = GetContainer(); IStockQuotes quotes = container.Get (); IAuthenticator auth = container.Get (); 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  p&p 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-mail Darrel Snow – dsnow@microsoft.comdsnow@microsoft.com


Download ppt "Dependency Injection What, Why and How Peter Provost Slides will be here ^^^ Brad Wilson"

Similar presentations


Ads by Google