Your SlideShare is downloading. ×
Dependency injection explained (Zbigniew Lukasiak)
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Dependency Injection Explained Zbigniew Łukasiak
  • 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. What is Dependency Injection?
  • 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. Photo:
  • 6. Intuition II - ConfigsMagic constants into configs.
  • 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. 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. 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. DecouplingA workaround – or simplified DI
  • 11. Intuition IV - Avoiding GlobalsThis is also decoupling.Everything the object needs in its attributes.And singletons are also globals!
  • 12. DefinitionDependency Injection is the practice ofpassing all object dependencies to its constructor.
  • 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. Example from
  • 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. 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. Example
  • 18. If you use the connection somewhere dontretrieve it from some global object – pass it there explicitly.
  • 19. Testability
  • 20. Can you do DI without DI container library?
  • 21. Law of Demeter
  • 22. Questions?
  • 23. Opera Software is hiring!
  • 24.