what is dependency injection
DESCRIPTION
A 15 minute overview of what Dependency Injection is.TRANSCRIPT
![Page 1: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/1.jpg)
What is DependencyInjection?
Shawn Stratton
2nd June 2011—
![Page 2: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/2.jpg)
What I’ll Cover
2
I will cover:
![Page 3: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/3.jpg)
What I’ll Cover
2
I will cover:
• What DI isn’t
![Page 4: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/4.jpg)
What I’ll Cover
2
I will cover:
• What DI isn’t
• What DI is
![Page 5: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/10.jpg)
What is DependencyInjection not?
![Page 11: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/11.jpg)
New
4
Figure 1: http://martinfowler.com/articles/injection.html
![Page 12: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/12.jpg)
Magic
5
Figure 2: Merlin by One Luck Guy (Flickr)
![Page 13: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/13.jpg)
Complex
6
Figure 3: Complexity 3 by Michael Heiss (Flickr)
![Page 14: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/14.jpg)
So what is it?
![Page 15: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/17.jpg)
About Components
![Page 18: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/18.jpg)
Service Locator
![Page 19: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/19.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/20.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/21.jpg)
Containers
![Page 22: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/23.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/24.jpg)
What are the benefits ofDependency Injection?
![Page 25: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/25.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/26.jpg)
Easy Extension
19
Steps to extend and use a class:
![Page 27: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/27.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/28.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/29.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/30.jpg)
What are the costs?
![Page 31: What is Dependency Injection](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/31.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/32.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052619/555cb9e5d8b42a64718b4725/html5/thumbnails/33.jpg)
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!