Slide 1

Slide 1 text

What is Dependency Injection? Shawn Stratton 2nd June 2011—

Slide 2

Slide 2 text

What I’ll Cover 2 I will cover:

Slide 3

Slide 3 text

What I’ll Cover 2 I will cover: • What DI isn’t

Slide 4

Slide 4 text

What I’ll Cover 2 I will cover: • What DI isn’t • What DI is

Slide 5

Slide 5 text

What I’ll Cover 2 I will cover: • What DI isn’t • What DI is • Why you should use DI

Slide 6

Slide 6 text

What I’ll Cover 2 I will cover: • What DI isn’t • What DI is • Why you should use DI I won’t cover:

Slide 7

Slide 7 text

What I’ll Cover 2 I will cover: • What DI isn’t • What DI is • Why you should use DI I won’t cover: • How to implement DI in your project

Slide 8

Slide 8 text

What I’ll Cover 2 I will cover: • What DI isn’t • What DI is • Why you should use DI I won’t cover: • How to implement DI in your project • Differences in available containers

Slide 9

Slide 9 text

What I’ll Cover 2 I will cover: • What DI isn’t • What DI is • Why you should use DI I won’t cover: • How to implement DI in your project • Differences in available containers • How to cook awesome bacon (ask Jeff)

Slide 10

Slide 10 text

What is Dependency Injection not?

Slide 11

Slide 11 text

New 4 Figure 1: http://martinfowler.com/articles/injection.html

Slide 12

Slide 12 text

Magic 5 Figure 2: Merlin by One Luck Guy (Flickr)

Slide 13

Slide 13 text

Complex 6 Figure 3: Complexity 3 by Michael Heiss (Flickr)

Slide 14

Slide 14 text

So what is it?

Slide 15

Slide 15 text

A Design Style 8 db = $db ; $this−>dependency = $dependency ; } public function somefunc ( ) { / / Use Dependencies } }

Slide 16

Slide 16 text

Easy 9 defineSingleton( ’PDO ’ , $pdo) ; $dependant = $sl−>get( ’ Dependant ’ ) ; / / Dependant i s type of Dependant

Slide 17

Slide 17 text

About Components

Slide 18

Slide 18 text

Service Locator

Slide 19

Slide 19 text

Service Locator 12 Service Location is like ordering with substitutions, and having the waiter completely ignore the substitutions; you get what’s on the menu, nothing more, nothing less. Figure 4: Matthew Weier O‘Phinney on Service Locators

Slide 20

Slide 20 text

Service Locators Detail 13 • It’s a fancy registry. • Inject the locator into the class via contstructor, call the the locator to find your services. • Works, but it’s not foolproof

Slide 21

Slide 21 text

Containers

Slide 22

Slide 22 text

Another Analogy 15 Dependency Injection is like ordering off the menu – but specifying things like, ”I’d like to substitute portabella mushrooms for the patties, please.” The waiter then goes and brings your dish, which has portabella mushrooms instead of the hamburger patties listed on the menu. Figure 5: Matthew Weier O‘Phinney on DI Containers

Slide 23

Slide 23 text

DI Container Detail 16 • Still a fancy registry, basically just a Service Locator. • Instantiates new classes by resolving and injecting their dependencies. • Very Clean in regards to separation of concerns. • Not required to run the system (you can do this manually, trust me)

Slide 24

Slide 24 text

What are the benefits of Dependency Injection?

Slide 25

Slide 25 text

Makes Testing Easy 18 getMock( ’ Dependency ’ , array ( ’ someFunction ’ ) ) ; $dependency−>expects( $this−>once ( ) )−>method( ’ someFunction ’ ) ; $this−>dependant = new Dependant($pdo , $dependency) ; } }

Slide 26

Slide 26 text

Easy Extension 19 Steps to extend and use a class:

Slide 27

Slide 27 text

Easy Extension 19 Steps to extend and use a class: 1. Create class b and have it extend class a

Slide 28

Slide 28 text

Easy Extension 19 Steps to extend and use a class: 1. Create class b and have it extend class a 2. Change Mapping

Slide 29

Slide 29 text

Easy Extension 19 Steps to extend and use a class: 1. Create class b and have it extend class a 2. Change Mapping 3. Profit!

Slide 30

Slide 30 text

What are the costs?

Slide 31

Slide 31 text

Enforces Interfaces 21

Slide 32

Slide 32 text

Mapping Files 22 array ( ’ class ’ => ’ Zend Foo ’ , ’ arguments ’ => array ( ’ c o n s t r u c t ’ => ’ComponentA ’ ) , ) , ’ComponentA ’ => array ( ’ class ’ => ’ Zend Foo Component A ’ , ’ instanceof ’ => ’ Zend Foo Component Interface ’ , ) ) ; Figure 6: Zend DI Proposal by Frederic Cargnelutti (mod- ified)

Slide 33

Slide 33 text

Thank You 23 More Resources: • Martin Fowler on Inversion of Control - http://martinfowler.com/articles/injectio • Ralph Schindler on Learning Dependency Injection - http://bit.ly/php-di • Sebastian Bergmann has an awesome book called Real-World Solutions for Developing High-Quality PHP Frameworks and Applications Ask Luke Allison about his Amazing Horse!