true loosely coupled dependency injection

24
True Loosely Coupled Dependency Injection

Upload: teksystems-montreal-solution-center

Post on 27-Jan-2015

114 views

Category:

Technology


2 download

DESCRIPTION

Good practices and common misconceptions about Dependency Injection. By Sergio Romero, Software Developer [email protected] Blog: http://sergeromerosoftware.wordpress.com

TRANSCRIPT

Page 1: True Loosely Coupled Dependency Injection

True Loosely Coupled

Dependency Injection

Page 2: True Loosely Coupled Dependency Injection

About me

Sergio Romero

[email protected]

Blog: http://sergeromerosoftware.wordpress.com

2

Page 3: True Loosely Coupled Dependency Injection

Agenda

• Inversion of Control

• Dependency Injection

• Anti-patterns

• Patterns

• Composition Root

• Examples

Page 4: True Loosely Coupled Dependency Injection

4

Inversion of Control

Page 5: True Loosely Coupled Dependency Injection

5

Dependency Injection

Page 6: True Loosely Coupled Dependency Injection

6

• Commonly used interchangeably as synonyms.

• Dependency Injection is a way to implement Inversion of Control.

Inversion of Control vs. Dependency

Injection

Page 7: True Loosely Coupled Dependency Injection

7

• Late binding

• Extensibility

• Maintainability

• Parallel development

• Testability

Dependency Injection Benefits

Page 8: True Loosely Coupled Dependency Injection

8

• Service Locator

• Bastard Injection

Dependency Injection Anti-Patterns

Page 9: True Loosely Coupled Dependency Injection

9

• First described by Martin Fowler.

• It is basically a static Dictionary that contains all the definitions of

the objects to use:

Service Locator

Page 10: True Loosely Coupled Dependency Injection

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?

Page 11: True Loosely Coupled Dependency Injection

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

Page 12: True Loosely Coupled Dependency 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?

Page 13: True Loosely Coupled Dependency Injection

13

• Constructor Injection

• Property (Setter) Injection

• Method Injection

Dependency Injection Patterns

Page 14: True Loosely Coupled Dependency Injection

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

Page 15: True Loosely Coupled Dependency Injection

Property Injection

• Should only be used when a good local default is available.

15

Page 16: True Loosely Coupled Dependency Injection

Method Injection

• Best used when implementation varies between calls to the

method.

16

Page 17: True Loosely Coupled Dependency Injection

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

Page 18: True Loosely Coupled Dependency Injection

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

Page 19: True Loosely Coupled Dependency Injection

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.

Page 20: True Loosely Coupled Dependency Injection

20

• Some of the most common DI Containers are:

> Castle Windsor

> Structure Map

> Spring .Net

> Autofac

> Unity

> MEF

DI or IoC Containers

Page 21: True Loosely Coupled Dependency Injection

21

Composition Root with Poor Man’s

DI Example

Page 22: True Loosely Coupled Dependency Injection

22

Composition Root with IoC

Container Example

Page 23: True Loosely Coupled Dependency Injection

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

Page 24: True Loosely Coupled Dependency Injection

Questions?