Intro

When a block of code needs another block of code to complete a task, but instead of creating / initializing the needed code from within, it gets passed as dependency.
It’s a very natural way to structure and abstract things and promotes SOILID Principles of OOP specifically Single Responsibility (SR).
For example, think of a situation where object A’s class needs an instance of B class to do something.
Instead of initializing the needed instance inside A, it gets passed (injected) into its constructor as a dependency. This also relates to composition.

Effects

Injecting Interfaces

By designing an interface and defining its method signatures, which are then implemented by various classes that each implement in their own way, we can inject any class that implements the interface wherever its needed.
This supercharges Separation of Concerns and SR.

Testing

By Injecting interfaces, we can test parts of our code in isolation using mock implementations of these interfaces and inject them instead.


References