The Dependency Inversion Principle
In our layered architecture, the cross-layer dependencies always point down to the next layer. When we apply the Single Responsibility Principle on a high level, we notice that the upper layers have more reasons to change than the lower layers.
Thus, due to the domain layer’s dependency on the persistence layer, each change in the persistence layer potentially requires a change in the domain layer. But the domain code is the most important code in our application! We don’t want to have to change it when something changes in the persistence code!
So, how can we get rid of this dependency?
The Dependency Inversion Principle (DIP) provides the answer.
In contrast to the SRP, the DIP means what the name suggests:
We can turn around (invert) the direction of any dependency within our code base2
2 Actually, we can only invert dependencies when we have control over the code on both ends of the dependency. If we have a...