Dependency Inversion Principle (DIP) – S.O.L.I.D. Framework

The fifth and final principle within the S.O.L.I.D. Framework is the Dependency Inversion Principle.

The main statement of this principle is “Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions”.

On the first sight this may sounds useless, but at the latest when you have to change legacy code – in the following example the database connection – you will love this principle.

In the following example we have an MS SQL connection which violates the Dependency Inversion Principle.

If we are now using this class to connect to the actual database we violate this principle by passing the concrete implement of the SQLDatabase class.

Instead of using the concrete ones we should rather introduce interfaces and pass those through. The correct implementation would look like this:

Should at some point in the future the Database Connection change or a new Database should be introduced you only have to add a new implementation of the database with the IDatabaseConnection interface. The Usermanger class will not be affected by those changes because this class only depends on the interface itself and not on concrete implementations.



Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.