building solid software with dependency injection jeremy rosenberg
TRANSCRIPT
![Page 1: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/1.jpg)
Building SOLID Software with Dependency Injection
Jeremy Rosenberg
![Page 2: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/2.jpg)
Me
![Page 3: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/3.jpg)
![Page 4: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/4.jpg)
![Page 5: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/5.jpg)
![Page 6: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/6.jpg)
![Page 7: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/7.jpg)
![Page 8: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/8.jpg)
Who cares about software design?
![Page 9: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/9.jpg)
What are our goals?
• Deliver yesterday• Communicate telepathically• No bugs
![Page 10: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/10.jpg)
What are our goals?
• Deliver yesterday• Communicate telepathically• No bugs
![Page 11: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/11.jpg)
What are our goals?
• Deliver quickly• Communicate quickly• Few bugs• Fix bugs quickly
• Deliver yesterday• Communicate telepathically• No bugs
![Page 12: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/12.jpg)
What are our goals?
• Deliver quickly• Communicate quickly• Few bugs• Fix bugs quickly
• Deliver yesterday• Communicate telepathically• No bugs
aka
Efficiently deal with Change
![Page 13: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/13.jpg)
What is a bad design?
![Page 14: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/14.jpg)
A bad design causes these
• Deliver slowly or unpredictably• Slow ramp-up for new team members• Slow sharing between veteran team members
![Page 15: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/15.jpg)
A bad design causes these
• Frequent bugs• Simple bugs need complex changes• Lots of regressions• Lots of regression testing
![Page 16: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/16.jpg)
What is a good design?
![Page 17: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/17.jpg)
What is SOLID?
![Page 18: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/18.jpg)
What it is
• Catchy acronym• Principles for dealing with change
![Page 19: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/19.jpg)
What it is
• Catchy acronym• Principles for dealing with change
![Page 20: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/20.jpg)
What it is
• Catchy acronym• Principles for dealing with change
SDOLI
![Page 21: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/21.jpg)
What it’s not
• Framework• Library• Pattern• Goal
![Page 22: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/22.jpg)
What is Dependency Injection?
• Pattern• Jives well with SOLID
![Page 23: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/23.jpg)
What kinds of code?
• Object-oriented• Statically typed*
(code examples are in C#)
![Page 24: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/24.jpg)
What does this method do?
![Page 25: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/25.jpg)
What does this method do?– Coordinates a book checkout process for a member
• Checkout• Storage
![Page 26: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/26.jpg)
What does this method do?– Coordinates a book checkout process for a member
• Checkout• Storage
Is that all?
![Page 27: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/27.jpg)
What does this class do?
![Page 28: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/28.jpg)
What does this class do?
• Configuration
• DB Access
![Page 29: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/29.jpg)
So what does this method really do?
• Checkout• Storage
![Page 30: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/30.jpg)
So what does this method really do?
• Checkout• Storage
– Configuration– DB access
Implicit!
![Page 31: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/31.jpg)
Let’s write a test case
![Page 32: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/32.jpg)
SDOLI: Single Responsibility Principle
• “Do one thing and do it well”
• Applies to a method, class, service, system, …
![Page 33: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/33.jpg)
![Page 34: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/34.jpg)
How do we isolate the coordination?
![Page 35: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/35.jpg)
SDOLI: Dependency Inversion Principle
• Depend on contracts, not implementations
• Goal: Decouple the single responsibility from implementations of its dependencies
![Page 36: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/36.jpg)
SDOLI: Dependency Inversion Principle
• Depend on a contract at design-time– e.g. IBookRepository or BookRepositoryBase
• Receive an implementation at runtime– e.g. SqlBookRepository
![Page 37: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/37.jpg)
![Page 38: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/38.jpg)
Dependency Injection
• A class declares its dependencies through its own contract
• Providing dependencies is someone else’s job
![Page 39: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/39.jpg)
Dependency Injection
• Constructor injection• Property injection• Method injection
![Page 40: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/40.jpg)
Constructor injection
![Page 41: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/41.jpg)
Our test case
• No configuration• No database• Only coordination
![Page 42: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/42.jpg)
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
![Page 43: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/43.jpg)
You’re a hack!
![Page 44: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/44.jpg)
Composition Root
• Constructs the object graph
• Lives near the entry point
![Page 45: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/45.jpg)
Example: Console app
![Page 46: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/46.jpg)
What about that repository?
![Page 47: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/47.jpg)
Make the dependencies explicit
![Page 48: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/48.jpg)
Updated Composition Root
![Page 49: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/49.jpg)
With me?
![Page 50: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/50.jpg)
![Page 51: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/51.jpg)
SDOLI: Open/Closed Principle
• Open for extensibility, closed for modification
• Add behavior without changing existing code
![Page 52: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/52.jpg)
![Page 53: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/53.jpg)
Decorator
• Inject behavior into an application
• Man-in-the-middle
![Page 54: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/54.jpg)
Example: Document storage
![Page 55: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/55.jpg)
Composition Root
![Page 56: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/56.jpg)
Add Logging
![Page 57: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/57.jpg)
Add Logging
![Page 58: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/58.jpg)
Add Authorization
![Page 59: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/59.jpg)
Add Authorization
![Page 60: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/60.jpg)
Add Caching
![Page 61: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/61.jpg)
Add Caching
![Page 62: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/62.jpg)
Noticing a pattern?
![Page 63: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/63.jpg)
Open…
![Page 64: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/64.jpg)
Closed.
![Page 65: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/65.jpg)
Closed.
![Page 66: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/66.jpg)
Closed.
![Page 67: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/67.jpg)
Closed.
![Page 68: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/68.jpg)
Closed.
![Page 69: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/69.jpg)
Composite
• Implements a contract• Delegates to others with the same contract
![Page 70: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/70.jpg)
Example
![Page 71: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/71.jpg)
Build it like this
![Page 72: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/72.jpg)
Use it like this
![Page 73: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/73.jpg)
A more concrete example
![Page 74: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/74.jpg)
Move the responsibility
![Page 75: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/75.jpg)
Notify billing
![Page 76: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/76.jpg)
Notify inventory
![Page 77: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/77.jpg)
Notify shipping
![Page 78: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/78.jpg)
Dispatch all notifications
![Page 79: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/79.jpg)
Open / closed
![Page 80: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/80.jpg)
SDOLI: Liskov Substitution Principle
• Functions that use pointers to base types must be able to use objects of derived types without knowing it.
• “Leaky abstraction principle”-Me
![Page 81: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/81.jpg)
![Page 82: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/82.jpg)
Is a Square a Rectangle?
![Page 83: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/83.jpg)
Is a Square a Rectangle?
SetWidth / SetHeight ?
![Page 84: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/84.jpg)
Example: calculator
![Page 85: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/85.jpg)
Usage
![Page 86: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/86.jpg)
Guess what’s next
![Page 87: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/87.jpg)
Guess what’s next
![Page 88: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/88.jpg)
Guess what’s next
Oops
![Page 89: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/89.jpg)
Plug the leak – Part 1
![Page 90: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/90.jpg)
Plug the leak – Part 1
- Nine fingers left!
![Page 91: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/91.jpg)
Plug the leak – Part 2
![Page 92: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/92.jpg)
What to do
• Some ideas– Kill the abstraction– Adapt each calculator to something that supports
input validation
![Page 93: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/93.jpg)
Another example: more documents…
![Page 94: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/94.jpg)
Read-only integration
![Page 95: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/95.jpg)
Oops
![Page 96: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/96.jpg)
Plug the leak
![Page 97: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/97.jpg)
What to do?
• Some ideas– IReadOnlyDocRepo and IReadWriteDocRepo– IReadDocs and IWriteDocs– SupportsXYZ methods
![Page 98: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/98.jpg)
How can DI help apply the LSP?
![Page 99: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/99.jpg)
How can DI help apply the LSP?
It can’t.
![Page 100: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/100.jpg)
![Page 101: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/101.jpg)
SDOLI: Interface Segregation Principle
• Thin interfaces are better than fat ones
• Client-focused
![Page 102: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/102.jpg)
![Page 103: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/103.jpg)
Example: more books…
![Page 104: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/104.jpg)
Who’s using it?
![Page 105: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/105.jpg)
Cache books?
![Page 106: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/106.jpg)
Don’t need these
![Page 107: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/107.jpg)
These are ok
![Page 108: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/108.jpg)
What about these?
![Page 109: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/109.jpg)
Client-specific interface
![Page 110: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/110.jpg)
Adapter
This:
or this:
![Page 111: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/111.jpg)
Slimmer caching
![Page 112: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/112.jpg)
Focused dependencies
![Page 113: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/113.jpg)
Let’s review
![Page 114: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/114.jpg)
SOLID Principles
• Single Responsibility• Open/Closed• Liskov Substitution• Interface Segregation• Dependency Inversion
![Page 115: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/115.jpg)
![Page 116: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/116.jpg)
I don’t know about you…
![Page 117: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/117.jpg)
DI Container
• Auto-wiring of dependencies
• Lifetime management
![Page 118: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/118.jpg)
Ninject
![Page 119: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/119.jpg)
Ninject
Register
![Page 120: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/120.jpg)
Ninject
Register
Resolve
![Page 121: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/121.jpg)
Ninject
Register
Resolve
Release
![Page 122: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/122.jpg)
Other containers
• Castle Windsor• Autofac• StructureMap• Unity• …
![Page 123: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/123.jpg)
What not to do
![Page 124: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/124.jpg)
Don’t use Service Locator
![Page 125: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/125.jpg)
Don’t use Service Locator
• Hides dependencies• Consumers must use the same container• Testing becomes un-fun
![Page 126: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/126.jpg)
Don’t use Service Locator
• Composition root should be the only place referencing the DI Container.
• General rule: if it’s not just initializing the object graph, it’s not the composition root.
![Page 127: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/127.jpg)
Don’t just new it
![Page 128: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/128.jpg)
Don’t just new it
• Useless interface definition• Same tight coupling• No way to swap, decorate, or otherwise
compose implementations
![Page 129: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/129.jpg)
Don’t be double minded
![Page 130: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/130.jpg)
Don’t be double minded
• Two paths…• Dependencies are still not always apparent• Auto-wiring can be difficult
![Page 131: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/131.jpg)
Building SOLID software
![Page 132: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/132.jpg)
Building SOLID software
• SOLID encourages good designs
• Dependency injection is a tool to bring closer alignment with SOLID
![Page 133: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/133.jpg)
Join us
![Page 134: Building SOLID Software with Dependency Injection Jeremy Rosenberg](https://reader038.vdocuments.net/reader038/viewer/2022110209/56649e375503460f94b270ad/html5/thumbnails/134.jpg)
Resources
• The Principles of OOD http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
• Dependency Injection in .NET by Mark Seemann
• ploeh blog – DependencyInjection http://blog.ploeh.dk/CategoryView,category,DependencyInjection.aspx
• SOLID (object-oriented design) http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)