Adding Dependency Injection to Legacy Applications

PHP Developer at Hypersites LLC
Oct. 19, 2011
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
Adding Dependency Injection to Legacy Applications
1 of 77

More Related Content

What's hot

PHPUnit elevato alla Symfony2PHPUnit elevato alla Symfony2
PHPUnit elevato alla Symfony2eugenio pombi
Symfony2, creare bundle e valore per il clienteSymfony2, creare bundle e valore per il cliente
Symfony2, creare bundle e valore per il clienteLeonardo Proietti
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Leonardo Proietti
Min-Maxing Software CostsMin-Maxing Software Costs
Min-Maxing Software CostsKonstantin Kudryashov
Crafting beautiful softwareCrafting beautiful software
Crafting beautiful softwareJorn Oomen
The IoC HydraThe IoC Hydra
The IoC HydraKacper Gunia

Similar to Adding Dependency Injection to Legacy Applications

PHPUnit でよりよくテストを書くためにPHPUnit でよりよくテストを書くために
PHPUnit でよりよくテストを書くためにYuya Takeyama
Things I Believe Now That I'm OldThings I Believe Now That I'm Old
Things I Believe Now That I'm OldRoss Tuck
Unittests für DummiesUnittests für Dummies
Unittests für DummiesLars Jankowfsky
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
Code moi une RH! (PHP tour 2017)Code moi une RH! (PHP tour 2017)
Code moi une RH! (PHP tour 2017)Arnaud Langlade
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith

Recently uploaded

Getting your enterprise ready for Microsoft 365 CopilotGetting your enterprise ready for Microsoft 365 Copilot
Getting your enterprise ready for Microsoft 365 CopilotVignesh Ganesan I Microsoft MVP
Unleashing Innovation: IoT Project with MicroPythonUnleashing Innovation: IoT Project with MicroPython
Unleashing Innovation: IoT Project with MicroPythonVubon Roy
EuroBSDCon 2023 - (auto)Installing BSD Systems - Cases using pfSense, TrueNAS...EuroBSDCon 2023 - (auto)Installing BSD Systems - Cases using pfSense, TrueNAS...
EuroBSDCon 2023 - (auto)Installing BSD Systems - Cases using pfSense, TrueNAS...Vinícius Zavam
LLaMA 2.pptxLLaMA 2.pptx
LLaMA 2.pptxRkRahul16
Understanding Wireguard, TLS and Workload IdentityUnderstanding Wireguard, TLS and Workload Identity
Understanding Wireguard, TLS and Workload IdentityChristian Posta
Machine learning with quantum computersMachine learning with quantum computers
Machine learning with quantum computersSpeck&Tech

Adding Dependency Injection to Legacy Applications

Editor's Notes

  1. http://www.flickr.com/photos/jon_bradley/3360454148/sizes/o/in/photostream/http://www.flickr.com/photos/walker-chris-steph-jordan/2983082765/sizes/o/in/photostream/
  2. http://www.flickr.com/photos/santomarco/5427382270/sizes/l/in/photostream/
  3. The Hollywood principle Try to make your code ignorant of it’s context
  4. It’s a way of adding abstraction.By abstraction, we mean to abstract from view or hide.
  5. danielspils
  6. tnarik's - flickr
  7. Have you ever felt like a crash test dummyAdd automated testing
  8. Strategy PatternExplain picPic – Flickr - dominicbartolini
  9. Add pointers to dependencies
  10. Welding customHitch interfaceRope a lot of workWelder – Flickr - F0t0Synthcogdogblog – tow hitch - flickr
  11. DI should not be your goal, especially when it comes to adding DI to you’re legacy appsMaintainable codeQuality codeFlexible codeReusable codeNVJ – Flickr
  12. Bang for the Buck!Not everything needs to have DI or fit the goal you are going afterIf you’re goal is testability and you are using ZF1 for example, not every plugin or helper you create maybe worth the time it would take to create unit tests for.If you are adding DI to a code base where not everyone is on-board with the concept. This will help you fall foul of the perception you are wasting time.Being a purest in legacy code may take you to an early grave.http://misternewuzer.deviantart.com/art/Pop-Bang-Warpath-119523671
  13. What I hope to give to you are some new tools to add to your tool boxWhiteforgeflickr
  14. The Hollywood principle in practiceYou’re code should not know what it’s doing in the grand schema of things It should not be aware of it’s contexthttp://www.flickr.com/photos/duke_raoul/2262882022/sizes/l/in/photostream/
  15. Be careful, opinions vary on this topicBike shed question,It doesn’t matter when you are adding DI to legacy, because the answer is, what ever works for you right now.http://www.flickr.com/photos/sue_langford/4525633192/sizes/l/in/photostream/
  16. A helper pattern that works well with constructor injection, when you expect people to extend the classhttp://www.flickr.com/photos/22212359@N06/2307645669/sizes/l/in/photostream/
  17. http://www.flickr.com/photos/mag3737/377132773/sizes/o/in/photostream/
  18. This can get ugly with large dependency listsThat’s also a code smell that your object is trying to do too much
  19. You can combine this with constructor injection also
  20. showbiz kids - flickr
  21. Mixed string with instance
  22. If you so an string check you could combine this is injection of an object instance
  23. Hollywood principle againIf you use a class name where polymorphism can’t be used then you are calling out to the global namespace.Polymorphism is the ability to switch or replace an object of one type with an object of another typehttp://www.flickr.com/photos/demonbaby/3788532381/sizes/o/in/photostream/
  24. Many types and names (factory method, builder, containers)Dirty work happens hereNeed to inject the factory into the objects that needs to create objectsRamson – Flickr
  25. This is a little tedious for simple things
  26. CDEGlobal – Flickr
  27. Need to be careful once you start to combine with other techniques
  28. Need some class name conventionCreate the real one to overrideDon’t have to use eval
  29. So I can inject an instanceI can inject a factoryBut when and how do I do thatThis is the real hard workMichael D. Dunn – flickr
  30. Just a sophisticated factorySo we will just be building upon what we have already cover for factoriesPinkpurse - Flickr
  31. DavidMenting – Flickr
  32. Could be PHP, XML, ini, etc
  33. DAN_DAN2 – Flickr
  34. Remember this would be inside of a factoryDoesn't scale great, but not awful
  35. If it walks like a duck and it talks like a duck, then it’s a duck!http://www.flickr.com/photos/shinyhappyworld/5475939747/sizes/l/in/photostream/
  36. Code from ZF1Could go further and check type hits and param counts.Could become a mess if it’s used a lot. In PHP you will need to do some kind of introspection You can cache the result of the introspection based on class nameIn reality this is the most powerful model for decupling, but with great power come great opportunity to kill yourself
  37. Try to wire the objects up with out a central configuration and without a hand coded factory for every non-trivial object
  38. Code from a DI project I created
  39. Well sudoZalgon – Flickr
  40. Will also have to check inheritance tree for things like class level annotations
  41. Used with auto wiring
  42. If you process the annotations by creating a config object about the class, you can then cache that config object.
  43. It not always possible or advisable to be 100% DI, especially but not right away.
  44. Gives you global access and a shared instanceWill spread global access all over your codeBob.Fornal– Flickr
  45. Hard to test without adding test hooks to reset, which is evil.
  46. You can put anything you need in the registryAnyone can put or change the data in the registry
  47. A little better than just a singleton. Something still has to fill it with class instances first. Could use anonymous function.
  48. ilovememphis – Flickr
  49. Like a fixed registry with builder code baked in.Still has an issue with testing, but solutions are less evilIf you’ve ever worked with ZF1 boot-strapper and resource plugins this is much like thatIt also is a model of making the service locator pluggable
  50. cogdogblog – Flickr
  51. Refactoring'sKaptain Kobold – Flickr
  52. Don't call an interface an interface
  53. Written, verbal, call a meeting, socialize it, lunch and learns. Give it time.