How I Learned To Love Dependency Injection


Published on

My presentation from the 12/7/2009 ArcSummit in Chicago put on by It covers Dependency Injection and a couple of the IOC containers.

Get more information and link to source code at

Published in: Technology, Business
  • Be the first to comment

How I Learned To Love Dependency Injection

  1. 1. How I learned to love Dependency Injection<br />Tim Murphy<br />Technical Specialist - PSC Group, LLC<br />Co-Leader – Chicago Architects Group<br />
  2. 2. How I learned to love Dependency Injection(IOC Contaners)<br />Tim Murphy<br />Technical Specialist - PSC Group, LLC<br />Co-Leader – Chicago Architects Group<br />
  3. 3. The Pursuit of the Loosely Coupled System<br />
  4. 4. Traditional Object Structure<br />Database Access Service<br />Entity<br /><ul><li>Database Access Service
  5. 5. Service Proxy
  6. 6. Logging Service
  7. 7. Etc..</li></ul>Service Proxy<br />Logging Service<br />Tightly coupled services!<br />
  8. 8. <ul><li> Doesn’t promote reusability
  9. 9. Creates brittle systems
  10. 10. Difficult to “mock” makes testing difficult</li></li></ul><li>With Dependency Injection<br />
  11. 11. The End(Or is it)<br />
  12. 12. … but still a lot of work<br />Better….<br />There are so many dependencies in this system, how do I manage them all?<br />
  13. 13. Dependency InjectionWith a Factory Pattern<br />Entity<br />“I need to be created, including have all my services crated and given to me.”<br />Object Factory <br />“ I know how to build all of this stuff.”<br />Create a<br />Concrete instance<br />Create an<br />Entity for me<br />Instance for<br />application<br />Application<br />“I need an Entity, but I don’t know how to create it.”<br />
  14. 14. I want to spend my day writing functional business code, NOT a bunch of plumbing code…<br />
  15. 15. Dependency Injection Frameworks to the Rescue!<br />Structure Map<br />Ninject<br />Windsor <br />Microsoft Unity<br />
  16. 16. With a Dependency Injection/ IOC Framework<br />Entity<br /><ul><li>Database Access Service
  17. 17. Service Proxy
  18. 18. Logging Service
  19. 19. Etc..</li></ul>Application<br />“I need a concrete instance of an Entity…”<br />Provider<br />“You tell me what type of object you want, I give you the object you need:”<br />Container<br />“You ask me for objects, I create them based on my registrations”<br />
  20. 20. DEMO TIME!<br />
  21. 21. Some helpful links…<br />Martin Fowler <br /><br />MSDN Article on Dependency Injection pattern<br /><br />Dependancey Injection tutorial<br /><br />
  22. 22. Some helpful links…<br />DI/IOC Frameworks:<br /><br /><br /><br />Unity (<br />
  23. 23. Chicago Architects Group<br />
  24. 24. twmurph<br />