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

What is Dependency Injection

Embed Size (px)

DESCRIPTION

A 15 minute overview of what Dependency Injection is.

Citation preview

Page 1: What is Dependency Injection

What is DependencyInjection?

Shawn Stratton

2nd June 2011—

Page 2: What is Dependency Injection

What I’ll Cover

2

I will cover:

Page 3: What is Dependency Injection

What I’ll Cover

2

I will cover:

• What DI isn’t

Page 4: What is Dependency Injection

What I’ll Cover

2

I will cover:

• What DI isn’t

• What DI is

Page 5: What is Dependency Injection

What I’ll Cover

2

I will cover:

• What DI isn’t

• What DI is

• Why you should use DI

Page 6: What is Dependency Injection

What I’ll Cover

2

I will cover:

• What DI isn’t

• What DI is

• Why you should use DI

I won’t cover:

Page 7: What is Dependency Injection

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

Page 8: What is Dependency Injection

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

Page 9: What is Dependency Injection

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)

Page 10: What is Dependency Injection

What is DependencyInjection not?

Page 11: What is Dependency Injection

New

4

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

Page 12: What is Dependency Injection

Magic

5

Figure 2: Merlin by One Luck Guy (Flickr)

Page 13: What is Dependency Injection

Complex

6

Figure 3: Complexity 3 by Michael Heiss (Flickr)

Page 14: What is Dependency Injection

So what is it?

Page 15: What is Dependency Injection

A Design Style

8

<?php/ / Just Some Classc lass Dependant {

pro tec ted $db ;p ro tec ted $dependency ;p u b l i c f u n c t i o n __construct (PDO $db , Depdendency

$dependency ) {$this−>db = $db ;$this−>dependency = $dependency ;

}

p u b l i c f u n c t i o n somefunc ( ) {/ / Use Dependencies

}}

Page 16: What is Dependency Injection

Easy

9

<?php/ / This i s Meta Code Only ( not a concrete Implementat ion )

/ / Create Locator , pass a mappings f i l e or var/ / Note Dependency would be def ined here as would/ / Dependant$sl = new Container ( ’ / path / to / mappings / f i l e ’ ) ;/ / Create PDO (we don ’ t want to map i t )$pdo = new PDO ( ’ dsn ’ ) ;$sl−>defineSingleton ( ’PDO ’ , $pdo ) ;

$dependant = $sl−>get ( ’ Dependant ’ ) ;/ / Dependant i s type of Dependant

Page 17: What is Dependency Injection

About Components

Page 18: What is Dependency Injection

Service Locator

Page 19: What is Dependency Injection

Service Locator

12

Service Location is like ordering withsubstitutions, and having the waiter completelyignore the substitutions; you get what’s on themenu, nothing more, nothing less.

Figure 4: Matthew Weier O‘Phinney on Service Locators

Page 20: What is Dependency Injection

Service Locators Detail

13

• It’s a fancy registry.

• Inject the locator into the class via contstructor, callthe the locator to find your services.

• Works, but it’s not foolproof

Page 21: What is Dependency Injection

Containers

Page 22: What is Dependency Injection

Another Analogy

15

Dependency Injection is like ordering off themenu – but specifying things like, ”I’d like tosubstitute portabella mushrooms for the patties,please.” The waiter then goes and brings yourdish, which has portabella mushrooms insteadof the hamburger patties listed on the menu.

Figure 5: Matthew Weier O‘Phinney on DI Containers

Page 23: What is Dependency Injection

DI Container Detail

16

• Still a fancy registry, basically just a Service Locator.

• Instantiates new classes by resolving and injectingtheir dependencies.

• Very Clean in regards to separation of concerns.

• Not required to run the system (you can do thismanually, trust me)

Page 24: What is Dependency Injection

What are the benefits ofDependency Injection?

Page 25: What is Dependency Injection

Makes Testing Easy

18

<?phpc lass DependantTest extends PHPUnit_Framework_TestCase

pro tec ted $dependant ;

p ro tec ted f u n c t i o n setUp ( ) {$pdo = new PDO ( ’ s q l i t e dsn ’ ) ;$dependency = $this−>getMock ( ’ Dependency ’ ,

a r ray ( ’ someFunction ’ ) ) ;$dependency−>expects ( $this−>once ( ) )−>method (

’ someFunction ’ ) ;$this−>dependant = new Dependant ($pdo , $dependency ) ;

}}

Page 26: What is Dependency Injection

Easy Extension

19

Steps to extend and use a class:

Page 27: What is Dependency Injection

Easy Extension

19

Steps to extend and use a class:

1. Create class b and have it extend class a

Page 28: What is Dependency Injection

Easy Extension

19

Steps to extend and use a class:

1. Create class b and have it extend class a

2. Change Mapping

Page 29: What is Dependency Injection

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!

Page 30: What is Dependency Injection

What are the costs?

Page 31: What is Dependency Injection

Enforces Interfaces

21

<?php/ / I n t e face Def in ing the ” Math ” Apii n t e r f a c e Math {

p u b l i c f u n c t i o n add ($a , $b ) ;p u b l i c f u n c t i o n sub ($a , $b ) ;p u b l i c f u n c t i o n multiply ($a , $b ) ;p u b l i c f u n c t i o n divide ($a , $b ) ;

}/ / 2+2 = 5 f o r la rge values o f 2/ / ( see Thinkgeek s h i r t s )c lass HeavyMath implements Math {

p u b l i c f u n c t i o n add ($a , $b ){

r e t u r n ($a == 2 && $b == 2) ? 5 : $a+$b ;}

Page 32: What is Dependency Injection

Mapping Files

22

<?phpr e t u r n ar ray (

’ Foo ’ => ar ray (’ c lass ’ => ’ Zend Foo ’ ,’ arguments ’ => ar ray ( ’ c o n s t r u c t ’ => ’ ComponentA ’ ) ,

) ,’ ComponentA ’ => ar ray (

’ c lass ’ => ’ Zend Foo Component A ’ ,’ i ns tanceo f ’ => ’ Zend Foo Component Interface ’ ,

) ;

Figure 6: Zend DI Proposal by Frederic Cargnelutti (mod-ified)

Page 33: What is Dependency Injection

Thank You

23

More Resources:

• Martin Fowler on Inversion of Control -http://martinfowler.com/articles/injection.html

• Ralph Schindler on Learning Dependency Injection -http://bit.ly/php-di

• Sebastian Bergmann has an awesome book calledReal-World Solutions for Developing High-QualityPHP Frameworks and Applications

Ask Luke Allison about his Amazing Horse!