Dependency Injection, Service Locators, Testing and Life

19
@dbhurley OpenWest 2014 DI, SL, Testing, Life A Pragmatic Approach

description

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

Transcript of Dependency Injection, Service Locators, Testing and Life

Page 1: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

DI, SL, Testing, LifeA Pragmatic Approach

Page 2: Dependency Injection, Service Locators, Testing and Life

@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

Page 3: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

WHO?

David HurleyOpen Source EvangelistEntrepreneur / Co-Founder

Joomla! Community ManagerProduction Leadership Team

Page 4: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

TERMSDependency InjectionAn injection is the passing of a dependency (a service) to a dependent object (a client).

Service LocatorA design pattern which uses a central registry and on request returns the information necessary to a client.

Inversion of ControlA design in which portions of a computer program receive flow of control from a generic, reusable library

Page 5: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

3 TYPES OF DEPENDENCY INJECTION

Constructor Setter Interface

class carPicker {

function carPicker ( Finder $finder ){

$this->finder = $finder;

Page 6: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

SERVICE LOCATORSMost often seen used as a singleton registry*

class serviceLocator {

public static function carFinder ()

{

return static::finder;

}

class carPicker {

function carPicker() {

$finder = serviceLocator::carFinder();

Page 7: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

WHAT WE’RE NOT DISCUSSING

Page 8: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

WHAT WE ARE DISCUSSING

Page 9: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

THE LAW OF DEMETEREach 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.

Page 10: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

WHAT’S THAT MEAN?

QuestionHow do you pay your tab at a restaurant?

Page 11: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

APPLY SL

class Car {

protected $seats

protected $seatbelt

function buildCar (Locator $locator) {

$this->seats = $locator->getSeats();

$this->seatbelt = $locator->getSeatbelt();

...

}

}

Page 12: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

APPLY DI

class carFactory {function buildCar() { $seatbelt = new SeatBelt();

$seats = new Seats($seatbelt); return new Car($seats);}

}

Page 13: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

LOOKING DEEPER

class Car {

function _constructor(Seats $seats) {

...

}

}

class Seats { function _constructor(Seatbelt $seatbelt, Fabric $fabric) {

}

Page 14: Dependency Injection, Service Locators, Testing and Life

@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);

}

}

Page 15: Dependency Injection, Service Locators, Testing and Life

@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.

Page 16: Dependency Injection, Service Locators, Testing and Life

@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 checkingPre-condition checking is hard to test. Rather check that things work as expected rather than checking against nulls.

Page 17: Dependency Injection, Service Locators, Testing and Life

@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.

Page 18: Dependency Injection, Service Locators, Testing and Life

@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.”

Page 19: Dependency Injection, Service Locators, Testing and Life

@dbhurley OpenWest 2014

QUESTIONS?

David Hurley@dbhurley (twitter, facebook, linkedin)

[email protected]