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)

364

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
364
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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: http://www.flickr.com/photos/fotoosvanrobin/
  • 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 http://fabien.potencier.org/article/16/symfony-service-container-the-need-for-speed
  • 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. 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/

×