Wake up Neo... Dependencies have you

1,047 views

Published on

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

No Downloads
Views
Total views
1,047
On SlideShare
0
From Embeds
0
Number of Embeds
102
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Wake up Neo... Dependencies have you

  1. 1. Ivan Mosiev — software architect, AltexSoft i.k.mosev@gmail.com http://verber.kh.ua @polny_otec
  2. 2. Wake up, Neo... Dependencies have you
  3. 3. Ideal world
  4. 4. Ideal Unit
  5. 5. Welcome to the real world
  6. 6. Real Unit Dependencies
  7. 7. Imagine, there is no dependencies
  8. 8. Imaginary Unit Indirect Input Indirect Output
  9. 9. Indirect Input $internal = $Dependency->something; function dependency() { return $something; } function dependency() { throw new Exception_WTF; } function dependency(&$by_ref) { $by_ref++; }
  10. 10. Indirect Output $Dep->something = $internal; $Dep->doSomething($internal);
  11. 11. Stub
  12. 12. Dummy
  13. 13. Fake
  14. 14. Spy
  15. 15. Mock
  16. 16. function runMatrix() { $agents = array(); while (TRUE) { $agents[] = new AgentSmith(); } } This is my world, my world!
  17. 17. Dependency Injection
  18. 18. Refactoring: step 0 function doSomething() { $auth = new Service(); … return $profit; }
  19. 19. Refactoring: step 1 private function getService() { return new Service(); } function doSomething() { $service = $this->getService(); … return $profit; }
  20. 20. Refactoring: step 2 private $_service; private function getService() { If (!$this->_service) $this->_service = new Service(); return $this->_service; } function setService(IService $service) { $this->_service = $service; }
  21. 21. do { $the->same(); } while ($deps_count > 0);
  22. 22. function getService() { if (!$this->_service) $this->_service = return Service(); return $this->_service; }
  23. 23. Inversion of Control IoC Container store information about dependencies Dependencies instantiated by Container recursively Dependencies injected into Object by Service Container
  24. 24. Benefits? Mock and test everything Loose coupling Components reuse
  25. 25. Now you know kung fu

×