1.34k likes | 1.46k Views
Building SOLID Software with Dependency Injection. Jeremy Rosenberg. Me. Who cares about software design?. What are our goals?. Deliver yesterday Communicate telepathically No bugs. What are our goals?. Deliver yesterday Communicate telepathically No bugs. What are our goals?.
E N D
Building SOLID Software with Dependency Injection Jeremy Rosenberg
What are our goals? • Deliver yesterday • Communicate telepathically • No bugs
What are our goals? • Deliver yesterday • Communicate telepathically • No bugs
What are our goals? • Deliver quickly • Communicate quickly • Few bugs • Fix bugs quickly • Deliver yesterday • Communicate telepathically • No bugs
What are our goals? • Deliver quickly • Communicate quickly • Few bugs • Fix bugs quickly • Deliver yesterday • Communicate telepathically • No bugs aka Efficiently deal with Change
A bad design causes these • Deliver slowly or unpredictably • Slow ramp-up for new team members • Slow sharing between veteran team members
A bad design causes these • Frequent bugs • Simple bugs need complex changes • Lots of regressions • Lots of regression testing
What it is • Catchy acronym • Principles for dealing with change
What it is • Catchy acronym • Principles for dealing with change
What it is • Catchy acronym • Principles for dealing with change SDOLI
What it’s not • Framework • Library • Pattern • Goal
What is Dependency Injection? • Pattern • Jives well with SOLID
What kinds of code? • Object-oriented • Statically typed* (code examples are in C#)
What does this method do? • Coordinates a book checkout process for a member • Checkout • Storage
What does this method do? • Coordinates a book checkout process for a member • Checkout • Storage Is that all?
What does this class do? • Configuration • DB Access
So what does this method really do? • Checkout • Storage
So what does this method really do? • Checkout • Storage • Configuration • DB access Implicit!
SDOLI: Single Responsibility Principle • “Do one thing and do it well” • Applies to a method, class, service, system, …
SDOLI: Dependency Inversion Principle • Depend on contracts, not implementations • Goal: Decouple the single responsibility from implementations of its dependencies
SDOLI: Dependency Inversion Principle • Depend on a contract at design-time • e.g. IBookRepository or BookRepositoryBase • Receive an implementation at runtime • e.g. SqlBookRepository
Dependency Injection • A class declares its dependencies through its own contract • Providing dependencies is someone else’s job
Dependency Injection • Constructor injection • Property injection • Method injection
Our test case • No configuration • No database • Only coordination
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
Composition Root • Constructs the object graph • Lives near the entry point