Dependency injection explained (Zbigniew Lukasiak)

430 views
409 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
430
On SlideShare
0
From Embeds
0
Number of Embeds
100
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dependency injection explained (Zbigniew Lukasiak)

  1. 1. Dependency Injection Explained Zbigniew Łukasiak
  2. 2. Why DI is such an elusive idea?The name.The original article by Martin Fowler.Sometimes people say it is all commonsense.Conflating DI with tools that support it.Thinking that it is just a Java concept.
  3. 3. What is Dependency Injection?
  4. 4. Intuition I – removing distractionsHaving all dependencies created before the workstarts is removing distractions.It is like having all the tools assembled and ready.
  5. 5. Photo: http://www.flickr.com/photos/fotoosvanrobin/
  6. 6. Intuition II - ConfigsMagic constants into configs.
  7. 7. ConfigsBut what if you needed { RaiseError => 1 } ?Connection creation into config!Object Oriented Config.Why we do that? The constants change more frequently. We want non-coders to be able to change them.
  8. 8. Intuition III - DecouplingThe code creating objects is coupled to their exact implementation.If we move this creation code out from our business logic – we make it more general.And easier to test.
  9. 9. DecouplingThis lowers rank of coupling from control coupling to data-structured couplingThis code was copied from a library we used – we needed a more restrictive UserAgent and we had to override the whole fetch subroutine instead of changing only one fieldYou can also say that LWP::UserAgent was a magic constant there
  10. 10. DecouplingA workaround – or simplified DI
  11. 11. Intuition IV - Avoiding GlobalsThis is also decoupling.Everything the object needs in its attributes.And singletons are also globals!
  12. 12. DefinitionDependency Injection is the practice ofpassing all object dependencies to its constructor.
  13. 13. ConsequencesNo globals.All object creation is separated from business logic.This separated code walks the dependency tree and recursively creates the nodes – it wires up the application.
  14. 14. Example from http://fabien.potencier.org/article/16/symfony-service-container-the-need-for-speed
  15. 15. Dependency Injection ContainerThe object creation code is very hard to debug and test, ergo it should be as simple and minimal as possible.In many cases it can be automated – this is what DI libraries can do.How many containers in your program? You choose. Or the library. But you need at least one per scope.
  16. 16. What is scope?You cannot create an object before you have all information needed.For example in a web app you have two main scopes: the application scope and request scope. The objects in request scope cannot be created before the user sends a request to your service.
  17. 17. Example
  18. 18. If you use the connection somewhere dontretrieve it from some global object – pass it there explicitly.
  19. 19. Testability
  20. 20. Can you do DI without DI container library?
  21. 21. Law of Demeter
  22. 22. Questions?
  23. 23. Opera Software is hiring!
  24. 24. http://misko.hevery.com/http://misko.hevery.com/code-reviewers-guide/http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/http://blacksheep.parry.org/archives/diy-di/print/

×