SlideShare a Scribd company logo
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

More Related Content

What's hot

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
 
Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010Testing untestable Code - PFCongres 2010
Testing untestable Code - PFCongres 2010
Stephan Hochdörfer
 
IoC with PHP
IoC with PHPIoC with PHP
IoC with PHP
Chris Weldon
 
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
Himel 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 Container
Diego Lewin
 
Authentication with zend framework
Authentication with zend frameworkAuthentication with zend framework
Authentication with zend framework
George Mihailov
 
ioc-castle-windsor
ioc-castle-windsorioc-castle-windsor
ioc-castle-windsor
Amir 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 Antonio
Denim Group
 
JDT Fundamentals 2010
JDT Fundamentals 2010JDT Fundamentals 2010
JDT Fundamentals 2010
Olivier Thomann
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
Stefano 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 platform
Bozhidar Bozhanov
 
Implementing security routines with zf2
Implementing security routines with zf2Implementing security routines with zf2
Implementing security routines with zf2
Er Galvão Abbott
 
Advanced java interview questions
Advanced java interview questionsAdvanced java interview questions
Advanced java interview questions
rithustutorials
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
husnara mohammad
 
Seven Versions of One Web Application
Seven Versions of One Web ApplicationSeven Versions of One Web Application
Seven Versions of One Web Application
Yakov Fain
 
Java j2ee interview_questions
Java j2ee interview_questionsJava j2ee interview_questions
Java j2ee interview_questions
ppratik86
 
Spring talk111204
Spring talk111204Spring talk111204
Spring talk111204
ealio
 
iOS API Design
iOS API DesigniOS API Design
iOS API Design
Brian Gesiak
 
Zend Studio Tips and Tricks
Zend Studio Tips and TricksZend Studio Tips and Tricks
Zend Studio Tips and Tricks
Roy Ganor
 
Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2Enterprise java beans(ejb) update 2
Enterprise java beans(ejb) update 2
vikram singh
 

What's hot (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
 

Viewers also liked

Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRNFacebook Apps: Ein Entwicklungsleitfaden - WMMRN
Facebook Apps: Ein Entwicklungsleitfaden - WMMRN
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
 
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-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 - phpbnl12
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! - WDC12Stephan Hochdörfer
 

Viewers also liked (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 to Real World Dependency Injection - PFCongres 2010

springtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdfspringtraning-7024840-phpapp01.pdf
springtraning-7024840-phpapp01.pdf
BruceLee275640
 
Spring training
Spring trainingSpring training
Spring training
TechFerry
 
Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13
Stephan Hochdörfer
 
Spring IOC and DAO
Spring IOC and DAOSpring IOC and DAO
Spring IOC and DAO
AnushaNaidu
 
Techlunch - Dependency Injection with Vaadin
Techlunch - Dependency Injection with VaadinTechlunch - Dependency Injection with Vaadin
Techlunch - Dependency Injection with Vaadin
Peter Lehto
 
Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望 Java EE 與 雲端運算的展望
Java EE 與 雲端運算的展望
javatwo2011
 
Dependency Injection & IoC
Dependency Injection & IoCDependency Injection & IoC
Dependency Injection & IoC
Dennis Loktionov
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
ASG
 
Spring session
Spring sessionSpring session
Spring session
Gamal Shaban
 
Writing Testable Code
Writing Testable CodeWriting Testable Code
Writing Testable Code
jameshalsall
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-on
homeworkping7
 
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
James Phillips
 
Introduction to Spring sec1.pptx
Introduction to Spring sec1.pptxIntroduction to Spring sec1.pptx
Introduction to Spring sec1.pptx
NourhanTarek23
 
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
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Swiz DAO
Swiz DAOSwiz DAO
Swiz DAO
devaraj ns
 
Spring
SpringSpring
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
Oleksandr Valetskyy
 
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
Vibrant Technologies & Computers
 
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
VisualBee.com
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversion
chhabraravish23
 

Similar to 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
 

More from 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
 
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 - 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 - ConFoo13
Stephan Hochdörfer
 
A Phing fairy tale - ConFoo13
A Phing fairy tale - ConFoo13A Phing fairy tale - ConFoo13
A Phing fairy tale - ConFoo13
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
 
Testing untestable code - IPC12
Testing untestable code - IPC12Testing untestable code - IPC12
Testing untestable code - IPC12
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 - 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 - JAZOON12
Stephan Hochdörfer
 
The state of DI - DPC12
The state of DI - DPC12The state of DI - DPC12
The state of DI - DPC12
Stephan Hochdörfer
 
Separation of concerns - DPC12
Separation of concerns - DPC12Separation of concerns - DPC12
Separation of concerns - DPC12
Stephan Hochdörfer
 
Managing variability in software applications - scandev12
Managing variability in software applications - scandev12Managing variability in software applications - scandev12
Managing variability in software applications - scandev12
Stephan Hochdörfer
 

More from 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
 

Recently uploaded

QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!
Tobias Schneck
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
ScyllaDB
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Neo4j
 
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
Fwdays
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
FilipTomaszewski5
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
Fwdays
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdfLee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
leebarnesutopia
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 
AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)
HarpalGohil4
 

Recently uploaded (20)

QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!Containers & AI - Beauty and the Beast!?!
Containers & AI - Beauty and the Beast!?!
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
 
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba"NATO Hackathon Winner: AI-Powered Drug Search",  Taras Kloba
"NATO Hackathon Winner: AI-Powered Drug Search", Taras Kloba
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
"What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w..."What does it really mean for your system to be available, or how to define w...
"What does it really mean for your system to be available, or how to define w...
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdfLee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
Lee Barnes - Path to Becoming an Effective Test Automation Engineer.pdf
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 
AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)AWS Certified Solutions Architect Associate (SAA-C03)
AWS Certified Solutions Architect Associate (SAA-C03)
 

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