personal-page/_master_wiki/void/Readwise/Dependency Injection, the Best Pattern.md

1.8 KiB

Dependency Injection, the Best Pattern

rw-book-cover

Metadata

[!tldr] The text explains how to use dependency injection to build an attachment service that uploads files. By creating an interface for storage and injecting it into the request handler, the code becomes simpler and less error-prone. This approach also allows for easy testing with mock implementations.

Highlights

have a piece of code which uses another piece of code, and instead of using that code directly, it's passed in instead. View Highlight)

Injection basically just lets us pick and choose from our compatible puzzle pieces and then slot them in when we need them. View Highlight)

We can use injection to inject fake or mock implementations instead, which basically means we can slice and dice up our architecture to isolate sections of code during testing. View Highlight)

A natural side effect of having nice code is that it's easy to test without needing to hack around the code structure. If you find yourself asking, how can I test a private method? Or I need to set some internal variable in order to test. That's a signal that you maybe need to pull some stuff out, that you need to isolate some part of it by separating it and injecting it instead. View Highlight)