What is Dependency Inversion and why is it necessary?
Dependency Inversion is the fifth SOLID principle. It states that components must not interact directly with one another and must be done via abstractions.
To understand what it means, let's take a general example of a Plug and Socket. Imagine you want to setup your desktop in a room, the most general and common thing you do for your power supply is to set a socket board and plug in your desktop to it.
There's also another way - you can directly attach your desktop wires to the power supply wires.
It may sound stupid - but that is what we call a tightly coupled system. A system where components are stuck with one another.
The disadvantage? Same as what happens when you wire up your desktop to the power supply - the position is now fixed and you can't move it.
A loosely coupled system has components which can be replaced if necessary and are connected via abstractions - a socket and a plug is an example for a loosely coupled system.
In dotnet or in any other framework, you can use an interface or an abstract class to declare what the implementation must look like and use that while passing as an argument or a parameter.
For example, use IEnumerable as a return type or a parameter type instead of a concrete List or Array. That way the calling components are free to pass any implementation of their choice instead of sticking to one.
#cleancode #design #architecture #engineering #programming #dependencies #bestpractices