SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
@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 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
@dbhurley OpenWest 2014
WHO?
David Hurley
Open Source Evangelist
Entrepreneur / Co-Founder
Joomla! Community Manager
Production Leadership Team
@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
@dbhurley OpenWest 2014
3 TYPES OF DEPENDENCY INJECTION
Constructor Setter Interface
class carPicker
{
function carPicker ( Finder $finder )
{
$this->finder = $finder;
@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();
@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 "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.
@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) {
$this->seats = $locator->getSeats();
$this->seatbelt = $locator->getSeatbelt();
...
}
}
@dbhurley OpenWest 2014
APPLY DI
class carFactory {
function buildCar() {
$seatbelt = new SeatBelt();
$seats = new Seats($seatbelt);
return new Car($seats);
}
}
@dbhurley OpenWest 2014
LOOKING DEEPER
class Car {
function _constructor(Seats $seats) {
...
}
}
class Seats {
function _constructor(Seatbelt $seatbelt, Fabric
$fabric) {
…
}
@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);
}
}
@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.
@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.
@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.
@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.”
@dbhurley OpenWest 2014
QUESTIONS?
David Hurley
@dbhurley (twitter, facebook, linkedin)
me@dbhurley.com

Weitere ähnliche Inhalte

Ähnlich wie Dependency Injection, Service Locators, Testing and Life

Deliver Double the Value in Half the Time
Deliver Double the Value in Half the TimeDeliver Double the Value in Half the Time
Deliver Double the Value in Half the TimeDavid Hawks
 
UX for Learning Design
UX for Learning DesignUX for Learning Design
UX for Learning DesignJulie Dirksen
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentationMike Long
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentationMike Long
 
Portfolio Presentation
Portfolio PresentationPortfolio Presentation
Portfolio PresentationMike Long
 
The Monolith First Strategy!
The Monolith First Strategy!The Monolith First Strategy!
The Monolith First Strategy!Daniel Pfeiffer
 
User Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners MeetupUser Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners MeetupAgile Velocity
 
Emergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with AgileEmergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with AgileAlexandre Magno Figueiredo
 
AKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptxAKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptxammarsaputra23
 
Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014 Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014 Patrick Quattlebaum
 
Recruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation SlidesRecruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation SlidesSlideTeam
 
Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)Wisetail
 
Empathy based product validation (full)
Empathy based product validation (full)Empathy based product validation (full)
Empathy based product validation (full)Noble Ackerson
 
Recruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation SlidesRecruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation SlidesSlideTeam
 
Agile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the TimeAgile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the TimeDavid Hawks
 
Watercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptxWatercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptxRestuBisnis
 
Techniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh JainTechniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh JainNaresh Jain
 
KAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the TimeKAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the TimeDavid Hawks
 
Wireframe and prototyping google Campus talk by Zoe Guiraudon
Wireframe and prototyping google Campus talk by Zoe GuiraudonWireframe and prototyping google Campus talk by Zoe Guiraudon
Wireframe and prototyping google Campus talk by Zoe GuiraudonZoé Guiraudon
 
From the designers laptop to the users
From the designers laptop to the usersFrom the designers laptop to the users
From the designers laptop to the usersuxpin
 

Ähnlich wie Dependency Injection, Service Locators, Testing and Life (20)

Deliver Double the Value in Half the Time
Deliver Double the Value in Half the TimeDeliver Double the Value in Half the Time
Deliver Double the Value in Half the Time
 
UX for Learning Design
UX for Learning DesignUX for Learning Design
UX for Learning Design
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentation
 
Mike long-portfolio-presentation
Mike long-portfolio-presentationMike long-portfolio-presentation
Mike long-portfolio-presentation
 
Portfolio Presentation
Portfolio PresentationPortfolio Presentation
Portfolio Presentation
 
The Monolith First Strategy!
The Monolith First Strategy!The Monolith First Strategy!
The Monolith First Strategy!
 
User Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners MeetupUser Stories Suck by David Hawks at North Dallas Product Owners Meetup
User Stories Suck by David Hawks at North Dallas Product Owners Meetup
 
Emergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with AgileEmergent Practices: the true pattern for suceeding with Agile
Emergent Practices: the true pattern for suceeding with Agile
 
AKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptxAKSI 2 PART 1 materi bahasa indonesia.pptx
AKSI 2 PART 1 materi bahasa indonesia.pptx
 
Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014 Service Design Making Workshop - Giant Conference 2014
Service Design Making Workshop - Giant Conference 2014
 
Recruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation SlidesRecruiting Process Powerpoint Presentation Slides
Recruiting Process Powerpoint Presentation Slides
 
Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)Experience Matters: Even in Learning & Development (DevLearn 2014)
Experience Matters: Even in Learning & Development (DevLearn 2014)
 
Empathy based product validation (full)
Empathy based product validation (full)Empathy based product validation (full)
Empathy based product validation (full)
 
Recruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation SlidesRecruiting Process PowerPoint Presentation Slides
Recruiting Process PowerPoint Presentation Slides
 
Agile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the TimeAgile Austin - Deliver Double the Value in Half the Time
Agile Austin - Deliver Double the Value in Half the Time
 
Watercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptxWatercolor Free Powerpoint Template.pptx
Watercolor Free Powerpoint Template.pptx
 
Techniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh JainTechniques for Effectively Slicing User Stories by Naresh Jain
Techniques for Effectively Slicing User Stories by Naresh Jain
 
KAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the TimeKAA: Deliver Double the Value in Half the Time
KAA: Deliver Double the Value in Half the Time
 
Wireframe and prototyping google Campus talk by Zoe Guiraudon
Wireframe and prototyping google Campus talk by Zoe GuiraudonWireframe and prototyping google Campus talk by Zoe Guiraudon
Wireframe and prototyping google Campus talk by Zoe Guiraudon
 
From the designers laptop to the users
From the designers laptop to the usersFrom the designers laptop to the users
From the designers laptop to the users
 

Mehr von David Hurley

Personalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super PowersPersonalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super PowersDavid Hurley
 
Building Powerful Community Networks
Building Powerful Community NetworksBuilding Powerful Community Networks
Building Powerful Community NetworksDavid Hurley
 
Joomla 3.4 Features and Timeline
Joomla 3.4 Features and TimelineJoomla 3.4 Features and Timeline
Joomla 3.4 Features and TimelineDavid Hurley
 
Joomla!, The Future, and You
Joomla!, The Future, and YouJoomla!, The Future, and You
Joomla!, The Future, and YouDavid Hurley
 
The Future of Joomla
The Future of JoomlaThe Future of Joomla
The Future of JoomlaDavid Hurley
 
The Importance of the User Experience
The Importance of the User ExperienceThe Importance of the User Experience
The Importance of the User ExperienceDavid Hurley
 
Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)David Hurley
 

Mehr von David Hurley (10)

Personalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super PowersPersonalization & 4 Other Marketing Super Powers
Personalization & 4 Other Marketing Super Powers
 
Building Powerful Community Networks
Building Powerful Community NetworksBuilding Powerful Community Networks
Building Powerful Community Networks
 
Age of the Robot
Age of the RobotAge of the Robot
Age of the Robot
 
Joomla 3.4 Features and Timeline
Joomla 3.4 Features and TimelineJoomla 3.4 Features and Timeline
Joomla 3.4 Features and Timeline
 
Joomla!, The Future, and You
Joomla!, The Future, and YouJoomla!, The Future, and You
Joomla!, The Future, and You
 
Joomla! and You
Joomla! and YouJoomla! and You
Joomla! and You
 
The Future of Joomla
The Future of JoomlaThe Future of Joomla
The Future of Joomla
 
Code Like A Boss
Code Like A BossCode Like A Boss
Code Like A Boss
 
The Importance of the User Experience
The Importance of the User ExperienceThe Importance of the User Experience
The Importance of the User Experience
 
Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)Joomla! Radical Ideas (J & Beyond 2013)
Joomla! Radical Ideas (J & Beyond 2013)
 

Kürzlich hochgeladen

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Kürzlich hochgeladen (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Dependency Injection, Service Locators, Testing and Life

  • 1. @dbhurley OpenWest 2014 DI, SL, Testing, Life A Pragmatic Approach
  • 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. @dbhurley OpenWest 2014 WHO? David Hurley Open Source Evangelist Entrepreneur / Co-Founder Joomla! Community Manager Production Leadership Team
  • 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. @dbhurley OpenWest 2014 3 TYPES OF DEPENDENCY INJECTION Constructor Setter Interface class carPicker { function carPicker ( Finder $finder ) { $this->finder = $finder;
  • 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. @dbhurley OpenWest 2014 WHAT WE’RE NOT DISCUSSING
  • 8. @dbhurley OpenWest 2014 WHAT WE ARE DISCUSSING
  • 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. @dbhurley OpenWest 2014 WHAT’S THAT MEAN? Question How do you pay your tab at a restaurant?
  • 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. @dbhurley OpenWest 2014 APPLY DI class carFactory { function buildCar() { $seatbelt = new SeatBelt(); $seats = new Seats($seatbelt); return new Car($seats); } }
  • 13. @dbhurley OpenWest 2014 LOOKING DEEPER class Car { function _constructor(Seats $seats) { ... } } class Seats { function _constructor(Seatbelt $seatbelt, Fabric $fabric) { … }
  • 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. @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. @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. @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. @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. @dbhurley OpenWest 2014 QUESTIONS? David Hurley @dbhurley (twitter, facebook, linkedin) me@dbhurley.com