Your SlideShare is downloading. ×
0
Dependency InjectionWas, wie, warum?Stephan Hochdörfer, bitExpert AG
Dependency Injection – Was, wie warum?Über mich Stephan Hochdörfer Head of IT der bitExpert AG, Mannheim PHP`ler seit 1...
Dependency Injection – Was, wie warum?DI ist nicht alles. Es geht um mehr.
Dependency Injection – Was, wie warum?Separation of Concerns
Design by ContractDependency Injection – Was, wie warum?
Dependency Injection – Was, wie warum?Dependency Injection
Dependency Injection – Was, wie warum?Dependency Injection"[...] zur Laufzeit die Abhängigkeiteneines Objekts diesem von e...
Dependency Injection – Was, wie warum?Was sind Dependencies?
Dependency Injection – Was, wie warum?Sind Dependencies schlecht?
Dependency Injection – Was, wie warum?Dependencies: Starke Kopplung
Keine Wiederverwendung!Dependency Injection – Was, wie warum?
Keine Isolation, nicht testbar!Dependency Injection – Was, wie warum?
Dependency Injection – Was, wie warum?Dependency Wahnsinn!
Was ist Dependency Injection?Dependency Injection – Was, wie warum?
Was ist Dependency Injection?Dependency Injection – Was, wie warum?new DeletePage(new PageManager());
Was ist Dependency Injection?ConsumerDependency Injection – Was, wie warum?
Was ist Dependency Injection?Consumer DependenciesDependency Injection – Was, wie warum?
Was ist Dependency Injection?Consumer Dependencies ContainerDependency Injection – Was, wie warum?
Was ist Dependency Injection?Consumer Dependencies ContainerDependency Injection – Was, wie warum?
sDependency Injection – Was, wie warum?„new“ is evil!
<?phpclass DeletePage extends Mvc_Action_AAction {private $pageManager;public function __construct() {$this->pageManager =...
Dependency Injection – Was, wie warum?„new“ is evil!<?phpclass DeletePage extends Mvc_Action_AAction {private $pageManager...
"High-level modules should notdepend on low-level modules.Both should depend on abstractions."Robert C. MartinDependency I...
Dependency Injection – Was, wie warum?„new“ is evil!<?phpclass DeletePage extends Mvc_Action_AAction {private $pageManager...
Wie verwaltet man Dependencies?Dependency Injection – Was, wie warum?
Wie verwaltet man Dependencies?Simple Container vs. Full stackedDI FrameworkDependency Injection – Was, wie warum?
Der Container nimmt uns die Arbeit ab!Dependency Injection – Was, wie warum?
Wie Dependencies injizieren?Dependency Injection – Was, wie warum?
Constructor Injection<?phpclass MySampleService implements IMySampleService {/*** @var ISampleDao*/private $sampleDao;publ...
Setter Injection<?phpclass MySampleService implements IMySampleService {/*** @var ISampleDao*/private $sampleDao;public fu...
Interface Injection<?phpinterface IApplicationContextAware {public function setCtx(IApplicationContext $ctx);}Dependency I...
Interface Injection<?phpclass MySampleService implements IMySampleService,IApplicationContextAware {/*** @var IApplication...
Property InjectionDependency Injection – Was, wie warum?
Property InjectionDependency Injection – Was, wie warum?"NEIN NEIN NEIN!"David Zülke
Wie funktioniert das „Wiring“?Dependency Injection – Was, wie warum?
Dependency Injection – Was, wie warum?Annotations<?phpclass MySampleService implements IMySampleService {private $sampleDa...
Dependency Injection – Was, wie warum?Annotations<?phpclass MySampleService implements IMySampleService {private $sampleDa...
Externe Konfiguration - XML<?xml version="1.0" encoding="UTF-8" ?><beans><bean id="SampleDao" class="SampleDao"><construct...
Externe Konfiguration - YAMLservices:SampleDao:class: SampleDaoarguments: [app_sample, iSampleId, BoSample]SampleService:c...
Externe Konfiguration - PHP<?phpclass BeanCache extends Beanfactory_Container_PHP {protected function createSampleDao() {$...
Interne vs. Externe KonfigurationDependency Injection – Was, wie warum?
Interne vs. Externe KonfigurationDependency Injection – Was, wie warum?Klassenkonfigurationvs.Instanzkonfiguration
Dependency Injection – Was, wie warum?Was bringt mir das? Beispiel bitte.
Unittesting einfach gemachtDependency Injection – Was, wie warum?
Unittesting einfach gemacht<?phprequire_once PHPUnit/Framework.php;class ServiceTest extends PHPUnit_Framework_TestCase {p...
Eine Klasse, mehrfache AusprägungDependency Injection – Was, wie warum?
Eine Klasse, mehrfache AusprägungPage ExporterPage ExporterReleased / PublishedPagesReleased / PublishedPagesWorkingcopyPa...
Eine Klasse, mehrfache Ausprägung<?phpabstract class PageExporter {protected function setPageDao(IPageDao $pageDao) {$this...
Eine Klasse, mehrfache Ausprägung<?phpabstract class PageExporter {protected function setPageDao(IPageDao $pageDao) {$this...
Eine Klasse, mehrfache Ausprägung<?phpclass PublishedPageExporter extends PageExporter {public function __construct() {$th...
"Only deleted code is good code!"Oliver GierkeEine Klasse, mehrfache AusprägungDependency Injection – Was, wie warum?
Eine Klasse, mehrfache Ausprägung<?phpclass PageExporter {public function __construct(IPageDao $pageDao) {$this->pageDao =...
Eine Klasse, mehrfache Ausprägung<?xml version="1.0" encoding="UTF-8" ?><beans><bean id="ExportLive" class="PageExporter">...
Eine Klasse, mehrfache Ausprägung<?php// create ApplicationContext instance$ctx = new ApplicationContext();// retrieve liv...
Externe Services mockenDependency Injection – Was, wie warum?
Externe Services mockenBookingmanagerBookingmanager WS-KonnektorWS-Konnektor WebserviceWebserviceDependency Injection – Wa...
Externe Services mockenBookingmanagerBookingmanager WS-KonnektorWS-Konnektor WebserviceWebserviceDependency Injection – Wa...
Externe Services mockenBookingmanagerBookingmanager FS-KonnektorFS-Konnektor FilesystemFilesystemDependency Injection – Wa...
Externe Services mockenBookingmanagerBookingmanager FS-KonnektorFS-Konnektor FilesystemFilesystemerfüllt denVertrag!Depend...
Sauberer, lesbarer CodeDependency Injection – Was, wie warum?
Sauberer, lesbarer Code<?phpclass DeletePage extends Mvc_Action_AAction {private $pageManager;public function __construct(...
Keine Framework AbhängigkeitDependency Injection – Was, wie warum?
Keine Framework Abhängigkeit<?phpclass MySampleService implements IMySampleService {private $sampleDao;public function __c...
Dependency Injection – Was, wie warum?Wie siehst das nun in der Praxis aus?
Dependency Injection – Was, wie warum?Pimple
Pimple – Erste SchritteDependency Injection – Was, wie warum?<?phpclass TalkService {public function __construct() {}publi...
Pimple – Erste SchritteDependency Injection – Was, wie warum?<?phprequire_once /path/to/Pimple.php;require_once /path/to/T...
Pimple – Constructor InjectionDependency Injection – Was, wie warum?<?phpinterface GenericRepository {public function read...
Pimple – Constructor InjectionDependency Injection – Was, wie warum?<?phprequire_once /path/to/Pimple.php;require_once /pa...
Pimple – Setter InjectionDependency Injection – Was, wie warum?<?phpclass Logger {public function doLog($logMsg) {}}class ...
Pimple – Setter InjectionDependency Injection – Was, wie warum?<?phprequire_once /path/to/Pimple.php;require_once /path/to...
Dependency Injection – Was, wie warum?
ZendDi – Erste SchritteDependency Injection – Was, wie warum?<?phpnamespace Acme;class TalkService {public function __cons...
ZendDi – Erste SchritteDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalkService...
ZendDi – Constructor InjectionDependency Injection – Was, wie warum?<?phpnamespace Acme;interface GenericRepository {publi...
ZendDi – Constructor InjectionDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalk...
ZendDi – Setter InjectionDependency Injection – Was, wie warum?<?phpnamespace Acme;class Logger {public function doLog($lo...
ZendDi – Setter InjectionDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$di->configure(new ZendDiConfigur...
ZendDi – Interface InjectionDependency Injection – Was, wie warum?<?phpnamespace Acme;class Logger {public function doLog(...
ZendDi – Interface InjectionDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalkSe...
ZendDi – GrundsätzlichesDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalkServic...
ZendDi – Builder DefinitionDependency Injection – Was, wie warum?<?php// describe dependency$dep = new ZendDiDefinitionBui...
ZendDi – Builder DefinitionDependency Injection – Was, wie warum?<?php// add to Di$defList = new ZendDiDefinitionList($bui...
Dependency Injection – Was, wie warum?
Symfony2Dependency Injection – Was, wie warum?<?phpnamespace AcmeTalkBundleController;use SymfonyBundleFrameworkBundleCont...
Symfony2 – KonfigurationsdateiDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony...
Symfony2 – Constructor InjectionDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfo...
Symfony2 – Setter InjectionDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony.co...
Symfony2 – Setter Injection (optional)Dependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http:/...
Symfony2 – Property InjectionDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony....
Dependency Injection – Was, wie warum?
Flow3 – Constructor InjectionDependency Injection – Was, wie warum?<?phpnamespace AcmeDemoController;use TYPO3FLOW3Annotat...
Flow3 – Setter Injection (manuell)Dependency Injection – Was, wie warum?<?phpnamespace AcmeDemoController;use TYPO3FLOW3An...
Flow3 – Setter Injection (manuell)Dependency Injection – Was, wie warum?File Objects.yaml in Packages/Application/Acme.Dem...
Flow3 – Setter Injection (automatisch)Dependency Injection – Was, wie warum?<?phpnamespace AcmeDemoController;use TYPO3FLO...
Dependency Injection – Was, wie warum?Fokus aufs Wesentliche. Keine Ablenkung.
Dependency Injection – Was, wie warum?Wiederverwendung steigern.
Dependency Injection – Was, wie warum?Hilft den Code besser zu verstehen.
Dependency Injection – Was, wie warum?Wieder Spaß bei der Arbeit ;)
Dependency Injection – Was, wie warum?Kein Standard. Kein Tooling.
Dependency Injection – Was, wie warum?Es braucht Zeit um DI zu verstehen.
Dependency Injection – Was, wie warum?Konfiguration vs. Laufzeit
Vielen Dank!
Upcoming SlideShare
Loading in...5
×

Dependency Injection in PHP - dwx13

1,138

Published on

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,138
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
11
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Dependency Injection in PHP - dwx13"

  1. 1. Dependency InjectionWas, wie, warum?Stephan Hochdörfer, bitExpert AG
  2. 2. Dependency Injection – Was, wie warum?Über mich Stephan Hochdörfer Head of IT der bitExpert AG, Mannheim PHP`ler seit 1999 S.Hochdoerfer@bitExpert.de @shochdoerfer
  3. 3. Dependency Injection – Was, wie warum?DI ist nicht alles. Es geht um mehr.
  4. 4. Dependency Injection – Was, wie warum?Separation of Concerns
  5. 5. Design by ContractDependency Injection – Was, wie warum?
  6. 6. Dependency Injection – Was, wie warum?Dependency Injection
  7. 7. Dependency Injection – Was, wie warum?Dependency Injection"[...] zur Laufzeit die Abhängigkeiteneines Objekts diesem von einemanderen Objekt als Referenzen zurVerfügung gestellt werden."(Wikipedia)
  8. 8. Dependency Injection – Was, wie warum?Was sind Dependencies?
  9. 9. Dependency Injection – Was, wie warum?Sind Dependencies schlecht?
  10. 10. Dependency Injection – Was, wie warum?Dependencies: Starke Kopplung
  11. 11. Keine Wiederverwendung!Dependency Injection – Was, wie warum?
  12. 12. Keine Isolation, nicht testbar!Dependency Injection – Was, wie warum?
  13. 13. Dependency Injection – Was, wie warum?Dependency Wahnsinn!
  14. 14. Was ist Dependency Injection?Dependency Injection – Was, wie warum?
  15. 15. Was ist Dependency Injection?Dependency Injection – Was, wie warum?new DeletePage(new PageManager());
  16. 16. Was ist Dependency Injection?ConsumerDependency Injection – Was, wie warum?
  17. 17. Was ist Dependency Injection?Consumer DependenciesDependency Injection – Was, wie warum?
  18. 18. Was ist Dependency Injection?Consumer Dependencies ContainerDependency Injection – Was, wie warum?
  19. 19. Was ist Dependency Injection?Consumer Dependencies ContainerDependency Injection – Was, wie warum?
  20. 20. sDependency Injection – Was, wie warum?„new“ is evil!
  21. 21. <?phpclass 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));}}Dependency Injection – Was, wie warum?„new“ is evil!
  22. 22. Dependency Injection – Was, wie warum?„new“ is evil!<?phpclass 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));}}
  23. 23. "High-level modules should notdepend on low-level modules.Both should depend on abstractions."Robert C. MartinDependency Injection – Was, wie warum?
  24. 24. Dependency Injection – Was, wie warum?„new“ is evil!<?phpclass 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));}}
  25. 25. Wie verwaltet man Dependencies?Dependency Injection – Was, wie warum?
  26. 26. Wie verwaltet man Dependencies?Simple Container vs. Full stackedDI FrameworkDependency Injection – Was, wie warum?
  27. 27. Der Container nimmt uns die Arbeit ab!Dependency Injection – Was, wie warum?
  28. 28. Wie Dependencies injizieren?Dependency Injection – Was, wie warum?
  29. 29. Constructor Injection<?phpclass MySampleService implements IMySampleService {/*** @var ISampleDao*/private $sampleDao;public function __construct(ISampleDao $sampleDao) {$this->sampleDao = $sampleDao;}}Dependency Injection – Was, wie warum?
  30. 30. Setter Injection<?phpclass MySampleService implements IMySampleService {/*** @var ISampleDao*/private $sampleDao;public function setSampleDao(ISampleDao $sampleDao){$this->sampleDao = $sampleDao;}}Dependency Injection – Was, wie warum?
  31. 31. Interface Injection<?phpinterface IApplicationContextAware {public function setCtx(IApplicationContext $ctx);}Dependency Injection – Was, wie warum?
  32. 32. Interface Injection<?phpclass MySampleService implements IMySampleService,IApplicationContextAware {/*** @var IApplicationContext*/private $ctx;public function setCtx(IApplicationContext $ctx) {$this->ctx = $ctx;}}Dependency Injection – Was, wie warum?
  33. 33. Property InjectionDependency Injection – Was, wie warum?
  34. 34. Property InjectionDependency Injection – Was, wie warum?"NEIN NEIN NEIN!"David Zülke
  35. 35. Wie funktioniert das „Wiring“?Dependency Injection – Was, wie warum?
  36. 36. Dependency Injection – Was, wie warum?Annotations<?phpclass MySampleService implements IMySampleService {private $sampleDao;/*** @Inject*/public function __construct(ISampleDao $sampleDao){$this->sampleDao = $sampleDao;}}
  37. 37. Dependency Injection – Was, wie warum?Annotations<?phpclass MySampleService implements IMySampleService {private $sampleDao;/*** @Inject* @Named(TheSampleDao)*/public function __construct(ISampleDao $sampleDao){$this->sampleDao = $sampleDao;}}
  38. 38. Externe Konfiguration - 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>Dependency Injection – Was, wie warum?
  39. 39. Externe Konfiguration - YAMLservices:SampleDao:class: SampleDaoarguments: [app_sample, iSampleId, BoSample]SampleService:class: SampleServicearguments: [@SampleDao]Dependency Injection – Was, wie warum?
  40. 40. Externe Konfiguration - PHP<?phpclass 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;}}Dependency Injection – Was, wie warum?
  41. 41. Interne vs. Externe KonfigurationDependency Injection – Was, wie warum?
  42. 42. Interne vs. Externe KonfigurationDependency Injection – Was, wie warum?Klassenkonfigurationvs.Instanzkonfiguration
  43. 43. Dependency Injection – Was, wie warum?Was bringt mir das? Beispiel bitte.
  44. 44. Unittesting einfach gemachtDependency Injection – Was, wie warum?
  45. 45. Unittesting einfach gemacht<?phprequire_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);}}Dependency Injection – Was, wie warum?
  46. 46. Eine Klasse, mehrfache AusprägungDependency Injection – Was, wie warum?
  47. 47. Eine Klasse, mehrfache AusprägungPage ExporterPage ExporterReleased / PublishedPagesReleased / PublishedPagesWorkingcopyPagesWorkingcopyPagesDependency Injection – Was, wie warum?
  48. 48. Eine Klasse, mehrfache Ausprägung<?phpabstract class PageExporter {protected function setPageDao(IPageDao $pageDao) {$this->pageDao = $pageDao;}}Dependency Injection – Was, wie warum?
  49. 49. Eine Klasse, mehrfache Ausprägung<?phpabstract class PageExporter {protected function setPageDao(IPageDao $pageDao) {$this->pageDao = $pageDao;}}Zur Erinnerung:Der Vertrag!Dependency Injection – Was, wie warum?
  50. 50. Eine Klasse, mehrfache Ausprägung<?phpclass PublishedPageExporter extends PageExporter {public function __construct() {$this->setPageDao(new PublishedPageDao());}}class WorkingCopyPageExporter extends PageExporter {public function __construct() {$this->setPageDao(new WorkingCopyPageDao());}}Dependency Injection – Was, wie warum?
  51. 51. "Only deleted code is good code!"Oliver GierkeEine Klasse, mehrfache AusprägungDependency Injection – Was, wie warum?
  52. 52. Eine Klasse, mehrfache Ausprägung<?phpclass PageExporter {public function __construct(IPageDao $pageDao) {$this->pageDao = $pageDao;}}Dependency Injection – Was, wie warum?
  53. 53. Eine Klasse, mehrfache Ausprägung<?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>Dependency Injection – Was, wie warum?
  54. 54. Eine Klasse, mehrfache Ausprägung<?php// create ApplicationContext instance$ctx = new ApplicationContext();// retrieve live exporter$exporter = $ctx->getBean(ExportLive);// retrieve working copy exporter$exporter = $ctx->getBean(ExportWorking);Dependency Injection – Was, wie warum?
  55. 55. Externe Services mockenDependency Injection – Was, wie warum?
  56. 56. Externe Services mockenBookingmanagerBookingmanager WS-KonnektorWS-Konnektor WebserviceWebserviceDependency Injection – Was, wie warum?
  57. 57. Externe Services mockenBookingmanagerBookingmanager WS-KonnektorWS-Konnektor WebserviceWebserviceDependency Injection – Was, wie warum?Zur Erinnerung:Der Vertrag!
  58. 58. Externe Services mockenBookingmanagerBookingmanager FS-KonnektorFS-Konnektor FilesystemFilesystemDependency Injection – Was, wie warum?
  59. 59. Externe Services mockenBookingmanagerBookingmanager FS-KonnektorFS-Konnektor FilesystemFilesystemerfüllt denVertrag!Dependency Injection – Was, wie warum?
  60. 60. Sauberer, lesbarer CodeDependency Injection – Was, wie warum?
  61. 61. Sauberer, lesbarer Code<?phpclass 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());}}Dependency Injection – Was, wie warum?
  62. 62. Keine Framework AbhängigkeitDependency Injection – Was, wie warum?
  63. 63. Keine Framework Abhängigkeit<?phpclass 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) {}}}Dependency Injection – Was, wie warum?
  64. 64. Dependency Injection – Was, wie warum?Wie siehst das nun in der Praxis aus?
  65. 65. Dependency Injection – Was, wie warum?Pimple
  66. 66. Pimple – Erste SchritteDependency Injection – Was, wie warum?<?phpclass TalkService {public function __construct() {}public function getTalks() {}}
  67. 67. Pimple – Erste SchritteDependency Injection – Was, wie warum?<?phprequire_once /path/to/Pimple.php;require_once /path/to/TalkService.php;// create the Container$container = new Pimple();// define talkService object in container$container[talkService] = function ($c) {return new TalkService();};// instantiate talkService from container$talkService = $container[talkService];
  68. 68. Pimple – Constructor InjectionDependency Injection – Was, wie warum?<?phpinterface GenericRepository {public function readTalks();}class TalkRepository implements GenericRepository {public function readTalks() {}}class TalkService {public function __construct(TalkRepository $repo) {}public function getTalks() {}}
  69. 69. Pimple – Constructor InjectionDependency Injection – Was, wie warum?<?phprequire_once /path/to/Pimple.php;require_once /path/to/TalkService.php;// create the Container$container = new Pimple();// define services in container$container[talkRepository] = function ($c) {return new TalkRepository();};$container[talkService] = function ($c) {return new TalkService($c[talkRepository]);};// instantiate talkService from container$talkService = $container[talkService];
  70. 70. Pimple – Setter InjectionDependency Injection – Was, wie warum?<?phpclass Logger {public function doLog($logMsg) {}}class TalkService {public function __construct(TalkRepository $repo) {}public function setLogger(Logger $logger) {}public function getTalks() {}}
  71. 71. Pimple – Setter InjectionDependency Injection – Was, wie warum?<?phprequire_once /path/to/Pimple.php;require_once /path/to/TalkService.php;// create the Container$container = new Pimple();// define services in container$container[logger] = function ($c) {return new Logger();};$container[talkRepository] = function ($c) {return new TalkRepository();};$container[talkService] = function ($c) {$service = new TalkService($c[talkRepository]);$service->setLogger($c[logger]);return $service;};// instantiate talkService from container$talkService = $container[talkService];
  72. 72. Dependency Injection – Was, wie warum?
  73. 73. ZendDi – Erste SchritteDependency Injection – Was, wie warum?<?phpnamespace Acme;class TalkService {public function __construct() {}public function getTalks() {}}
  74. 74. ZendDi – Erste SchritteDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalkService);$service->getTalks();
  75. 75. ZendDi – Constructor InjectionDependency Injection – Was, wie warum?<?phpnamespace Acme;interface GenericRepository {public function readTalks();}class TalkRepository implements GenericRepository {public function readTalks() {}}class TalkService {public function __construct(TalkRepository $repo) {}public function getTalks() {}}
  76. 76. ZendDi – Constructor InjectionDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalkService);$service->getTalks();
  77. 77. ZendDi – Setter InjectionDependency Injection – Was, wie warum?<?phpnamespace Acme;class Logger {public function doLog($logMsg) {}}class TalkService {public function __construct(TalkRepository $repo) {}public function setLogger(Logger $logger) {}public function getTalks() {}}
  78. 78. ZendDi – Setter InjectionDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$di->configure(new ZendDiConfiguration(array(definition => array(class => array(AcmeTalkService => array(setLogger => array(required => true)))))));$service = $di->get(AcmeTalkService);var_dump($service);
  79. 79. ZendDi – Interface InjectionDependency Injection – Was, wie warum?<?phpnamespace Acme;class Logger {public function doLog($logMsg) {}}interface LoggerAware {public function setLogger(Logger $logger);}class TalkService implements LoggerAware {public function __construct(TalkRepository $repo) {}public function setLogger(Logger $logger) {}public function getTalks() {}}
  80. 80. ZendDi – Interface InjectionDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalkService);$service->getTalks();
  81. 81. ZendDi – GrundsätzlichesDependency Injection – Was, wie warum?<?php$di = new ZendDiDi();$service = $di->get(AcmeTalkService);var_dump($service);$service2 = $di->get(AcmeTalkService);var_dump($service2); // same instance as $service$service3 = $di->get(AcmeTalkService,array(repo => new AcmeTalkRepository()));
  82. 82. ZendDi – Builder DefinitionDependency Injection – Was, wie warum?<?php// describe dependency$dep = new ZendDiDefinitionBuilderPhpClass();$dep->setName(AcmeTalkRepository);// describe class$class = new ZendDiDefinitionBuilderPhpClass();$class->setName(AcmeTalkService);// add injection method$im = new ZendDiDefinitionBuilderInjectionMethod();$im->setName(__construct);$im->addParameter(repo, AcmeTalkRepository);$class->addInjectionMethod($im);// configure builder$builder = new ZendDiDefinitionBuilderDefinition();$builder->addClass($dep);$builder->addClass($class);
  83. 83. ZendDi – Builder DefinitionDependency Injection – Was, wie warum?<?php// add to Di$defList = new ZendDiDefinitionList($builder);$di = new ZendDiDi($defList);$service = $di->get(AcmeTalkService);var_dump($service);
  84. 84. Dependency Injection – Was, wie warum?
  85. 85. Symfony2Dependency Injection – Was, wie warum?<?phpnamespace AcmeTalkBundleController;use SymfonyBundleFrameworkBundleControllerController;use SensioBundleFrameworkExtraBundleConfigurationRoute;use SensioBundleFrameworkExtraBundleConfigurationTemplate;class TalkController extends Controller {/*** @Route("/", name="_talk")* @Template()*/public function indexAction() {$service = $this->get(acme.talk.service);return array();}}
  86. 86. Symfony2 – KonfigurationsdateiDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd"></container>File services.xml in src/Acme/DemoBundle/Resources/config
  87. 87. Symfony2 – Constructor InjectionDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd"><services><service id="acme.talk.repo"class="AcmeTalkBundleServiceTalkRepository" /><service id="acme.talk.service"class="AcmeTalkBundleServiceTalkService"><argument type="service" id="acme.talk.repo" /></service></services></container>
  88. 88. Symfony2 – Setter InjectionDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd"><services><service id="acme.talk.logger"class="AcmeTalkBundleServiceLogger" /><service id="acme.talk.repo"class="AcmeTalkBundleServiceTalkRepository" /><service id="acme.talk.service"class="AcmeTalkBundleServiceTalkService"><argument type="service" id="acme.talk.repo" /><call method="setLogger"><argument type="service" id="acme.talk.logger" /></call></service></services></container>
  89. 89. Symfony2 – Setter Injection (optional)Dependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd"><services><service id="acme.talk.logger"class="AcmeTalkBundleServiceLogger" /><service id="acme.talk.repo"class="AcmeTalkBundleServiceTalkRepository" /><service id="acme.talk.service"class="AcmeTalkBundleServiceTalkService"><argument type="service" id="acme.talk.repo" /><call method="setLogger"><argument type="service" id="acme.talk.logger"on-invalid="ignore" /></call></service></services></container>
  90. 90. Symfony2 – Property InjectionDependency Injection – Was, wie warum?<?xml version="1.0" ?><container xmlns="http://symfony.com/schema/dic/services"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://symfony.com/schema/dic/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd"><services><service id="acme.talk.repo"class="AcmeTalkBundleServiceTalkRepository" /><service id="acme.talk.service"class="AcmeTalkBundleServiceTalkService"><property name="talkRepository" type="service"id="acme.talk.repo" /></service></services></container>
  91. 91. Dependency Injection – Was, wie warum?
  92. 92. Flow3 – Constructor InjectionDependency Injection – Was, wie warum?<?phpnamespace AcmeDemoController;use TYPO3FLOW3Annotations as FLOW3;/*** @FLOW3Scope("session")*/class StandardController extends TYPO3FLOW3MVCControllerActionController {/*** @var AcmeDemoServiceTalkServiceInterface*/protected $talkService;public function __construct(AcmeDemoServiceTalkService $talkService) {$this->talkService = $talkService;}public function indexAction() {}}
  93. 93. Flow3 – Setter Injection (manuell)Dependency Injection – Was, wie warum?<?phpnamespace AcmeDemoController;use TYPO3FLOW3Annotations as FLOW3;/*** @FLOW3Scope("session")*/class StandardController extends TYPO3FLOW3MVCControllerActionController {/*** @var AcmeDemoServiceTalkServiceInterface*/protected $talkService;public function setTalkService(AcmeDemoServiceTalkService $talkService) {$this->talkService = $talkService;}public function indexAction() {}}
  94. 94. Flow3 – Setter Injection (manuell)Dependency Injection – Was, wie warum?File Objects.yaml in Packages/Application/Acme.Demo/Configuration# @package AcmeAcmeDemoControllerStandardController:properties:talkService:object: AcmeDemoServiceTalkService
  95. 95. Flow3 – Setter Injection (automatisch)Dependency Injection – Was, wie warum?<?phpnamespace AcmeDemoController;use TYPO3FLOW3Annotations as FLOW3;/*** @FLOW3Scope("session")*/class StandardController extends TYPO3FLOW3MVCControllerActionController {/*** @var AcmeDemoServiceTalkServiceInterface*/protected $talkService;public function injectTalkService(AcmeDemoServiceTalkService $talkService) {$this->talkService = $talkService;}public function indexAction() {}}
  96. 96. Dependency Injection – Was, wie warum?Fokus aufs Wesentliche. Keine Ablenkung.
  97. 97. Dependency Injection – Was, wie warum?Wiederverwendung steigern.
  98. 98. Dependency Injection – Was, wie warum?Hilft den Code besser zu verstehen.
  99. 99. Dependency Injection – Was, wie warum?Wieder Spaß bei der Arbeit ;)
  100. 100. Dependency Injection – Was, wie warum?Kein Standard. Kein Tooling.
  101. 101. Dependency Injection – Was, wie warum?Es braucht Zeit um DI zu verstehen.
  102. 102. Dependency Injection – Was, wie warum?Konfiguration vs. Laufzeit
  103. 103. Vielen Dank!
  1. A particular slide catching your eye?

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

×