What is Dependency Injection

Post on 20-May-2015

862 views 1 download

Tags:

description

A 15 minute overview of what Dependency Injection is.

Transcript of What is Dependency Injection

What is DependencyInjection?

Shawn Stratton

2nd June 2011—

What I’ll Cover

2

I will cover:

What I’ll Cover

2

I will cover:

• What DI isn’t

What I’ll Cover

2

I will cover:

• What DI isn’t

• What DI is

What I’ll Cover

2

I will cover:

• What DI isn’t

• What DI is

• Why you should use DI

What I’ll Cover

2

I will cover:

• What DI isn’t

• What DI is

• Why you should use DI

I won’t cover:

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

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

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)

What is DependencyInjection not?

New

4

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

Magic

5

Figure 2: Merlin by One Luck Guy (Flickr)

Complex

6

Figure 3: Complexity 3 by Michael Heiss (Flickr)

So what is it?

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

}}

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

About Components

Service Locator

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

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

Containers

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

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)

What are the benefits ofDependency 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 ) ;

}}

Easy Extension

19

Steps to extend and use a class:

Easy Extension

19

Steps to extend and use a class:

1. Create class b and have it extend class a

Easy Extension

19

Steps to extend and use a class:

1. Create class b and have it extend class a

2. Change Mapping

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!

What are the costs?

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 ;}

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)

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!