Dependency injection

30
Dependency Injection (DI) Or stop cooking spaghetti (code)

Transcript of Dependency injection

Page 1: Dependency injection

Dependency Injection (DI)

Or stop cooking spaghetti (code)

Page 2: Dependency injection

1

“Dependency Injection

is a key element

of agile architecture”

Ward Cunningham

Page 3: Dependency injection

Dependency Injection (DI) is not a pattern, but rather a set of tools

2

Page 4: Dependency injection

DI is one of the ways to apply Inversion of Control (IoC) principle

3

Inversion of Control

Dependency

Injection

Page 5: Dependency injection

Literally – let go off control and let someone feed you

4

Page 6: Dependency injection

Example Task

Obviously its time to go to code . Check out NoDI project. 5

Page 7: Dependency injection

Specifications are singing rigoletto

6

Page 8: Dependency injection

Specs tend to change

7

Page 9: Dependency injection

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.

Page 10: Dependency injection

Divide et impera

9

DI’s base is Interface Oriented Programming. Write your code against abstractions rather then concrete implementations.

Page 11: Dependency injection

Single Responsibility Principle (SRP)

10

By applying DI to class we let it “concentrate” on task it was designed for.

Page 12: Dependency injection

Modularity

Go on and check this stuff applied in PoorMansDI project. 11

• Replace;• Reuse;• Build in parallel;• Easier to maintain;• Easier to unit-test.

Page 13: Dependency injection

DI patterns.

12

Page 14: Dependency injection

Composition Root

13

Page 15: Dependency injection

Constructor injection

14

Page 16: Dependency injection

Property (setter) injection

15

Page 17: Dependency injection

Method injection

16

Page 18: Dependency injection

Ambient context

17

Page 19: Dependency injection

Which one to choose?

18

Page 20: Dependency injection

3D of DI

19

Page 21: Dependency injection

1. Composition

20

Page 22: Dependency injection

2. Lifetime Management

21

• Transient (per call);• Singleton;• Per thread;• Per web request;• Per web session;• Pooled;

Page 23: Dependency injection

3. Interception

22

Page 24: Dependency injection

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.

Page 25: Dependency injection

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

Page 26: Dependency injection

Pros?

25

Simplified component architecture;Easier to maintain;Replace;Reuse;Build in parallel;Easier to unit-test.

Page 27: Dependency injection

Cons?

26

Have to learn something new?Lower performance? (it depends)It might be hard to fix lifetime errors.

Page 28: Dependency injection

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.

Page 29: Dependency injection

Thank you!

28

Page 30: Dependency injection

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