Sorin Damian - Using and Abusing Dependency Injection Containers

19
Using and Abusing Dependency Injection Containers Sorin Damian SC RomSoft SRL (rms.ro) [10th of November 2012]

Transcript of Sorin Damian - Using and Abusing Dependency Injection Containers

Page 1: Sorin Damian - Using and Abusing Dependency Injection Containers

Using and Abusing

Dependency Injection

ContainersSorin Damian

SC RomSoft SRL (rms.ro)

[10th of November 2012]

Page 2: Sorin Damian - Using and Abusing Dependency Injection Containers

Why do YOU use it?

Page 3: Sorin Damian - Using and Abusing Dependency Injection Containers

UGLY DEMO

Page 4: Sorin Damian - Using and Abusing Dependency Injection Containers

The Inversion of Control Principle

Don’t ask for information

Ask for help

Page 5: Sorin Damian - Using and Abusing Dependency Injection Containers

IoC – Don’t call us, we’ll call you

Page 6: Sorin Damian - Using and Abusing Dependency Injection Containers

IoC for 5 year olds

When you go and get things out of the

refrigerator for yourself, you can cause problems.

You might leave the door open, you might get

something Mommy or Daddy doesn't want you to

have. You might even be looking for something

we don't even have or which has expired.

What you should be doing is stating a need, "I

need something to drink with lunch," and then we

will make sure you have something when you sit

down to eat. http://stackoverflow.com/questions/1638919/how-to-explain-dependency-injection-to-a-5-year-old

Page 7: Sorin Damian - Using and Abusing Dependency Injection Containers

NICER BUT STILL

UGLY DEMO

Page 8: Sorin Damian - Using and Abusing Dependency Injection Containers

Dependency Injection

Design pattern

The Inversion of Control principle

applied to dependency management

Ask for dependencies

Instantiating them is a separate concern

Page 9: Sorin Damian - Using and Abusing Dependency Injection Containers

What you get

+ decupling

+ reusability

+ testability

- more code

- more ceremony

- composition hell

Page 10: Sorin Damian - Using and Abusing Dependency Injection Containers
Page 11: Sorin Damian - Using and Abusing Dependency Injection Containers

DEMO (15 lines container)

Page 12: Sorin Damian - Using and Abusing Dependency Injection Containers

DEMO (using the container)

Page 13: Sorin Damian - Using and Abusing Dependency Injection Containers

If it looks like a factory..

And it works like a factory..

Page 14: Sorin Damian - Using and Abusing Dependency Injection Containers
Page 15: Sorin Damian - Using and Abusing Dependency Injection Containers

Call the container only at

the composition root!

Page 16: Sorin Damian - Using and Abusing Dependency Injection Containers

Every time you call the container to

get your dependencies a kitten dies..

Page 17: Sorin Damian - Using and Abusing Dependency Injection Containers

Any questions?

Page 18: Sorin Damian - Using and Abusing Dependency Injection Containers

Thank you!

Page 19: Sorin Damian - Using and Abusing Dependency Injection Containers

Diamond Sponsors

Platinum Sponsors Gold Sponsors

Training Partners Media Partners Other Partners