true loosely coupled dependency injection
DESCRIPTION
Good practices and common misconceptions about Dependency Injection. By Sergio Romero, Software Developer [email protected] Blog: http://sergeromerosoftware.wordpress.comTRANSCRIPT
True Loosely Coupled
Dependency Injection
About me
Sergio Romero
Blog: http://sergeromerosoftware.wordpress.com
2
Agenda
• Inversion of Control
• Dependency Injection
• Anti-patterns
• Patterns
• Composition Root
• Examples
4
Inversion of Control
5
Dependency Injection
6
• Commonly used interchangeably as synonyms.
• Dependency Injection is a way to implement Inversion of Control.
Inversion of Control vs. Dependency
Injection
7
• Late binding
• Extensibility
• Maintainability
• Parallel development
• Testability
Dependency Injection Benefits
8
• Service Locator
• Bastard Injection
Dependency Injection Anti-Patterns
9
• First described by Martin Fowler.
• It is basically a static Dictionary that contains all the definitions of
the objects to use:
Service Locator
10
• Maintainability and reusability are hindered since:
> Dependencies are hidden and could throw unexpected and
hard to debug exceptions if the dependency is not properly
configured.
> All the assemblies now have a hard dependency on the
service locator’s one.
• Testability is hindered since configuration is needed for each
testing assembly.
• Not really necessary for DI to exist or work.
Why is it an anti-pattern?
11
• Incorrectly confused with Poor Man’s DI.
• Typically the overloaded constructor is used for testing and the
default one for production code.
Bastard Injection
12
• Use of foreign default: The class drags along the dependant
assembly which may not be needed in another module.
• The benefits of loose coupling are lost. The foreign default
hinders the ability to reuse or replace the class and even the
whole module.
• It makes parallel development more difficult since now the class
has a strong dependency.
• A DI Container will not know which constructor to use.
Why is it an anti-pattern?
13
• Constructor Injection
• Property (Setter) Injection
• Method Injection
Dependency Injection Patterns
14
• The Dependency is exposed in the class’ constructor.
• Viewed in isolation makes it seem that the instantiation of the
dependency is just pushed up one level.
Constructor Injection
Property Injection
• Should only be used when a good local default is available.
15
Method Injection
• Best used when implementation varies between calls to the
method.
16
17
• Where should we compose object graphs? As close as possible
to the application’s entry point.
• A (preferably) unique location in an application where modules
are composed together.
• Even a very modular application that uses loose coupling and
late binding to compose itself has a root that contains the entry
point into the application.
The Composition Root
18
• Examples of composition roots:
> A console application is an executable (.exe) with a Main
method.
> An ASP.NET web application is a library (.dll) with an
Application_Start event handler in its Global.asax.
> A WPF application is an executable (.exe) with an App.xaml
file.
> A WCF service is a library (.dll) with a class that derives from a
service interface, although you can hook into a more low-level
entry point by creating a custom ServiceHostFactory.
The Composition Root
DI or IoC Containers
19
• A DI Container is a software library that can automate many
of the tasks involved in composing objects and managing
their lifetimes.
• Object composition may be achieved via Poor Man’s DI, but
the challenges in managing the object’s lifetimes makes a DI
Container an invaluable tool.
• Most DI Containers will handle the most life cycles.
20
• Some of the most common DI Containers are:
> Castle Windsor
> Structure Map
> Spring .Net
> Autofac
> Unity
> MEF
DI or IoC Containers
21
Composition Root with Poor Man’s
DI Example
22
Composition Root with IoC
Container Example
References http://en.wikipedia.org/wiki/Inversion_of_control
http://en.wikipedia.org/wiki/Dependency_Injection
http://martinfowler.com/articles/injection.html#InversionOfControl
http://martinfowler.com/bliki/InversionOfControl.html
http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-Pattern/
http://blogs.clariusconsulting.net/kzu/what-is-all-the-fuzz-about-the-
new-common-iservicelocator/
http://blogs.msdn.com/b/nblumhardt/archive/2008/12/27/container-
managed-application-design-prelude-where-does-the-container-
belong.aspx
http://ayende.com/blog/4092/reviewing-nerddinner
http://blog.ploeh.dk/2011/07/28/CompositionRoot/
Dependency Injection in .Net, Mark Seeman, Manning Publications
Questions?