Download
building solid software with dependency injection n.
Skip this Video
Loading SlideShow in 5 Seconds..
Building SOLID Software with Dependency Injection PowerPoint Presentation
Download Presentation
Building SOLID Software with Dependency Injection

Building SOLID Software with Dependency Injection

93 Views Download Presentation
Download Presentation

Building SOLID Software with Dependency Injection

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Building SOLID Software with Dependency Injection Jeremy Rosenberg

  2. Me

  3. Who cares about software design?

  4. What are our goals? • Deliver yesterday • Communicate telepathically • No bugs

  5. What are our goals? • Deliver yesterday • Communicate telepathically • No bugs

  6. What are our goals? • Deliver quickly • Communicate quickly • Few bugs • Fix bugs quickly • Deliver yesterday • Communicate telepathically • No bugs

  7. What are our goals? • Deliver quickly • Communicate quickly • Few bugs • Fix bugs quickly • Deliver yesterday • Communicate telepathically • No bugs aka Efficiently deal with Change

  8. What is a bad design?

  9. A bad design causes these • Deliver slowly or unpredictably • Slow ramp-up for new team members • Slow sharing between veteran team members

  10. A bad design causes these • Frequent bugs • Simple bugs need complex changes • Lots of regressions • Lots of regression testing

  11. What is a good design?

  12. What is SOLID?

  13. What it is • Catchy acronym • Principles for dealing with change

  14. What it is • Catchy acronym • Principles for dealing with change

  15. What it is • Catchy acronym • Principles for dealing with change SDOLI

  16. What it’s not • Framework • Library • Pattern • Goal

  17. What is Dependency Injection? • Pattern • Jives well with SOLID

  18. What kinds of code? • Object-oriented • Statically typed* (code examples are in C#)

  19. What does this method do?

  20. What does this method do? • Coordinates a book checkout process for a member • Checkout • Storage

  21. What does this method do? • Coordinates a book checkout process for a member • Checkout • Storage Is that all?

  22. What does this class do?

  23. What does this class do? • Configuration • DB Access

  24. So what does this method really do? • Checkout • Storage

  25. So what does this method really do? • Checkout • Storage • Configuration • DB access Implicit!

  26. Let’s write a test case

  27. SDOLI: Single Responsibility Principle • “Do one thing and do it well” • Applies to a method, class, service, system, …

  28. How do we isolate the coordination?

  29. SDOLI: Dependency Inversion Principle • Depend on contracts, not implementations • Goal: Decouple the single responsibility from implementations of its dependencies

  30. SDOLI: Dependency Inversion Principle • Depend on a contract at design-time • e.g. IBookRepository or BookRepositoryBase • Receive an implementation at runtime • e.g. SqlBookRepository

  31. Dependency Injection • A class declares its dependencies through its own contract • Providing dependencies is someone else’s job

  32. Dependency Injection • Constructor injection • Property injection • Method injection

  33. Constructor injection

  34. Our test case • No configuration • No database • Only coordination

  35. All this “just” for test cases? • What else have we gained? • “Tightly bound, loosely coupled” • Focused responsibility => readability • Dependent on behavior, not implementation • Dependencies are explicit • Limits unexpected side effects • Straightforward (re)usage • Composability

  36. You’re a hack!

  37. Composition Root • Constructs the object graph • Lives near the entry point

  38. Example: Console app

  39. What about that repository?

  40. Make the dependencies explicit

  41. Updated Composition Root

  42. With me?