SlideShare una empresa de Scribd logo
1 de 34
Descargar para leer sin conexión
Real World Dependency Injection
          Stephan Hochdörfer, bitExpert AG




"Dependency Injection is a key element of agile architecture"
                     Ward Cunningham
About me
 Founder of bitExpert AG, Mannheim

 Field of duty
         
           Department Manager of Research Labs
         
           Head of Development for bitFramework

 Focusing on
        
          PHP
        
          Generative Programming


    Contact me
          
            @shochdoerfer
          
            S.Hochdoerfer@bitExpert.de
Agenda
1.   What is Dependency Injection?

2.   Real World examples

3.   Pros & Cons

4.   Questions
What is Dependency Injection?




     "Dependency Injection is probably one of the most dead simple
      design pattern [...] but it is also one of the most difficult one to
                                 explain well."
                                Fabien Potencier
What is Dependency Injection?




     "Dependency Injection is probably one of the most dead simple
      design pattern [...] but it is also one of the most difficult one to
                                 explain well."
                                Fabien Potencier
What is Dependency Injection?



What is Dependency Injection?

 Popularized by Martin Fowler in 2004

 Technique for supplying external dependencies to a component

 Main idea: Ask for things, do not look for things!

 Three elements
        
          Dependant / Consumer
        
          Dependencies, e.g. service objects
        
          Injector / Container
What is Dependency Injection?



Problems of Dependencies

 Code is very tightly coupled
        
          Hard to re-use code
        
          Hard to test code, no isolation possible

 Difficult to maintain
         
            What is affected when code changes?
         
            Boilerplate configuration code within Business logic
What is Dependency Injection?



Simple Dependency



                                Main    Required
                                class    class
What is Dependency Injection?



Complex Dependency


                                           Required
                                            class
                     Main       Required
                     class       class
                                           Required
                                            class
What is Dependency Injection?



Very complex Dependency
                                           Database

                                Required
                                 class
                                            External
  Main             Required
                                            resource
  class             class
                                Required
                                 class




                   Required     Required
                                            Webservice
                    class        class
What is Dependency Injection?



Very complex Dependency
                                           Database

                                Required
                                 class
                                            External
  Main             Required
                                            resource
  class             class
                                Required
                                 class




                   Required     Required
                                            Webservice
                    class        class
What is Dependency Injection?



Very complex Dependency
                                           Database

                                Required
                                 class
                                            External
  Main             Required
                                            resource
  class             class
                                Required
                                 class




                   Required     Required
                                            Webservice
                    class        class
What is Dependency Injection?



How to Manage Dependencies?
What is Dependency Injection?



How to Manage Dependencies?




               You don`t need to. The Framework does it all!
What is Dependency Injection?



Types of Dependency Injection

Type 1: Interface injection

 <?php

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

      public function setApplicationContext(IApplicationContext $poCtx) {
        $this->oCtx = $poCtx;
      }
 }
 ?>
What is Dependency Injection?



Types of Dependency Injection

Type 2: Setter injection

 <?php

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

   public function setSampleDao(ISampleDao $poSampleDao) {
     $this->oSampleDao = $poSamleDao;
   }
 }
 ?>
What is Dependency Injection?



Types of Dependency Injection

Type 3: Constructor injection

 <?php

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

   public function __construct(ISampleDao $poSampleDao) {
     $this->oSampleDao = $poSamleDao;
   }
 }
 ?>
What is Dependency Injection?



 Configuration Types

  Type 1: Annotations

  <?php

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

       /**
         * @Inject
         */
       public function __construct(ISampleDao $poSampleDao) {
          $this->oSampleDao = $poSamleDao;
       }
  }
  ?>
What is Dependency Injection?



Configuration Types

Type 2: External configuration via XML, JSON, YAML, ...

 <?xml version="1.0" encoding="UTF-8" ?>
 <beans xmlns="http://www.bitexpert.de/schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.bitexpert.de/schema/
 http://www.bitexpert.de/schema/bitFramework-beans.xsd">

     <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>
What is Dependency Injection?



Configuration Types

 Type 3: PHP Configuration

 <?php
 class BeanCache extends bF_Beanfactory_Container_PHP_ACache {
     protected function createSampleDao() {
          $oBean = new SampleDao('app_sample', 'iSampleId', 'BoSample');
          return array("oBean" => $oBean, "bSingleton" => "1");
     }

      protected function createMySampleService() {
          $oBean = new MySampleService($this->getBean('SampleDao'));
          return array("oBean" => $oBean, "bSingleton" => "1");
      }
 ?>
Agenda
1.   What is Dependency Injection?

2.   Real World examples

3.   Pros & Cons

4.   Questions
Real world examples




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



Unittesting made easy

 <?php
 require_once 'PHPUnit/Framework.php';

 class ServiceTest extends PHPUnit_Framework_TestCase {
     public function testSampleService() {
         $oSampleDao = $this->getMock('ISampleDao');

          // run test case
          $oService = new MySampleService($oSampleDao);
          $bReturn = $oService->doWork();

          // check assertions
          $this->assertTrue($bReturn);
      }
 }
 ?>
Real World examples



One class, multiple configurations

                                            Implementation

                         Live                                        Working




<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.bitexpert.de/schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.bitexpert.de/schema/
http://www.bitexpert.de/schema/bitFramework-beans.xsd">

     <bean id="ExportLive" class="MyApp_Service_ExportManager">
          <constructor-arg ref="DAOPageLive" />
     </bean>

     <bean id="ExportWorking" class="MyApp_Service_ExportManager">
          <constructor-arg ref="DAOPageWorking" />
     </bean>

</beans>
Real World examples



Mocking external services


       Consumer                   Connector                  Webservice


                IConnector interface


<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.bitexpert.de/schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.bitexpert.de/schema/
http://www.bitexpert.de/schema/bitFramework-beans.xsd">

    <bean id="Consumer" class="MyApp_Service_Consumer">
         <constructor-arg ref="Connector" />
    </bean>

</beans>
Real World examples



Mocking external services

                                  alternative                    Local
       Consumer
                                  Connector                  filesystem


                IConnector interface


<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.bitexpert.de/schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.bitexpert.de/schema/
http://www.bitexpert.de/schema/bitFramework-beans.xsd">

     <bean id="Consumer" class="MyApp_Service_Consumer">
          <constructor-arg ref="AltConnector" />
     </bean>

</beans>
Real World examples



Clean, readable code
<?php

class Promio_Action_News_Delete extends bF_Mvc_Action_AAction {
     /**
      * @var Promio_Service_INewsManager
      */
     private $oNewsManager;

     public function __construct(Promio_Service_INewsManager $poNewsManager) {
          $this->oNewsManager = $poNewsManager;
     }

     protected function execute(bF_Mvc_Request $poRequest) {
          try {
               $this->oNewsManager->delete((int) $poRequest->getVar('iNewsId'));
          }
          catch(bF_Service_ServiceException $oException) {
          }

         $oMaV = new bF_Mvc_ModelAndView($this->getSuccessView(), true);
         return $oMaV;
     }
}
?>
Real World examples



No framework dependency
<?php

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

  public function __construct(ISampleDao $poSampleDao) {
     $this->oSampleDao = $poSamleDao;
  }

  public function getSample($piSampleId) {
     try {
       return $this->oSampleDao->readByPrimaryKey((int) $piSampleId);
     }
     catch(DaoException $oException) {
     }
  }
}
?>
Real World examples



Cache, Cache, Cache!
 180



 160



 140



 120



 100



  80



  60



  40



  20



  0
           XML no Caching   XML with Caching     PHP            PHP compiled

       Requests per Second meassured via Apache HTTP server benchmarking tool
Agenda
1.   What is Dependency Injection?

2.   Real World Examples

3.   Pros & Cons

4.   Questions
Pros & Cons



Benefits

 Good for agile development
        
          Reducing amount of code
        
          Helpful for Unit testing

 Loose coupling
        
          Least intrusive mechanism
        
          Switching implementations by changing configuration

 Clean view on the code
        
          Separate configuration from code
        
          Getting rid of boilerpate configuration code
Pros & Cons



Cons

 To many different implementations, no standard today!
       
         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, …
       
         No JSR 330 for PHP

 Simple Containers vs. fully-stacked Framework
        
          No dependency from application to Container!

 Developers need to be aware of configuration ↔ runtime
Agenda
1.   What is Dependency Injection?

2.   Pros & Cons

3.   Real World examples

4.   Questions
Real World Dependency Injection - PFCongres 2010

Más contenido relacionado

La actualidad más candente

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 EditionStephan Hochdörfer
 
Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Stephan Hochdörfer
 
Create Your Own Framework by Fabien Potencier
Create Your Own Framework by Fabien PotencierCreate Your Own Framework by Fabien Potencier
Create Your Own Framework by Fabien PotencierHimel Nag Rana
 
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 ContainerDiego Lewin
 
Authentication with zend framework
Authentication with zend frameworkAuthentication with zend framework
Authentication with zend frameworkGeorge Mihailov
 
ioc-castle-windsor
ioc-castle-windsorioc-castle-windsor
ioc-castle-windsorAmir Barylko
 
Enterprise Security API (ESAPI) Java - Java User Group San Antonio
Enterprise Security API (ESAPI) Java - Java User Group San AntonioEnterprise Security API (ESAPI) Java - Java User Group San Antonio
Enterprise Security API (ESAPI) Java - Java User Group San AntonioDenim Group
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Stefano Valle
 
Contexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platformContexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platformBozhidar Bozhanov
 
Implementing security routines with zf2
Implementing security routines with zf2Implementing security routines with zf2
Implementing security routines with zf2Er Galvão Abbott
 
Advanced java interview questions
Advanced java interview questionsAdvanced java interview questions
Advanced java interview questionsrithustutorials
 
Seven Versions of One Web Application
Seven Versions of One Web ApplicationSeven Versions of One Web Application
Seven Versions of One Web ApplicationYakov Fain
 
Java j2ee interview_questions
Java j2ee interview_questionsJava j2ee interview_questions
Java j2ee interview_questionsppratik86
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204ealio
 
Zend Studio Tips and Tricks
Zend Studio Tips and TricksZend Studio Tips and Tricks
Zend Studio Tips and TricksRoy Ganor
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2vikram singh
 

La actualidad más candente (20)

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
 
Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010
 
IoC with PHP
IoC with PHPIoC with PHP
IoC with PHP
 
Create Your Own Framework by Fabien Potencier
Create Your Own Framework by Fabien PotencierCreate Your Own Framework by Fabien Potencier
Create Your Own Framework by Fabien Potencier
 
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
 
Authentication with zend framework
Authentication with zend frameworkAuthentication with zend framework
Authentication with zend framework
 
ioc-castle-windsor
ioc-castle-windsorioc-castle-windsor
ioc-castle-windsor
 
Enterprise Security API (ESAPI) Java - Java User Group San Antonio
Enterprise Security API (ESAPI) Java - Java User Group San AntonioEnterprise Security API (ESAPI) Java - Java User Group San Antonio
Enterprise Security API (ESAPI) Java - Java User Group San Antonio
 
JDT Fundamentals 2010
JDT Fundamentals 2010JDT Fundamentals 2010
JDT Fundamentals 2010
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
 
Contexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platformContexts and Dependency Injection for the JavaEE platform
Contexts and Dependency Injection for the JavaEE platform
 
Implementing security routines with zf2
Implementing security routines with zf2Implementing security routines with zf2
Implementing security routines with zf2
 
Advanced java interview questions
Advanced java interview questionsAdvanced java interview questions
Advanced java interview questions
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
 
Seven Versions of One Web Application
Seven Versions of One Web ApplicationSeven Versions of One Web Application
Seven Versions of One Web Application
 
Java j2ee interview_questions
Java j2ee interview_questionsJava j2ee interview_questions
Java j2ee interview_questions
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
 
iOS API Design
iOS API DesigniOS API Design
iOS API Design
 
Zend Studio Tips and Tricks
Zend Studio Tips and TricksZend Studio Tips and Tricks
Zend Studio Tips and Tricks
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2
 

Destacado

Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRNFacebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRNStephan 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 - IPC12Stephan Hochdörfer
 
Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Stephan 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 - WMMRN12Stephan Hochdörfer
 
The state of DI in PHP - phpbnl12
The state of DI in PHP - phpbnl12The state of DI in PHP - phpbnl12
The state of DI in PHP - phpbnl12Stephan 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! - WDC12Stephan Hochdörfer
 

Destacado (6)

Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRNFacebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
 
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
 
Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13Phing for power users - dpc_uncon13
Phing for power users - dpc_uncon13
 
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
 
The state of DI in PHP - phpbnl12
The state of DI in PHP - phpbnl12The state of DI in PHP - phpbnl12
The state of DI in PHP - phpbnl12
 
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
 

Similar a Real World Dependency Injection - PFCongres 2010

springtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfspringtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfBruceLee275640
 
Spring training
Spring trainingSpring training
Spring trainingTechFerry
 
Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Stephan Hochdörfer
 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAOAnushaNaidu
 
Techlunch - Dependency Injection with Vaadin
Techlunch - Dependency Injection with VaadinTechlunch - Dependency Injection with Vaadin
Techlunch - Dependency Injection with VaadinPeter Lehto
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 javatwo2011
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoCDennis Loktionov
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkASG
 
Writing Testable Code
Writing Testable CodeWriting Testable Code
Writing Testable Codejameshalsall
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-onhomeworkping7
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignJames Phillips
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptxNourhanTarek23
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy
 
The Spring Framework: A brief introduction to Inversion of Control
The Spring Framework:A brief introduction toInversion of ControlThe Spring Framework:A brief introduction toInversion of Control
The Spring Framework: A brief introduction to Inversion of ControlVisualBee.com
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversionchhabraravish23
 

Similar a Real World Dependency Injection - PFCongres 2010 (20)

springtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfspringtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdf
 
Spring training
Spring trainingSpring training
Spring training
 
Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13
 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAO
 
Techlunch - Dependency Injection with Vaadin
Techlunch - Dependency Injection with VaadinTechlunch - Dependency Injection with Vaadin
Techlunch - Dependency Injection with Vaadin
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoC
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring session
Spring sessionSpring session
Spring session
 
Writing Testable Code
Writing Testable CodeWriting Testable Code
Writing Testable Code
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-on
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptx
 
JavaCro'15 - Web UI best practice integration with Java EE 7 - Peter Lehto
JavaCro'15 - Web UI best practice integration with Java EE 7 - Peter LehtoJavaCro'15 - Web UI best practice integration with Java EE 7 - Peter Lehto
JavaCro'15 - Web UI best practice integration with Java EE 7 - Peter Lehto
 
Swiz DAO
Swiz DAOSwiz DAO
Swiz DAO
 
Spring
SpringSpring
Spring
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with NinjectOleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
 
Hybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbaiHybernat and structs, spring classes in mumbai
Hybernat and structs, spring classes in mumbai
 
The Spring Framework: A brief introduction to Inversion of Control
The Spring Framework:A brief introduction toInversion of ControlThe Spring Framework:A brief introduction toInversion of Control
The Spring Framework: A brief introduction to Inversion of Control
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversion
 

Más 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 - frOSCon8Stephan 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 - frOSCon8Stephan 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 - oscon13Stephan 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 - dpc13Stephan 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 - ipc13Stephan 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 - wmkaStephan 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 - bedcon13Stephan Hochdörfer
 
Testing untestable code - ConFoo13
Testing untestable code - ConFoo13Testing untestable code - ConFoo13
Testing untestable code - ConFoo13Stephan 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 - ConFoo13Stephan 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 - pfCongres2012Stephan 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 - Herbstcampus12Stephan Hochdörfer
 
Testing untestable code - Herbstcampus12
Testing untestable code - Herbstcampus12Testing untestable code - Herbstcampus12
Testing untestable code - Herbstcampus12Stephan Hochdörfer
 
Introducing a Software Generator Framework - JAZOON12
Introducing a Software Generator Framework - JAZOON12Introducing a Software Generator Framework - JAZOON12
Introducing a Software Generator Framework - JAZOON12Stephan Hochdörfer
 
Managing variability in software applications - scandev12
Managing variability in software applications - scandev12Managing variability in software applications - scandev12
Managing variability in software applications - scandev12Stephan Hochdörfer
 

Más 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
 
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
 
Testing untestable code - ConFoo13
Testing untestable code - ConFoo13Testing untestable code - ConFoo13
Testing untestable code - ConFoo13
 
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
 
Testing untestable code - IPC12
Testing untestable code - IPC12Testing untestable code - IPC12
Testing untestable code - IPC12
 
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
 
The state of DI - DPC12
The state of DI - DPC12The state of DI - DPC12
The state of DI - DPC12
 
Separation of concerns - DPC12
Separation of concerns - DPC12Separation of concerns - DPC12
Separation of concerns - DPC12
 
Managing variability in software applications - scandev12
Managing variability in software applications - scandev12Managing variability in software applications - scandev12
Managing variability in software applications - scandev12
 

Último

Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Último (20)

Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

Real World Dependency Injection - PFCongres 2010

  • 1. Real World Dependency Injection Stephan Hochdörfer, bitExpert AG "Dependency Injection is a key element of agile architecture" Ward Cunningham
  • 2. About me  Founder of bitExpert AG, Mannheim  Field of duty  Department Manager of Research Labs  Head of Development for bitFramework  Focusing on  PHP  Generative Programming  Contact me  @shochdoerfer  S.Hochdoerfer@bitExpert.de
  • 3. Agenda 1. What is Dependency Injection? 2. Real World examples 3. Pros & Cons 4. Questions
  • 4. What is Dependency Injection? "Dependency Injection is probably one of the most dead simple design pattern [...] but it is also one of the most difficult one to explain well." Fabien Potencier
  • 5. What is Dependency Injection? "Dependency Injection is probably one of the most dead simple design pattern [...] but it is also one of the most difficult one to explain well." Fabien Potencier
  • 6. What is Dependency Injection? What is Dependency Injection?  Popularized by Martin Fowler in 2004  Technique for supplying external dependencies to a component  Main idea: Ask for things, do not look for things!  Three elements  Dependant / Consumer  Dependencies, e.g. service objects  Injector / Container
  • 7. What is Dependency Injection? Problems of Dependencies  Code is very tightly coupled  Hard to re-use code  Hard to test code, no isolation possible  Difficult to maintain  What is affected when code changes?  Boilerplate configuration code within Business logic
  • 8. What is Dependency Injection? Simple Dependency Main Required class class
  • 9. What is Dependency Injection? Complex Dependency Required class Main Required class class Required class
  • 10. What is Dependency Injection? Very complex Dependency Database Required class External Main Required resource class class Required class Required Required Webservice class class
  • 11. What is Dependency Injection? Very complex Dependency Database Required class External Main Required resource class class Required class Required Required Webservice class class
  • 12. What is Dependency Injection? Very complex Dependency Database Required class External Main Required resource class class Required class Required Required Webservice class class
  • 13. What is Dependency Injection? How to Manage Dependencies?
  • 14. What is Dependency Injection? How to Manage Dependencies? You don`t need to. The Framework does it all!
  • 15. What is Dependency Injection? Types of Dependency Injection Type 1: Interface injection <?php class MySampleService implements IMySampleService, IApplicationContextAware { /** * @var IApplicationContext */ private $oCtx; public function setApplicationContext(IApplicationContext $poCtx) { $this->oCtx = $poCtx; } } ?>
  • 16. What is Dependency Injection? Types of Dependency Injection Type 2: Setter injection <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $oSampleDao; public function setSampleDao(ISampleDao $poSampleDao) { $this->oSampleDao = $poSamleDao; } } ?>
  • 17. What is Dependency Injection? Types of Dependency Injection Type 3: Constructor injection <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $oSampleDao; public function __construct(ISampleDao $poSampleDao) { $this->oSampleDao = $poSamleDao; } } ?>
  • 18. What is Dependency Injection? Configuration Types Type 1: Annotations <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $oSampleDao; /** * @Inject */ public function __construct(ISampleDao $poSampleDao) { $this->oSampleDao = $poSamleDao; } } ?>
  • 19. What is Dependency Injection? Configuration Types Type 2: External configuration via XML, JSON, YAML, ... <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.bitexpert.de/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bitexpert.de/schema/ http://www.bitexpert.de/schema/bitFramework-beans.xsd"> <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>
  • 20. What is Dependency Injection? Configuration Types Type 3: PHP Configuration <?php class BeanCache extends bF_Beanfactory_Container_PHP_ACache { protected function createSampleDao() { $oBean = new SampleDao('app_sample', 'iSampleId', 'BoSample'); return array("oBean" => $oBean, "bSingleton" => "1"); } protected function createMySampleService() { $oBean = new MySampleService($this->getBean('SampleDao')); return array("oBean" => $oBean, "bSingleton" => "1"); } ?>
  • 21. Agenda 1. What is Dependency Injection? 2. Real World examples 3. Pros & Cons 4. Questions
  • 22. Real world examples "High-level modules should not depend on low-level modules. Both should depend on abstractions." Robert C. Martin
  • 23. Real World examples Unittesting made easy <?php require_once 'PHPUnit/Framework.php'; class ServiceTest extends PHPUnit_Framework_TestCase { public function testSampleService() { $oSampleDao = $this->getMock('ISampleDao'); // run test case $oService = new MySampleService($oSampleDao); $bReturn = $oService->doWork(); // check assertions $this->assertTrue($bReturn); } } ?>
  • 24. Real World examples One class, multiple configurations Implementation Live Working <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.bitexpert.de/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bitexpert.de/schema/ http://www.bitexpert.de/schema/bitFramework-beans.xsd"> <bean id="ExportLive" class="MyApp_Service_ExportManager"> <constructor-arg ref="DAOPageLive" /> </bean> <bean id="ExportWorking" class="MyApp_Service_ExportManager"> <constructor-arg ref="DAOPageWorking" /> </bean> </beans>
  • 25. Real World examples Mocking external services Consumer Connector Webservice IConnector interface <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.bitexpert.de/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bitexpert.de/schema/ http://www.bitexpert.de/schema/bitFramework-beans.xsd"> <bean id="Consumer" class="MyApp_Service_Consumer"> <constructor-arg ref="Connector" /> </bean> </beans>
  • 26. Real World examples Mocking external services alternative Local Consumer Connector filesystem IConnector interface <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.bitexpert.de/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bitexpert.de/schema/ http://www.bitexpert.de/schema/bitFramework-beans.xsd"> <bean id="Consumer" class="MyApp_Service_Consumer"> <constructor-arg ref="AltConnector" /> </bean> </beans>
  • 27. Real World examples Clean, readable code <?php class Promio_Action_News_Delete extends bF_Mvc_Action_AAction { /** * @var Promio_Service_INewsManager */ private $oNewsManager; public function __construct(Promio_Service_INewsManager $poNewsManager) { $this->oNewsManager = $poNewsManager; } protected function execute(bF_Mvc_Request $poRequest) { try { $this->oNewsManager->delete((int) $poRequest->getVar('iNewsId')); } catch(bF_Service_ServiceException $oException) { } $oMaV = new bF_Mvc_ModelAndView($this->getSuccessView(), true); return $oMaV; } } ?>
  • 28. Real World examples No framework dependency <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $oSampleDao; public function __construct(ISampleDao $poSampleDao) { $this->oSampleDao = $poSamleDao; } public function getSample($piSampleId) { try { return $this->oSampleDao->readByPrimaryKey((int) $piSampleId); } catch(DaoException $oException) { } } } ?>
  • 29. Real World examples Cache, Cache, Cache! 180 160 140 120 100 80 60 40 20 0 XML no Caching XML with Caching PHP PHP compiled Requests per Second meassured via Apache HTTP server benchmarking tool
  • 30. Agenda 1. What is Dependency Injection? 2. Real World Examples 3. Pros & Cons 4. Questions
  • 31. Pros & Cons Benefits  Good for agile development  Reducing amount of code  Helpful for Unit testing  Loose coupling  Least intrusive mechanism  Switching implementations by changing configuration  Clean view on the code  Separate configuration from code  Getting rid of boilerpate configuration code
  • 32. Pros & Cons Cons  To many different implementations, no standard today!  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, …  No JSR 330 for PHP  Simple Containers vs. fully-stacked Framework  No dependency from application to Container!  Developers need to be aware of configuration ↔ runtime
  • 33. Agenda 1. What is Dependency Injection? 2. Pros & Cons 3. Real World examples 4. Questions