Dependency injection
-
Upload
boris-tveritnev -
Category
Technology
-
view
1.121 -
download
3
Transcript of Dependency injection
Dependency Injection (DI)
Or stop cooking spaghetti (code)
1
“Dependency Injection
is a key element
of agile architecture”
Ward Cunningham
Dependency Injection (DI) is not a pattern, but rather a set of tools
2
DI is one of the ways to apply Inversion of Control (IoC) principle
3
Inversion of Control
Dependency
Injection
Literally – let go off control and let someone feed you
4
Example Task
Obviously its time to go to code . Check out NoDI project. 5
Specifications are singing rigoletto
6
Specs tend to change
7
Highly coupled code is a step to spaghetti code
8
Can’t intercept and change code;
Can’t unit-test in isolation; Conceptual problems in
modules design; Logic is spread across many
parts.
Divide et impera
9
DI’s base is Interface Oriented Programming. Write your code against abstractions rather then concrete implementations.
Single Responsibility Principle (SRP)
10
By applying DI to class we let it “concentrate” on task it was designed for.
Modularity
Go on and check this stuff applied in PoorMansDI project. 11
• Replace;• Reuse;• Build in parallel;• Easier to maintain;• Easier to unit-test.
DI patterns.
12
Composition Root
13
Constructor injection
14
Property (setter) injection
15
Method injection
16
Ambient context
17
Which one to choose?
18
3D of DI
19
1. Composition
20
2. Lifetime Management
21
• Transient (per call);• Singleton;• Per thread;• Per web request;• Per web session;• Pooled;
3. Interception
22
DI containers
23
• Castle Windsor www.castleproject.org/container/index.html
• StructureMap http://structuremap.github.com/structuremap/index.html
• Spring.Net http://www.springframework.net/
• PikoContainer http://www.picocontainer.org/
• Ninject http://ninject.org/• Unity http://unity.codeplex.com/• PocoCapsule/C++
http://www.pocomatic.com• etc.
Ways to configure containers
See how to combine all of aforementioned styles in DIWithContainer project. 24
Style Description Pros Cons
Code as configuration
Final object graph is composed in code
Compile-time checks, high degree of control
Need to recompile to change
Configuration based
Composition in external file (xml)
No need to recompile to change something, high degree of control
Verbose, no compile-time checks
Auto-registration
Components are composed with use of conventions (Convention over configuration)
No need to recompile, easiest to apply
Limited control, no compile-time checks
Pros?
25
Simplified component architecture;Easier to maintain;Replace;Reuse;Build in parallel;Easier to unit-test.
Cons?
26
Have to learn something new?Lower performance? (it depends)It might be hard to fix lifetime errors.
Should I use it?
27
Do you know OOP and design patterns?Do you write unit-tests?Do you use statically typed languages?
First learn it.
Don’t force a team that doesn’t see DI pros to use it.
Thank you!
28
Materials
30
M. Seemann. Dependency Injection in .Net - Manning Publications Co., 2010.
R. Osherove. The art of Unit Testing - Manning Publications Co., 2009, ISBN: 978-933988-27-6.
J. Miller. Cohesion And Coupling – MSDN Magazine, Oct. 2008. http://msdn.microsoft.com/en-us/magazine/cc947917.aspx
F. Maulo. GuyWire. http://fabiomaulo.blogspot.com/2009/11/guywire.html
K. Kozmic. How I use Inversion of Control containers. http://kozmic.pl/archive/2010/06/20/how-i-use-inversion-of-control-containers.aspx