Real WorldDependency InjectionStephan Hochdörfer, bitExpert AG
Real World Dependency Injection About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying ...
Real World Dependency Injection What are Dependencies?
Real World Dependency Injection What are Dependencies?                          Application                  Framework    ...
Real World Dependency Injection What are Dependencies?                           Controller  PHP extensions         Servic...
Real World Dependency Injection Are Dependencies bad?
Real World Dependency Injection Are Dependencies bad?           Dependencies are not bad!               They are useful!
Real World Dependency Injection Are Dependencies bad?      Hard-coded dependencies are bad!
Real World Dependency Injection Tightly coupled code
Real World Dependency Injection No reuse of components
Real World Dependency Injection No isolation, not testable!
Real World Dependency Injection Development gets overcomplicated!
Real World Dependency Injection                              s „new“ is evil!
Real World Dependency Injection „new“ is evil! <?php class DeletePage extends Mvc_Action_AAction {    private $pageManager...
Real World Dependency Injection „new“ is evil! <?php class DeletePage extends Mvc_Action_AAction {    private $pageManager...
Real World Dependency Injection        "High-level modules should not         depend on low-level modules.            Both...
Real World Dependency Injection Interfaces act as contracts
Real World Dependency Injection „new“ is evil! <?php class DeletePage extends Mvc_Action_AAction {    private $pageManager...
Real World Dependency Injection How to Manage Dependencies?
Real World Dependency Injection Manually resolve dependencies?
Real World Dependency Injection Automatic wiring required!     Simple Container       vs.    Full stacked                 ...
Real World Dependency Injection What is Dependency Injection?
Real World Dependency Injection What is Dependency Injection?    Consumer
Real World Dependency Injection What is Dependency Injection?    Consumer            Dependencies
Real World Dependency Injection What is Dependency Injection?    Consumer            Dependencies   Container
Real World Dependency Injection What is Dependency Injection?    Consumer            Dependencies   Container
Real World Dependency Injection How to inject a Dependency?
Real World Dependency Injection Constructor Injection <?php class MySampleService implements IMySampleService {   /**    *...
Real World Dependency Injection Setter injection <?php class MySampleService implements IMySampleService {   /**    * @var...
Real World Dependency Injection Interface injection <?php interface IApplicationContextAware {   public function setCtx(IA...
Real World Dependency Injection Interface injection <?php class MySampleService implements IMySampleService, IApplicationC...
Real World Dependency Injection How to wire it all up?
Real World Dependency Injection Annotation based wiring <?php class MySampleService implements IMySampleService {   privat...
Real World Dependency Injection Annotation based wiring <?php class MySampleService implements IMySampleService {   privat...
Real World Dependency Injection External configuration - XML <?xml version="1.0" encoding="UTF-8" ?> <beans>    <bean id="...
Real World Dependency Injection External configuration - YAML services:   SampleDao:     class: SampleDao     arguments: [...
Real World Dependency Injection External configuration - PHP <?php class BeanCache extends Beanfactory_Container_PHP {    ...
Real World Dependency Injection Ready to unlock the door?
Real World Dependency Injection Unittesting made easy
Real World Dependency Injection Unittesting made easy <?php require_once PHPUnit/Framework.php; class ServiceTest extends ...
Real World Dependency Injection One class, multiple configurations
Real World Dependency Injection One class, multiple configurations                              Page Exporter             ...
Real World Dependency Injection One class, multiple configurations <?php abstract class PageExporter {   protected functio...
Real World Dependency Injection One class, multiple configurations <?php abstract class PageExporter {   protected functio...
Real World Dependency Injection One class, multiple configurations <?php class PublishedPageExporter extends PageExporter ...
Real World Dependency Injection One class, multiple configurations      "Only deleted code is good code!"                 ...
Real World Dependency Injection One class, multiple configurations <?php class PageExporter {   public function __construc...
Real World Dependency Injection One class, multiple configurations <?xml version="1.0" encoding="UTF-8" ?> <beans>    <bea...
Real World Dependency Injection One class, multiple configurations <?php // create ApplicationContext instance $ctx = new ...
Real World Dependency Injection One class, multiple configurations II
Real World Dependency Injection One class, multiple configurations II        http://editor.loc/page/[id]/headline/        ...
Real World Dependency Injection One class, multiple configurations II <?php class EditPart extends Mvc_Action_AFormAction ...
Real World Dependency Injection One class, multiple configurations II <?xml version="1.0" encoding="UTF-8" ?> <beans>    <...
Real World Dependency Injection Mocking external service access
Real World Dependency Injection Mocking external service access                               WS-                         ...
Real World Dependency Injection Mocking external service access                                   WS-                     ...
Real World Dependency Injection Mocking external service access                                FS-                        ...
Real World Dependency Injection Mocking external service access                                         FS-               ...
Real World Dependency Injection Clean, readable code
Real World Dependency Injection Clean, readable code <?php class DeletePage extends Mvc_Action_AAction {    private $pageM...
Real World Dependency Injection No framework dependency
Real World Dependency Injection No framework dependency <?php class MySampleService implements IMySampleService {   privat...
Real World Dependency Injection Benefits  Loose coupling, reuse of components!
Real World Dependency Injection Benefits       Can reduce the amount of code!
Real World Dependency Injection Benefits               Helps developers to               understand the code!
Real World Dependency Injection Cons – No JSR330 for PHP  Bucket, Crafty, FLOW3, Imind_Context,      PicoContainer, Pimple...
Real World Dependency Injection Cons – Developers need mindshift            Configuration ↔ Runtime
http://joind.in/3002
Image Creditshttp://www.sxc.hu/photo/1028452
Upcoming SlideShare
Loading in...5
×

Real World Dependency Injection - phpday

5,715

Published on

Published in: Business, Technology
1 Comment
7 Likes
Statistics
Notes
No Downloads
Views
Total Views
5,715
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
57
Comments
1
Likes
7
Embeds 0
No embeds

No notes for slide

Real World Dependency Injection - phpday

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

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

×