SlideShare une entreprise Scribd logo
1  sur  69
Télécharger pour lire hors ligne
Real World
Dependency Injection
Stephan Hochdörfer, bitExpert AG
Real World Dependency Injection

 About me

  Stephan Hochdörfer, bitExpert AG

  Department Manager Research Labs

  enjoying PHP since 1999

  S.Hochdoerfer@bitExpert.de

  @shochdoerfer
Real World Dependency Injection

 What are Dependencies?
Real World Dependency Injection

 What are Dependencies?



                          Application




                  Framework        add. Libraries
Real World Dependency Injection

 What are Dependencies?

                           Controller




  PHP extensions         Service / Model   Utils




                          Datastore(s)
Real World Dependency Injection

 Are Dependencies bad?
Real World Dependency Injection

 Are Dependencies bad?




           Dependencies are not bad!
               They are useful!
Real World Dependency Injection

 Are Dependencies bad?




      Hard-coded dependencies are bad!
Real World Dependency Injection


 Tightly coupled code
Real World Dependency Injection




 No reuse of components
Real World Dependency Injection




 No isolation, not testable!
Real World Dependency Injection




 Development gets overcomplicated!
Real World Dependency Injection




                              s




 „new“ is evil!
Real World Dependency Injection

 „new“ is evil!
 <?php
 class DeletePage extends Mvc_Action_AAction {
    private $pageManager;

     public function __construct() {
        $this->pageManager = new PageManager();
     }

     protected function execute(Mvc_Request $request) {
        $this->pageManager->delete(
           (int) $request->get('pageId')
        );
     }
 }
Real World Dependency Injection

 „new“ is evil!
 <?php
 class DeletePage extends Mvc_Action_AAction {
    private $pageManager;

     public function __construct(PageManager $pm) {
        $this->pageManager = $pm;
     }

     protected function execute(Mvc_Request $request) {
        $this->pageManager->delete(
           (int) $request->get('pageId')
        );
     }
 }
Real World Dependency Injection




        "High-level modules should not
         depend on low-level modules.
            Both should depend on
                 abstractions."
                        Robert C. Martin
Real World Dependency Injection



 Interfaces act as contracts
Real World Dependency Injection

 „new“ is evil!
 <?php
 class DeletePage extends Mvc_Action_AAction {
    private $pageManager;

     public function __construct(IPageManager $pm) {
        $this->pageManager = $pm;
     }

     protected function execute(Mvc_Request $request) {
        $this->pageManager->delete(
           (int) $request->get('pageId')
        );
     }
 }
Real World Dependency Injection

 How to Manage Dependencies?
Real World Dependency Injection



 Manually resolve dependencies?
Real World Dependency Injection

 Automatic wiring required!




     Simple Container       vs.    Full stacked
                                  DI Framework
Real World Dependency Injection

 What is Dependency Injection?
Real World Dependency Injection

 What is Dependency Injection?




    Consumer
Real World Dependency Injection

 What is Dependency Injection?




    Consumer            Dependencies
Real World Dependency Injection

 What is Dependency Injection?




    Consumer            Dependencies   Container
Real World Dependency Injection

 What is Dependency Injection?




    Consumer            Dependencies   Container
Real World Dependency Injection




 How to inject a Dependency?
Real World Dependency Injection

 Constructor Injection
 <?php

 class MySampleService implements IMySampleService {
   /**
    * @var ISampleDao
    */
   private $sampleDao;

     public function __construct(ISampleDao $sampleDao) {
       $this->sampleDao = $sampleDao;
     }
 }
Real World Dependency Injection

 Setter injection
 <?php

 class MySampleService implements IMySampleService {
   /**
    * @var ISampleDao
    */
   private $sampleDao;

     public function setSampleDao(ISampleDao $sampleDao){
      $this->sampleDao = $sampleDao;
     }
 }
Real World Dependency Injection

 Interface injection
 <?php

 interface IApplicationContextAware {
   public function setCtx(IApplicationContext $ctx);
 }
Real World Dependency Injection

 Interface injection
 <?php

 class MySampleService implements IMySampleService,
 IApplicationContextAware {
   /**
    * @var IApplicationContext
    */
   private $ctx;

     public function setCtx(IApplicationContext $ctx) {
      $this->ctx = $ctx;
     }
 }
Real World Dependency Injection




 How to wire it all up?
Real World Dependency Injection

 Annotation based wiring
 <?php

 class MySampleService implements IMySampleService {
   private $sampleDao;

     /**
      * @Inject
      */
     public function __construct(ISampleDao $sampleDao)
     {
         $this->sampleDao = $sampleDao;
     }
 }
Real World Dependency Injection

 Annotation based wiring
 <?php

 class MySampleService implements IMySampleService {
   private $sampleDao;

     /**
      * @Inject
      * @Named('TheSampleDao')
      */
     public function __construct(ISampleDao $sampleDao)
     {
         $this->sampleDao = $sampleDao;
     }
 }
Real World Dependency Injection

 External configuration - XML

 <?xml version="1.0" encoding="UTF-8" ?>
 <beans>
    <bean id="SampleDao" class="SampleDao">
       <constructor-arg value="app_sample" />
       <constructor-arg value="iSampleId" />
       <constructor-arg value="BoSample" />
    </bean>

    <bean id="SampleService" class="MySampleService">
       <constructor-arg ref="SampleDao" />
    </bean>
 </beans>
Real World Dependency Injection

 External configuration - YAML

 services:
   SampleDao:
     class: SampleDao
     arguments: ['app_sample', 'iSampleId', 'BoSample']
   SampleService:
     class: SampleService
     arguments: [@SampleDao]
Real World Dependency Injection

 External configuration - PHP
 <?php
 class BeanCache extends Beanfactory_Container_PHP {
    protected function createSampleDao() {
       $oBean = new SampleDao('app_sample',
         'iSampleId', 'BoSample');
       return $oBean;
    }

     protected function createMySampleService() {
        $oBean = new MySampleService(
           $this->getBean('SampleDao')
        );
        return $oBean;
     }
 }
Real World Dependency Injection



 Ready to unlock the door?
Real World Dependency Injection

 Unittesting made easy
Real World Dependency Injection

 Unittesting made easy
 <?php
 require_once 'PHPUnit/Framework.php';

 class ServiceTest extends PHPUnit_Framework_TestCase {
     public function testSampleService() {
       // set up dependencies
       $sampleDao = $this->getMock('ISampleDao');
       $service   = new MySampleService($sampleDao);

         // run test case
         $return = $service->doWork();

         // check assertions
         $this->assertTrue($return);
     }
 }
Real World Dependency Injection

 One class, multiple configurations
Real World Dependency Injection

 One class, multiple configurations


                              Page Exporter
                               Page Exporter




      Released / /Published
       Released Published                      Workingcopy
                                               Workingcopy
             Pages
              Pages                              Pages
                                                  Pages
Real World Dependency Injection

 One class, multiple configurations
 <?php
 abstract class PageExporter {
   protected function setPageDao(IPageDao $pageDao) {
     $this->pageDao = $pageDao;
   }
 }
Real World Dependency Injection

 One class, multiple configurations
 <?php
 abstract class PageExporter {
   protected function setPageDao(IPageDao $pageDao) {
     $this->pageDao = $pageDao;
   }
 }


                                   Remember:
                                   The contract!
Real World Dependency Injection

 One class, multiple configurations
 <?php
 class PublishedPageExporter extends PageExporter {
   public function __construct() {
     $this->setPageDao(new PublishedPageDao());
   }
 }


 class WorkingCopyPageExporter extends PageExporter {
   public function __construct() {
     $this->setPageDao(new WorkingCopyPageDao());
   }
 }
Real World Dependency Injection

 One class, multiple configurations




      "Only deleted code is good code!"
                         Oliver Gierke
Real World Dependency Injection

 One class, multiple configurations
 <?php
 class PageExporter {
   public function __construct(IPageDao $pageDao) {
     $this->pageDao = $pageDao;
   }
 }
Real World Dependency Injection

 One class, multiple configurations
 <?xml version="1.0" encoding="UTF-8" ?>
 <beans>
    <bean id="ExportLive" class="PageExporter">
       <constructor-arg ref="PublishedPageDao" />
    </bean>


    <bean id="ExportWorking" class="PageExporter">
       <constructor-arg ref="WorkingCopyPageDao" />
    </bean>
 </beans>
Real World Dependency Injection

 One class, multiple configurations
 <?php

 // create ApplicationContext instance
 $ctx = new ApplicationContext();

 // retrieve live exporter
 $exporter = $ctx->getBean('ExportLive');

 // retrieve working copy exporter
 $exporter = $ctx->getBean('ExportWorking');
Real World Dependency Injection

 One class, multiple configurations II
Real World Dependency Injection

 One class, multiple configurations II


        http://editor.loc/page/[id]/headline/

         http://editor.loc/page/[id]/content/

          http://editor.loc/page/[id]/teaser/
Real World Dependency Injection

 One class, multiple configurations II
 <?php
 class EditPart extends Mvc_Action_AFormAction {
    private $pagePartsManager;
    private $type;

     public function __construct(IPagePartsManager $pm) {
        $this->pagePartsManager = $pm;
     }

     public function setType($ptype) {
        $this->type = (int) $type;
     }

     protected function process(Bo_ABo $formBackObj) {
     }
 }
Real World Dependency Injection

 One class, multiple configurations II
 <?xml version="1.0" encoding="UTF-8" ?>
 <beans>
    <bean id="EditHeadline" class="EditPart">
       <constructor-arg ref="PagePartDao" />
       <property name="Type" const="PType::Headline" />
    </bean>

   <bean id="EditContent" class="EditPart">
      <constructor-arg ref="PagePartDao" />
      <property name="Type" const="PType::Content" />
   </bean>

 </beans>
Real World Dependency Injection

 Mocking external service access
Real World Dependency Injection

 Mocking external service access




                               WS-
                                WS-
   Booking service
    Booking service                       Webservice
                                          Webservice
                             Connector
                              Connector
Real World Dependency Injection

 Mocking external service access




                                   WS-
                                    WS-
   Booking service
    Booking service                           Webservice
                                              Webservice
                                 Connector
                                  Connector




                 Remember:
                 The contract!
Real World Dependency Injection

 Mocking external service access




                                FS-
                                 FS-
   Booking service
    Booking service                       Filesystem
                                           Filesystem
                             Connector
                              Connector
Real World Dependency Injection

 Mocking external service access




                                         FS-
                                          FS-
   Booking service
    Booking service                                Filesystem
                                                    Filesystem
                                      Connector
                                       Connector




                      fullfills the
                       contract!
Real World Dependency Injection

 Clean, readable code
Real World Dependency Injection

 Clean, readable code
 <?php
 class DeletePage extends Mvc_Action_AAction {
    private $pageManager;

     public function __construct(IPageManager $pm) {
        $this->pageManager = $pm;
     }

     protected function execute(Mvc_Request $request) {
        $this->pageManager->delete(
           (int) $request->get('pageId'));

         return new ModelAndView($this->getSuccessView());
     }
 }
Real World Dependency Injection

 No framework dependency
Real World Dependency Injection

 No framework dependency
 <?php
 class MySampleService implements IMySampleService {
   private $sampleDao;

     public function __construct(ISampleDao $sampleDao) {
      $this->sampleDao = $sampleDao;
     }

     public function getSample($sampleId) {
      try {
        return $this->sampleDao->readById($sampleId);
      }
      catch(DaoException $exception) {}
     }
 }
Real World Dependency Injection

 Benefits




  Loose coupling, reuse of components!
Real World Dependency Injection

 Benefits




       Can reduce the amount of code!
Real World Dependency Injection

 Benefits




               Helps developers to
               understand the code!
Real World Dependency Injection

 Cons – No JSR330 for PHP


  Bucket, Crafty, FLOW3, Imind_Context,
      PicoContainer, Pimple, Phemto,
   Stubbles, Symfony 2.0, Sphicy, Solar,
     Substrate, XJConf, Yadif, Zend_Di
    (Proposal), Lion Framework, Spiral
     Framework, Xyster Framework, …
Real World Dependency Injection

 Cons – Developers need mindshift




            Configuration ↔ Runtime
http://joind.in/3002
Image Credits
http://www.sxc.hu/photo/1028452

Contenu connexe

Tendances

Real World Dependency Injection - PFCongres 2010
Real World Dependency Injection - PFCongres 2010Real World Dependency Injection - PFCongres 2010
Real World Dependency Injection - PFCongres 2010
Stephan Hochdörfer
 
Real World Dependency Injection SE - phpugrhh
Real World Dependency Injection SE - phpugrhhReal World Dependency Injection SE - phpugrhh
Real World Dependency Injection SE - phpugrhh
Stephan Hochdörfer
 
Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010
Stephan Hochdörfer
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Code
erikmsp
 

Tendances (20)

Real World Dependency Injection - PFCongres 2010
Real World Dependency Injection - PFCongres 2010Real World Dependency Injection - PFCongres 2010
Real World Dependency Injection - PFCongres 2010
 
Real World Dependency Injection SE - phpugrhh
Real World Dependency Injection SE - phpugrhhReal World Dependency Injection SE - phpugrhh
Real World Dependency Injection SE - phpugrhh
 
JavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainerJavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainer
 
UI testing in Xcode 7
UI testing in Xcode 7UI testing in Xcode 7
UI testing in Xcode 7
 
Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
 
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
 
Con5623 pdf 5623_001
Con5623 pdf 5623_001Con5623 pdf 5623_001
Con5623 pdf 5623_001
 
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"
 
PHP 7 Crash Course
PHP 7 Crash CoursePHP 7 Crash Course
PHP 7 Crash Course
 
Java Annotation Processing: A Beginner Walkthrough
Java Annotation Processing: A Beginner WalkthroughJava Annotation Processing: A Beginner Walkthrough
Java Annotation Processing: A Beginner Walkthrough
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Code
 
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
 
Mastering Mock Objects - Advanced Unit Testing for Java
Mastering Mock Objects - Advanced Unit Testing for JavaMastering Mock Objects - Advanced Unit Testing for Java
Mastering Mock Objects - Advanced Unit Testing for Java
 
PHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4DevelopersPHPSpec - the only Design Tool you need - 4Developers
PHPSpec - the only Design Tool you need - 4Developers
 
EMF Tips n Tricks
EMF Tips n TricksEMF Tips n Tricks
EMF Tips n Tricks
 
Deep dive into Oracle ADF
Deep dive into Oracle ADFDeep dive into Oracle ADF
Deep dive into Oracle ADF
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013
 

En vedette (6)

Dapper performance
Dapper performanceDapper performance
Dapper performance
 
Difference between wcf and asp.net web api
Difference between wcf and asp.net web apiDifference between wcf and asp.net web api
Difference between wcf and asp.net web api
 
Dependency injection in asp.net core
Dependency injection in asp.net coreDependency injection in asp.net core
Dependency injection in asp.net core
 
Introduction the Repository Pattern
Introduction the Repository PatternIntroduction the Repository Pattern
Introduction the Repository Pattern
 
Repository and Unit Of Work Design Patterns
Repository and Unit Of Work Design PatternsRepository and Unit Of Work Design Patterns
Repository and Unit Of Work Design Patterns
 
Real World Asp.Net WebApi Applications
Real World Asp.Net WebApi ApplicationsReal World Asp.Net WebApi Applications
Real World Asp.Net WebApi Applications
 

Similaire à Real World Dependency Injection - phpday

Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13
Stephan Hochdörfer
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring Edition
Stephan Hochdörfer
 
Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13
Stephan Hochdörfer
 
Dependency Injection in PHP - dwx13
Dependency Injection in PHP - dwx13Dependency Injection in PHP - dwx13
Dependency Injection in PHP - dwx13
Stephan Hochdörfer
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejug
robbiev
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
arcware
 

Similaire à Real World Dependency Injection - phpday (20)

Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13Real World Dependency Injection - phpugffm13
Real World Dependency Injection - phpugffm13
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring Edition
 
Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13
 
Dependency Injection in PHP - dwx13
Dependency Injection in PHP - dwx13Dependency Injection in PHP - dwx13
Dependency Injection in PHP - dwx13
 
Dependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony Container
 
Yii Introduction
Yii IntroductionYii Introduction
Yii Introduction
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web Framework
 
WCLA12 JavaScript
WCLA12 JavaScriptWCLA12 JavaScript
WCLA12 JavaScript
 
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey BuzdinMarvel of Annotation Preprocessing in Java by Alexey Buzdin
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
 
Dropwizard Introduction
Dropwizard IntroductionDropwizard Introduction
Dropwizard Introduction
 
Hexagonal architecture
Hexagonal architectureHexagonal architecture
Hexagonal architecture
 
Maintaining a dependency graph with weaver
Maintaining a dependency graph with weaverMaintaining a dependency graph with weaver
Maintaining a dependency graph with weaver
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejug
 
WebGUI Developers Workshop
WebGUI Developers WorkshopWebGUI Developers Workshop
WebGUI Developers Workshop
 
Writing your Third Plugin
Writing your Third PluginWriting your Third Plugin
Writing your Third Plugin
 
React 101 by Anatoliy Sieryi
React 101 by Anatoliy Sieryi React 101 by Anatoliy Sieryi
React 101 by Anatoliy Sieryi
 
Vaadin 7 CN
Vaadin 7 CNVaadin 7 CN
Vaadin 7 CN
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 

Plus de Stephan Hochdörfer

Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
Stephan Hochdörfer
 
Phing for power users - frOSCon8
Phing for power users - frOSCon8Phing for power users - frOSCon8
Phing for power users - frOSCon8
Stephan Hochdörfer
 
Offline strategies for HTML5 web applications - frOSCon8
Offline strategies for HTML5 web applications - frOSCon8Offline strategies for HTML5 web applications - frOSCon8
Offline strategies for HTML5 web applications - frOSCon8
Stephan Hochdörfer
 
Offline Strategies for HTML5 Web Applications - oscon13
Offline Strategies for HTML5 Web Applications - oscon13Offline Strategies for HTML5 Web Applications - oscon13
Offline Strategies for HTML5 Web Applications - oscon13
Stephan Hochdörfer
 
Offline Strategien für HTML5 Web Applikationen - dwx13
Offline Strategien für HTML5 Web Applikationen - dwx13 Offline Strategien für HTML5 Web Applikationen - dwx13
Offline Strategien für HTML5 Web Applikationen - dwx13
Stephan Hochdörfer
 
Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13
Stephan Hochdörfer
 
Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13
Stephan Hochdörfer
 
Offline Strategies for HTML5 Web Applications - ipc13
Offline Strategies for HTML5 Web Applications - ipc13Offline Strategies for HTML5 Web Applications - ipc13
Offline Strategies for HTML5 Web Applications - ipc13
Stephan Hochdörfer
 
Offline-Strategien für HTML5 Web Applikationen - wmka
Offline-Strategien für HTML5 Web Applikationen - wmkaOffline-Strategien für HTML5 Web Applikationen - wmka
Offline-Strategien für HTML5 Web Applikationen - wmka
Stephan Hochdörfer
 
Offline-Strategien für HTML5 Web Applikationen - bedcon13
Offline-Strategien für HTML5 Web Applikationen - bedcon13Offline-Strategien für HTML5 Web Applikationen - bedcon13
Offline-Strategien für HTML5 Web Applikationen - bedcon13
Stephan Hochdörfer
 
Offline strategies for HTML5 web applications - ConFoo13
Offline strategies for HTML5 web applications - ConFoo13Offline strategies for HTML5 web applications - ConFoo13
Offline strategies for HTML5 web applications - ConFoo13
Stephan Hochdörfer
 
Offline-Strategien für HTML5Web Applikationen - WMMRN12
Offline-Strategien für HTML5Web Applikationen - WMMRN12Offline-Strategien für HTML5Web Applikationen - WMMRN12
Offline-Strategien für HTML5Web Applikationen - WMMRN12
Stephan Hochdörfer
 
Offline strategies for HTML5 web applications - IPC12
Offline strategies for HTML5 web applications - IPC12Offline strategies for HTML5 web applications - IPC12
Offline strategies for HTML5 web applications - IPC12
Stephan Hochdörfer
 
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12
Stephan Hochdörfer
 
Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012
Stephan Hochdörfer
 
Wie Software-Generatoren die Welt verändern können - Herbstcampus12
Wie Software-Generatoren die Welt verändern können - Herbstcampus12Wie Software-Generatoren die Welt verändern können - Herbstcampus12
Wie Software-Generatoren die Welt verändern können - Herbstcampus12
Stephan Hochdörfer
 
Testing untestable code - Herbstcampus12
Testing untestable code - Herbstcampus12Testing untestable code - Herbstcampus12
Testing untestable code - Herbstcampus12
Stephan Hochdörfer
 
Introducing a Software Generator Framework - JAZOON12
Introducing a Software Generator Framework - JAZOON12Introducing a Software Generator Framework - JAZOON12
Introducing a Software Generator Framework - JAZOON12
Stephan Hochdörfer
 

Plus de Stephan Hochdörfer (20)

Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
 
Phing for power users - frOSCon8
Phing for power users - frOSCon8Phing for power users - frOSCon8
Phing for power users - frOSCon8
 
Offline strategies for HTML5 web applications - frOSCon8
Offline strategies for HTML5 web applications - frOSCon8Offline strategies for HTML5 web applications - frOSCon8
Offline strategies for HTML5 web applications - frOSCon8
 
Offline Strategies for HTML5 Web Applications - oscon13
Offline Strategies for HTML5 Web Applications - oscon13Offline Strategies for HTML5 Web Applications - oscon13
Offline Strategies for HTML5 Web Applications - oscon13
 
Offline Strategien für HTML5 Web Applikationen - dwx13
Offline Strategien für HTML5 Web Applikationen - dwx13 Offline Strategien für HTML5 Web Applikationen - dwx13
Offline Strategien für HTML5 Web Applikationen - dwx13
 
Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13
 
Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13
 
Offline Strategies for HTML5 Web Applications - ipc13
Offline Strategies for HTML5 Web Applications - ipc13Offline Strategies for HTML5 Web Applications - ipc13
Offline Strategies for HTML5 Web Applications - ipc13
 
Offline-Strategien für HTML5 Web Applikationen - wmka
Offline-Strategien für HTML5 Web Applikationen - wmkaOffline-Strategien für HTML5 Web Applikationen - wmka
Offline-Strategien für HTML5 Web Applikationen - wmka
 
Offline-Strategien für HTML5 Web Applikationen - bedcon13
Offline-Strategien für HTML5 Web Applikationen - bedcon13Offline-Strategien für HTML5 Web Applikationen - bedcon13
Offline-Strategien für HTML5 Web Applikationen - bedcon13
 
A Phing fairy tale - ConFoo13
A Phing fairy tale - ConFoo13A Phing fairy tale - ConFoo13
A Phing fairy tale - ConFoo13
 
Offline strategies for HTML5 web applications - ConFoo13
Offline strategies for HTML5 web applications - ConFoo13Offline strategies for HTML5 web applications - ConFoo13
Offline strategies for HTML5 web applications - ConFoo13
 
Offline-Strategien für HTML5Web Applikationen - WMMRN12
Offline-Strategien für HTML5Web Applikationen - WMMRN12Offline-Strategien für HTML5Web Applikationen - WMMRN12
Offline-Strategien für HTML5Web Applikationen - WMMRN12
 
Testing untestable code - IPC12
Testing untestable code - IPC12Testing untestable code - IPC12
Testing untestable code - IPC12
 
Offline strategies for HTML5 web applications - IPC12
Offline strategies for HTML5 web applications - IPC12Offline strategies for HTML5 web applications - IPC12
Offline strategies for HTML5 web applications - IPC12
 
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12
 
Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012
 
Wie Software-Generatoren die Welt verändern können - Herbstcampus12
Wie Software-Generatoren die Welt verändern können - Herbstcampus12Wie Software-Generatoren die Welt verändern können - Herbstcampus12
Wie Software-Generatoren die Welt verändern können - Herbstcampus12
 
Testing untestable code - Herbstcampus12
Testing untestable code - Herbstcampus12Testing untestable code - Herbstcampus12
Testing untestable code - Herbstcampus12
 
Introducing a Software Generator Framework - JAZOON12
Introducing a Software Generator Framework - JAZOON12Introducing a Software Generator Framework - JAZOON12
Introducing a Software Generator Framework - JAZOON12
 

Dernier

Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
amitlee9823
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
daisycvs
 
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
lizamodels9
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
lizamodels9
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
amitlee9823
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
amitlee9823
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
dlhescort
 

Dernier (20)

Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
Call Girls Electronic City Just Call 👗 7737669865 👗 Top Class Call Girl Servi...
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
Falcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business GrowthFalcon Invoice Discounting: Empowering Your Business Growth
Falcon Invoice Discounting: Empowering Your Business Growth
 
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdfDr. Admir Softic_ presentation_Green Club_ENG.pdf
Dr. Admir Softic_ presentation_Green Club_ENG.pdf
 
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
Quick Doctor In Kuwait +2773`7758`557 Kuwait Doha Qatar Dubai Abu Dhabi Sharj...
 
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
Call Girls From Raj Nagar Extension Ghaziabad❤️8448577510 ⊹Best Escorts Servi...
 
Falcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in indiaFalcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in india
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Falcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business PotentialFalcon Invoice Discounting: Unlock Your Business Potential
Falcon Invoice Discounting: Unlock Your Business Potential
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
 
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
Call Girls Kengeri Satellite Town Just Call 👗 7737669865 👗 Top Class Call Gir...
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
 
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort ServiceMalegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
Malegaon Call Girls Service ☎ ️82500–77686 ☎️ Enjoy 24/7 Escort Service
 
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Nelamangala Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
Call Girls Jp Nagar Just Call 👗 7737669865 👗 Top Class Call Girl Service Bang...
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
Call Girls Zirakpur👧 Book Now📱7837612180 📞👉Call Girl Service In Zirakpur No A...
 
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
Call Girls in Delhi, Escort Service Available 24x7 in Delhi 959961-/-3876
 

Real World Dependency Injection - phpday

  • 1. Real World Dependency Injection Stephan Hochdörfer, bitExpert AG
  • 2. Real World Dependency Injection About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP since 1999  S.Hochdoerfer@bitExpert.de  @shochdoerfer
  • 3. Real World Dependency Injection What are Dependencies?
  • 4. Real World Dependency Injection What are Dependencies? Application Framework add. Libraries
  • 5. Real World Dependency Injection What are Dependencies? Controller PHP extensions Service / Model Utils Datastore(s)
  • 6. Real World Dependency Injection Are Dependencies bad?
  • 7. Real World Dependency Injection Are Dependencies bad? Dependencies are not bad! They are useful!
  • 8. Real World Dependency Injection Are Dependencies bad? Hard-coded dependencies are bad!
  • 9. Real World Dependency Injection Tightly coupled code
  • 10. Real World Dependency Injection No reuse of components
  • 11. Real World Dependency Injection No isolation, not testable!
  • 12. Real World Dependency Injection Development gets overcomplicated!
  • 13. Real World Dependency Injection s „new“ is evil!
  • 14. Real World Dependency Injection „new“ is evil! <?php class DeletePage extends Mvc_Action_AAction { private $pageManager; public function __construct() { $this->pageManager = new PageManager(); } protected function execute(Mvc_Request $request) { $this->pageManager->delete( (int) $request->get('pageId') ); } }
  • 15. Real World Dependency Injection „new“ is evil! <?php class DeletePage extends Mvc_Action_AAction { private $pageManager; public function __construct(PageManager $pm) { $this->pageManager = $pm; } protected function execute(Mvc_Request $request) { $this->pageManager->delete( (int) $request->get('pageId') ); } }
  • 16. Real World Dependency Injection "High-level modules should not depend on low-level modules. Both should depend on abstractions." Robert C. Martin
  • 17. Real World Dependency Injection Interfaces act as contracts
  • 18. Real World Dependency Injection „new“ is evil! <?php class DeletePage extends Mvc_Action_AAction { private $pageManager; public function __construct(IPageManager $pm) { $this->pageManager = $pm; } protected function execute(Mvc_Request $request) { $this->pageManager->delete( (int) $request->get('pageId') ); } }
  • 19. Real World Dependency Injection How to Manage Dependencies?
  • 20. Real World Dependency Injection Manually resolve dependencies?
  • 21. Real World Dependency Injection Automatic wiring required! Simple Container vs. Full stacked DI Framework
  • 22. Real World Dependency Injection What is Dependency Injection?
  • 23. Real World Dependency Injection What is Dependency Injection? Consumer
  • 24. Real World Dependency Injection What is Dependency Injection? Consumer Dependencies
  • 25. Real World Dependency Injection What is Dependency Injection? Consumer Dependencies Container
  • 26. Real World Dependency Injection What is Dependency Injection? Consumer Dependencies Container
  • 27. Real World Dependency Injection How to inject a Dependency?
  • 28. Real World Dependency Injection Constructor Injection <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $sampleDao; public function __construct(ISampleDao $sampleDao) { $this->sampleDao = $sampleDao; } }
  • 29. Real World Dependency Injection Setter injection <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $sampleDao; public function setSampleDao(ISampleDao $sampleDao){ $this->sampleDao = $sampleDao; } }
  • 30. Real World Dependency Injection Interface injection <?php interface IApplicationContextAware { public function setCtx(IApplicationContext $ctx); }
  • 31. Real World Dependency Injection Interface injection <?php class MySampleService implements IMySampleService, IApplicationContextAware { /** * @var IApplicationContext */ private $ctx; public function setCtx(IApplicationContext $ctx) { $this->ctx = $ctx; } }
  • 32. Real World Dependency Injection How to wire it all up?
  • 33. Real World Dependency Injection Annotation based wiring <?php class MySampleService implements IMySampleService { private $sampleDao; /** * @Inject */ public function __construct(ISampleDao $sampleDao) { $this->sampleDao = $sampleDao; } }
  • 34. Real World Dependency Injection Annotation based wiring <?php class MySampleService implements IMySampleService { private $sampleDao; /** * @Inject * @Named('TheSampleDao') */ public function __construct(ISampleDao $sampleDao) { $this->sampleDao = $sampleDao; } }
  • 35. Real World Dependency Injection External configuration - XML <?xml version="1.0" encoding="UTF-8" ?> <beans> <bean id="SampleDao" class="SampleDao"> <constructor-arg value="app_sample" /> <constructor-arg value="iSampleId" /> <constructor-arg value="BoSample" /> </bean> <bean id="SampleService" class="MySampleService"> <constructor-arg ref="SampleDao" /> </bean> </beans>
  • 36. Real World Dependency Injection External configuration - YAML services: SampleDao: class: SampleDao arguments: ['app_sample', 'iSampleId', 'BoSample'] SampleService: class: SampleService arguments: [@SampleDao]
  • 37. Real World Dependency Injection External configuration - PHP <?php class BeanCache extends Beanfactory_Container_PHP { protected function createSampleDao() { $oBean = new SampleDao('app_sample', 'iSampleId', 'BoSample'); return $oBean; } protected function createMySampleService() { $oBean = new MySampleService( $this->getBean('SampleDao') ); return $oBean; } }
  • 38. Real World Dependency Injection Ready to unlock the door?
  • 39. Real World Dependency Injection Unittesting made easy
  • 40. Real World Dependency Injection Unittesting made easy <?php require_once 'PHPUnit/Framework.php'; class ServiceTest extends PHPUnit_Framework_TestCase { public function testSampleService() { // set up dependencies $sampleDao = $this->getMock('ISampleDao'); $service = new MySampleService($sampleDao); // run test case $return = $service->doWork(); // check assertions $this->assertTrue($return); } }
  • 41. Real World Dependency Injection One class, multiple configurations
  • 42. Real World Dependency Injection One class, multiple configurations Page Exporter Page Exporter Released / /Published Released Published Workingcopy Workingcopy Pages Pages Pages Pages
  • 43. Real World Dependency Injection One class, multiple configurations <?php abstract class PageExporter { protected function setPageDao(IPageDao $pageDao) { $this->pageDao = $pageDao; } }
  • 44. Real World Dependency Injection One class, multiple configurations <?php abstract class PageExporter { protected function setPageDao(IPageDao $pageDao) { $this->pageDao = $pageDao; } } Remember: The contract!
  • 45. Real World Dependency Injection One class, multiple configurations <?php class PublishedPageExporter extends PageExporter { public function __construct() { $this->setPageDao(new PublishedPageDao()); } } class WorkingCopyPageExporter extends PageExporter { public function __construct() { $this->setPageDao(new WorkingCopyPageDao()); } }
  • 46. Real World Dependency Injection One class, multiple configurations "Only deleted code is good code!" Oliver Gierke
  • 47. Real World Dependency Injection One class, multiple configurations <?php class PageExporter { public function __construct(IPageDao $pageDao) { $this->pageDao = $pageDao; } }
  • 48. Real World Dependency Injection One class, multiple configurations <?xml version="1.0" encoding="UTF-8" ?> <beans> <bean id="ExportLive" class="PageExporter"> <constructor-arg ref="PublishedPageDao" /> </bean> <bean id="ExportWorking" class="PageExporter"> <constructor-arg ref="WorkingCopyPageDao" /> </bean> </beans>
  • 49. Real World Dependency Injection One class, multiple configurations <?php // create ApplicationContext instance $ctx = new ApplicationContext(); // retrieve live exporter $exporter = $ctx->getBean('ExportLive'); // retrieve working copy exporter $exporter = $ctx->getBean('ExportWorking');
  • 50. Real World Dependency Injection One class, multiple configurations II
  • 51. Real World Dependency Injection One class, multiple configurations II http://editor.loc/page/[id]/headline/ http://editor.loc/page/[id]/content/ http://editor.loc/page/[id]/teaser/
  • 52. Real World Dependency Injection One class, multiple configurations II <?php class EditPart extends Mvc_Action_AFormAction { private $pagePartsManager; private $type; public function __construct(IPagePartsManager $pm) { $this->pagePartsManager = $pm; } public function setType($ptype) { $this->type = (int) $type; } protected function process(Bo_ABo $formBackObj) { } }
  • 53. Real World Dependency Injection One class, multiple configurations II <?xml version="1.0" encoding="UTF-8" ?> <beans> <bean id="EditHeadline" class="EditPart"> <constructor-arg ref="PagePartDao" /> <property name="Type" const="PType::Headline" /> </bean> <bean id="EditContent" class="EditPart"> <constructor-arg ref="PagePartDao" /> <property name="Type" const="PType::Content" /> </bean> </beans>
  • 54. Real World Dependency Injection Mocking external service access
  • 55. Real World Dependency Injection Mocking external service access WS- WS- Booking service Booking service Webservice Webservice Connector Connector
  • 56. Real World Dependency Injection Mocking external service access WS- WS- Booking service Booking service Webservice Webservice Connector Connector Remember: The contract!
  • 57. Real World Dependency Injection Mocking external service access FS- FS- Booking service Booking service Filesystem Filesystem Connector Connector
  • 58. Real World Dependency Injection Mocking external service access FS- FS- Booking service Booking service Filesystem Filesystem Connector Connector fullfills the contract!
  • 59. Real World Dependency Injection Clean, readable code
  • 60. Real World Dependency Injection Clean, readable code <?php class DeletePage extends Mvc_Action_AAction { private $pageManager; public function __construct(IPageManager $pm) { $this->pageManager = $pm; } protected function execute(Mvc_Request $request) { $this->pageManager->delete( (int) $request->get('pageId')); return new ModelAndView($this->getSuccessView()); } }
  • 61. Real World Dependency Injection No framework dependency
  • 62. Real World Dependency Injection No framework dependency <?php class MySampleService implements IMySampleService { private $sampleDao; public function __construct(ISampleDao $sampleDao) { $this->sampleDao = $sampleDao; } public function getSample($sampleId) { try { return $this->sampleDao->readById($sampleId); } catch(DaoException $exception) {} } }
  • 63. Real World Dependency Injection Benefits Loose coupling, reuse of components!
  • 64. Real World Dependency Injection Benefits Can reduce the amount of code!
  • 65. Real World Dependency Injection Benefits Helps developers to understand the code!
  • 66. Real World Dependency Injection Cons – No JSR330 for PHP Bucket, Crafty, FLOW3, Imind_Context, PicoContainer, Pimple, Phemto, Stubbles, Symfony 2.0, Sphicy, Solar, Substrate, XJConf, Yadif, Zend_Di (Proposal), Lion Framework, Spiral Framework, Xyster Framework, …
  • 67. Real World Dependency Injection Cons – Developers need mindshift Configuration ↔ Runtime