Real World Dependency Injection
          Stephan Hochdörfer, bitExpert AG




"Dependency Injection is a key element of a...
Agenda
1.   About me

2.   What is Dependency Injection?

3.   Real World examples

4.   Pros & Cons

5.   Questions
About me
 Stephan Hochdörfer, bitExpert AG

 Department Manager Research Labs

 enjoying PHP since 1999

 S.Hochdoerfe...
What is Dependency Injection?



What is Dependency?
What is Dependency Injection?



What is Dependency?

 Application Layer
        
           Data Access layer
        ...
What is Dependency Injection?



Why are Dependencies bad?
What is Dependency Injection?



Why are Dependencies bad?




                            „new“ is evil!
What is Dependency Injection?



Why are Dependencies bad?

 Tightly coupled code

 Hard to re-use components

 Hard to...
What is Dependency Injection?



Simple Dependency



                                Main    Required
                   ...
What is Dependency Injection?



Complex Dependency


                                           Required
                ...
What is Dependency Injection?



Very complex Dependency
                                           Database

            ...
What is Dependency Injection?



Very complex Dependency
                                           Database

            ...
What is Dependency Injection?



Very complex Dependency
                                           Database

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



How to Manage Dependencies?




          Simple Container      vs.   Full stacked Framew...
What is Dependency Injection?



What is Dependency Injection?
What is Dependency Injection?



What is Dependency Injection?




      Consumer
What is Dependency Injection?



What is Dependency Injection?




      Consumer                  Dependencies
What is Dependency Injection?



What is Dependency Injection?




      Consumer                  Dependencies   Container
What is Dependency Injection?



What is Dependency Injection?




      Consumer                  Dependencies   Container
What is Dependency Injection?



Types of Dependency Injection

Type 1: Constructor injection

 <?php

 class MySampleServ...
What is Dependency Injection?



Types of Dependency Injection

Type 2: Setter injection

 <?php

 class MySampleService i...
What is Dependency Injection?



Types of Dependency Injection

Type 3: Interface injection

 <?php

 class MySampleServic...
What is Dependency Injection?



 Configuration Types

  Type 1: Annotations

  <?php

  class MySampleService implements ...
What is Dependency Injection?



 Configuration Types

  Type 1: Annotations
  <?php

  class MySampleService implements I...
What is Dependency Injection?



Configuration Types

Type 2: External configuration via XML

 <?xml version="1.0" encodin...
What is Dependency Injection?



Configuration Types

Type 2: External configuration via YAML

 services:
   SampleDao:
  ...
What is Dependency Injection?



Configuration Types

 Type 3: PHP Configuration

 <?php
 class BeanCache extends bF_Beanf...
What is Dependency Injection?



Cache, Cache, Cache!
 180



 160



 140



 120



 100



  80



  60



  40



  20...
Real world examples




      "High-level modules should not depend on low-level modules.
                   Both should d...
Real World examples



Unittesting made easy

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

 class ServiceTest extends PH...
Real World examples



One class, multiple configurations

                                            Implementation

   ...
Real World examples



One class, multiple configurations II
<?php
class Promio_Action_Account_Auth extends bF_Mvc_Action_...
Real World examples



Mocking external services


       Consumer                   Connector                  Webservice...
Real World examples



Mocking external services

                                  alternative                    Local
 ...
Real World examples



Clean, readable code
<?php

class Promio_Action_News_Delete extends bF_Mvc_Action_AAction {
     /*...
Real World examples



No framework dependency
<?php

class MySampleService implements IMySampleService {
  /**
   * @var ...
Pros & Cons



Benefits

 Good for agile development
        
          Reducing amount of code
        
          Help...
Pros & Cons



Cons

 To many different implementations, no standard today!
       
         Bucket, Crafty, FLOW3, Imin...
http://joind.in/1553
Upcoming SlideShare
Loading in...5
×

Real world dependency injection - DPC10

4,188

Published on

Published in: Business, Technology
2 Comments
6 Likes
Statistics
Notes
No Downloads
Views
Total Views
4,188
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
68
Comments
2
Likes
6
Embeds 0
No embeds

No notes for slide

Real world dependency injection - DPC10

  1. 1. Real World Dependency Injection Stephan Hochdörfer, bitExpert AG "Dependency Injection is a key element of agile architecture" Ward Cunningham
  2. 2. Agenda 1. About me 2. What is Dependency Injection? 3. Real World examples 4. Pros & Cons 5. Questions
  3. 3. About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP since 1999  S.Hochdoerfer@bitExpert.de  @shochdoerfer
  4. 4. What is Dependency Injection? What is Dependency?
  5. 5. What is Dependency Injection? What is Dependency?  Application Layer  Data Access layer  Business logic layer  ...  External components  Framework classes  Webservices  ...
  6. 6. What is Dependency Injection? Why are Dependencies bad?
  7. 7. What is Dependency Injection? Why are Dependencies bad? „new“ is evil!
  8. 8. What is Dependency Injection? Why are Dependencies bad?  Tightly coupled code  Hard to re-use components  Hard to test components, no isolation
  9. 9. What is Dependency Injection? Simple Dependency Main Required class class
  10. 10. What is Dependency Injection? Complex Dependency Required class Main Required class class Required class
  11. 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. 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. 13. What is Dependency Injection? Very complex Dependency Database Required class External Main Required resource class class Required class Required Required Webservice class class
  14. 14. What is Dependency Injection? How to Manage Dependencies?
  15. 15. What is Dependency Injection? How to Manage Dependencies? You don`t need to. The Framework does it all!
  16. 16. What is Dependency Injection? How to Manage Dependencies? Simple Container vs. Full stacked Framework
  17. 17. What is Dependency Injection? What is Dependency Injection?
  18. 18. What is Dependency Injection? What is Dependency Injection? Consumer
  19. 19. What is Dependency Injection? What is Dependency Injection? Consumer Dependencies
  20. 20. What is Dependency Injection? What is Dependency Injection? Consumer Dependencies Container
  21. 21. What is Dependency Injection? What is Dependency Injection? Consumer Dependencies Container
  22. 22. What is Dependency Injection? Types of Dependency Injection Type 1: Constructor injection <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $oSampleDao; public function __construct(ISampleDao $poSampleDao) { $this->oSampleDao = $poSamleDao; } } ?>
  23. 23. 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; } } ?>
  24. 24. What is Dependency Injection? Types of Dependency Injection Type 3: Interface injection <?php class MySampleService implements IMySampleService, IApplicationContextAware { /** * @var IApplicationContext */ private $oCtx; public function setApplicationContext(IApplicationContext $poCtx) { $this->oCtx = $poCtx; } } ?>
  25. 25. 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; } } ?>
  26. 26. What is Dependency Injection? Configuration Types Type 1: Annotations <?php class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $oSampleDao; /** * @Inject * @Named('TheSampleDao') */ public function __construct(ISampleDao $poSampleDao) { $this->oSampleDao = $poSamleDao; } } ?>
  27. 27. What is Dependency Injection? Configuration Types Type 2: External configuration via XML <?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>
  28. 28. What is Dependency Injection? Configuration Types Type 2: External configuration via YAML services: SampleDao: class: SampleDao arguments: ['app_sample', 'iSampleId', 'BoSample'] SampleService: class: SampleService arguments: [@SampleDao]
  29. 29. 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"); } ?>
  30. 30. What is Dependency Injection? 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
  31. 31. Real world examples "High-level modules should not depend on low-level modules. Both should depend on abstractions." Robert C. Martin
  32. 32. 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); } } ?>
  33. 33. 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>
  34. 34. Real World examples One class, multiple configurations II <?php class Promio_Action_Account_Auth extends bF_Mvc_Action_AFormAction { private $oAccountManager; private $iType; public function __construct(Promio_Service_IAccountManager $poAccountManager) { $this->oAccountManager = $poAccountManager; } public function setType($piType) { $this->iType = (int) $piType; } protected function processFormSubmission(bF_Bo_ABo $poBOFormBackingObject) { $oMaV = new bF_Mvc_ModelAndView($this->getSuccessView(), true); try { $poBOFormBackingObject->setType($this->iType); $this->oAccountManager->save($poBOFormBackingObject); } catch(bF_Service_ServiceException $oException) { $oMaV = new bF_Mvc_ModelAndView($this->getFailureView(), true); } return $oMaV; } } ?>
  35. 35. 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>
  36. 36. 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>
  37. 37. 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; } } ?>
  38. 38. 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) { } } } ?>
  39. 39. 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  Separation of glue code from business logic  Clean view on the code  Separate configuration from code  Getting rid of boilerpate configuration code
  40. 40. 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
  41. 41. http://joind.in/1553
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×