@dbhurley OpenWest 2014
DI, SL, Testing, Life
A Pragmatic Approach
@dbhurley OpenWest 2014
WELCOME!
This session is not intimidating. This session is
not intimidating. This session is not i...
@dbhurley OpenWest 2014
WHO?
David Hurley
Open Source Evangelist
Entrepreneur / Co-Founder
Joomla! Community Manager
Produ...
@dbhurley OpenWest 2014
TERMS
Dependency Injection
An injection is the passing of a dependency (a service)
to a dependent ...
@dbhurley OpenWest 2014
3 TYPES OF DEPENDENCY INJECTION
Constructor Setter Interface
class carPicker
{
function carPicker ...
@dbhurley OpenWest 2014
SERVICE LOCATORS
Most often seen used as a singleton registry*
class serviceLocator {
public stati...
@dbhurley OpenWest 2014
WHAT WE’RE NOT DISCUSSING
@dbhurley OpenWest 2014
WHAT WE ARE DISCUSSING
@dbhurley OpenWest 2014
THE LAW OF DEMETER
Each unit should have only limited knowledge about
other units: only units "clo...
@dbhurley OpenWest 2014
WHAT’S THAT MEAN?
Question
How do you pay your tab at a restaurant?
@dbhurley OpenWest 2014
APPLY SL
class Car {
protected $seats
protected $seatbelt
function buildCar (Locator $locator) {
$...
@dbhurley OpenWest 2014
APPLY DI
class carFactory {
function buildCar() {
$seatbelt = new SeatBelt();
$seats = new Seats($...
@dbhurley OpenWest 2014
LOOKING DEEPER
class Car {
function _constructor(Seats $seats) {
...
}
}
class Seats {
function _c...
@dbhurley OpenWest 2014
RETURN TO FACTORY
class carFactory {
function buildCar() {
$seatbelt = new SeatBelt();
$fabric = n...
@dbhurley OpenWest 2014
KEY CONCEPT?
A Factory is used for all the objects of
the same lifetime.
Constructor Injection: On...
@dbhurley OpenWest 2014
TESTING
Service Locators are often difficult to test.
Mixed responsibilities / Law of Demeter part...
@dbhurley OpenWest 2014
THE GOAL
The goal is to write clean, concise, easily-
testable code which accomplishes a
purpose a...
@dbhurley OpenWest 2014
THE OUTCOME
A strong codebase which can be easily read
and understood by others.
“Always code as i...
@dbhurley OpenWest 2014
QUESTIONS?
David Hurley
@dbhurley (twitter, facebook, linkedin)
me@dbhurley.com
Upcoming SlideShare
Loading in...5
×

Dependency Injection, Service Locators, Testing and Life

262

Published on

This talk, given at OpenWest Utah 2014, covers a comparison between PHP design patterns and how they affect testing practices.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
262
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dependency Injection, Service Locators, Testing and Life

  1. 1. @dbhurley OpenWest 2014 DI, SL, Testing, Life A Pragmatic Approach
  2. 2. @dbhurley OpenWest 2014 WELCOME! This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not intimidating. This session is not inti
  3. 3. @dbhurley OpenWest 2014 WHO? David Hurley Open Source Evangelist Entrepreneur / Co-Founder Joomla! Community Manager Production Leadership Team
  4. 4. @dbhurley OpenWest 2014 TERMS Dependency Injection An injection is the passing of a dependency (a service) to a dependent object (a client). Service Locator A design pattern which uses a central registry and on request returns the information necessary to a client. Inversion of Control A design in which portions of a computer program receive flow of control from a generic, reusable library
  5. 5. @dbhurley OpenWest 2014 3 TYPES OF DEPENDENCY INJECTION Constructor Setter Interface class carPicker { function carPicker ( Finder $finder ) { $this->finder = $finder;
  6. 6. @dbhurley OpenWest 2014 SERVICE LOCATORS Most often seen used as a singleton registry* class serviceLocator { public static function carFinder () { return static::finder; } class carPicker { function carPicker() { $finder = serviceLocator::carFinder();
  7. 7. @dbhurley OpenWest 2014 WHAT WE’RE NOT DISCUSSING
  8. 8. @dbhurley OpenWest 2014 WHAT WE ARE DISCUSSING
  9. 9. @dbhurley OpenWest 2014 THE LAW OF DEMETER Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. Each unit should only talk to its friends; don't talk to strangers. Only talk to your immediate friends.
  10. 10. @dbhurley OpenWest 2014 WHAT’S THAT MEAN? Question How do you pay your tab at a restaurant?
  11. 11. @dbhurley OpenWest 2014 APPLY SL class Car { protected $seats protected $seatbelt function buildCar (Locator $locator) { $this->seats = $locator->getSeats(); $this->seatbelt = $locator->getSeatbelt(); ... } }
  12. 12. @dbhurley OpenWest 2014 APPLY DI class carFactory { function buildCar() { $seatbelt = new SeatBelt(); $seats = new Seats($seatbelt); return new Car($seats); } }
  13. 13. @dbhurley OpenWest 2014 LOOKING DEEPER class Car { function _constructor(Seats $seats) { ... } } class Seats { function _constructor(Seatbelt $seatbelt, Fabric $fabric) { … }
  14. 14. @dbhurley OpenWest 2014 RETURN TO FACTORY class carFactory { function buildCar() { $seatbelt = new SeatBelt(); $fabric = new Fabric(); $seats = new Seats($seatbelt, $fabric); return new Car($seats); } }
  15. 15. @dbhurley OpenWest 2014 KEY CONCEPT? A Factory is used for all the objects of the same lifetime. Constructor Injection: Only inject items whose lifetime is equal or greater than the injectee (i.e. The seat is alive at least as long as the car) Method Parameter: At execution for shorter lifetimes.
  16. 16. @dbhurley OpenWest 2014 TESTING Service Locators are often difficult to test. Mixed responsibilities / Law of Demeter partially broken Dependency Injection allows for easy testing. Clear to follow / Law of Demeter obeyed Side note: Null checking Pre-condition checking is hard to test. Rather check that things work as expected rather than checking against nulls.
  17. 17. @dbhurley OpenWest 2014 THE GOAL The goal is to write clean, concise, easily- testable code which accomplishes a purpose and allows others to improve it.
  18. 18. @dbhurley OpenWest 2014 THE OUTCOME A strong codebase which can be easily read and understood by others. “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
  19. 19. @dbhurley OpenWest 2014 QUESTIONS? David Hurley @dbhurley (twitter, facebook, linkedin) me@dbhurley.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×