Dependency injection can be summarized as *"A piece of code that uses a piece of code indirectly, by being passed in"*.
Dependency Injection makes sense when the *business logic* of the code can happen with a few different ways, but they map to the same *action* (e.g. Uploading a file to a Filesystem, but that FS can be AWS, Azure, etc...)
Additionally, it makes sense to use when the logic of deciding which business logic to use is elsewhere.
Dependency Injection totally makes sense with the [[Factory Pattern]]! If you think about it, all the factory pattern does decouple the code that makes the decision vs. uses the code.
Dependency creation can work in a many-to-one configuration (Factory) or one-to-many
(multiple pieces of code use an ImageScaler).
Time of injection can vary. It usually happens per-request or on initialization.
By splitting modules in this way, it becomes very easy to test.
## Strategies for Making Code Injectable
1. Break modules down into parts. If you have code responsible for uploading, each should be it's own module injected into the function instead with a common [[Interface]].
2. Modules that aren't atomic are likely good candidates for breaking into smaller parts. Think about classes of modules, and if there ever could be something else that does this functionality (VirusScanner class could be two virus scanners)
---
# References
https://www.youtube.com/watch?v=J1f5b4vcxCQ (Amazing Visuals!)