True Loosely Coupled Dependency Injection
-
Upload
teksystems-montreal-solution-center -
Category
Technology
-
view
123 -
download
2
description
Transcript of True Loosely Coupled Dependency Injection
![Page 1: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/1.jpg)
True Loosely Coupled
Dependency Injection
![Page 2: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/2.jpg)
About me
Sergio Romero
Blog: http://sergeromerosoftware.wordpress.com
2
![Page 3: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/3.jpg)
Agenda
• Inversion of Control
• Dependency Injection
• Anti-patterns
• Patterns
• Composition Root
• Examples
![Page 4: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/4.jpg)
4
Inversion of Control
![Page 5: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/5.jpg)
5
Dependency Injection
![Page 6: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/7.jpg)
7
• Late binding
• Extensibility
• Maintainability
• Parallel development
• Testability
Dependency Injection Benefits
![Page 8: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/8.jpg)
8
• Service Locator
• Bastard Injection
Dependency Injection Anti-Patterns
![Page 9: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/13.jpg)
13
• Constructor Injection
• Property (Setter) Injection
• Method Injection
Dependency Injection Patterns
![Page 14: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/15.jpg)
Property Injection
• Should only be used when a good local default is available.
15
![Page 16: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/16.jpg)
Method Injection
• Best used when implementation varies between calls to the
method.
16
![Page 17: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/21.jpg)
21
Composition Root with Poor Man’s
DI Example
![Page 22: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/22.jpg)
22
Composition Root with IoC
Container Example
![Page 23: True Loosely Coupled Dependency Injection](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022051412/54c6f6c24a7959c5088b458a/html5/thumbnails/24.jpg)
Questions?