Dependency Injection vs Abstractions

2019/08/29

Categories: Software Design Tags: design software design dependency injection

After some debates about using dependency injection versus hiding implementation details (a.k.a. abstraction) which came up during code reviews, I thought about writing some opinion on these two approaches which seem to be opposing each other.

Dependency injection is the norm in modern programming, but people seem to apply it blindly without taking into consideration other perspectives. Some use cases for dependency injection:

An opposing perspective to passing dependencies from top to bottom in a class hierarchy is hiding implementation details or abstraction. Some use cases would be:

Of course, when designing software there is no right solution for a problem - there is only the solution that solves the problem taking into consideration the context and the constraints.

Also, these “winner” solutions can be the correct ones for a given time only - since tech is changing so fast, yesterday’s solutions become obsolete and need to be updated - this is another reason why there are no perfect solutions.