Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Real World Dependency Injection - PFCongres 2010

2,016 views

Published on

Published in: Technology
  • Be the first to comment

Real World Dependency Injection - PFCongres 2010

  1. 1. Real World Dependency Injection Stephan Hochdörfer, bitExpert AG "Dependency Injection is a key element of agile architecture" Ward Cunningham
  2. 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. 3. Agenda 1. What is Dependency Injection? 2. Real World examples 3. Pros & Cons 4. Questions
  4. 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. 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. 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. 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. 8. What is Dependency Injection? Simple Dependency Main Required class class
  9. 9. What is Dependency Injection? Complex Dependency Required class Main Required class class Required class
  10. 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. 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? How to Manage Dependencies?
  14. 14. What is Dependency Injection? How to Manage Dependencies? You don`t need to. The Framework does it all!
  15. 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. 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. 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. 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. 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. 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. 21. Agenda 1. What is Dependency Injection? 2. Real World examples 3. Pros & Cons 4. Questions
  22. 22. Real world examples "High-level modules should not depend on low-level modules. Both should depend on abstractions." Robert C. Martin
  23. 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. 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. 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. 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. 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. 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. 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. 30. Agenda 1. What is Dependency Injection? 2. Real World Examples 3. Pros & Cons 4. Questions
  31. 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. 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. 33. Agenda 1. What is Dependency Injection? 2. Pros & Cons 3. Real World examples 4. Questions

×