Dependency injection explained (Zbigniew Lukasiak)


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
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:
  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
  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.