D In SOLID - Dependency Inversion Principle (DIP)

This is the Last principle in SOLID. Definition of DIP is as below, basically, it has 2 points,
  • High-level modules should not depend on low-level modules. Both should depend on the abstraction.
  • Abstractions should not depend on details. Details should depend on abstractions.
DIP is used to convert Tightly Coupled Architecture to Loosely Coupled Architecture.
 
To implement DIP firstly we need to understand Abstraction. In simple words, Abstraction means something which is non-concrete. abstraction in programming means to create an interface or an abstract class which is non-concrete. This means we cannot create an object of an interface or an abstract class.
 
For example, try to understand below code,
  1. public class CustBL  
  2. {  
  3.     public CustBL()  
  4.     {  
  5.     }  
  6.   
  7.     public string GetCustName(int id)  
  8.     {  
  9.         DataAccess _dataAccess = DAFactory.GetDAObj();  
  10.   
  11.         return _dataAccess.GetCustName(id);  
  12.     }  
  13. }  
  14.   
  15. public class DAFactory  
  16. {  
  17.     public static DataAccess GetDAObj()   
  18.     {  
  19.         return new DataAccess();  
  20.     }  
  21. }  
  22.   
  23. public class DataAccess  
  24. {  
  25.     public DataAccess()  
  26.     {  
  27.     }  
  28.   
  29.     public string GetCustName(int id) {  
  30.         return "Dinesh"// get actual from DB  
  31.     }  
  32. }  
The above CustBL and DataAccess are concrete classes, meaning we can create objects of them.
 
Now if we want to create loosely coupled architecture, we need to implement DIP as below.
  1. public interface ICustDA  
  2. {  
  3.     string GetCustName(int id);  
  4. }  
  5.   
  6. public class CustDA: ICustDA  
  7. {  
  8.     public CustDA() {  
  9.     }  
  10.   
  11.     public string GetCustName(int id) {  
  12.         return "Dinesh";          
  13.     }  
  14. }  
  15.   
  16. public class DAFactory  
  17. {  
  18.     public static ICustDA GetCustDAObj()   
  19.     {  
  20.         return new CustDA();  
  21.     }  
  22. }  
  23.   
  24. public class CustBL  
  25. {  
  26.     ICustDA _custDA;  
  27.   
  28.     public CustBL()  
  29.     {  
  30.         _ custDA = DAFactory.GetCustDAObj();  
  31.     }  
  32.   
  33.     public string GetCustName(int id)  
  34.     {  
  35.         return _ custDA.GetCustName(id);  
  36.     }  
  37. }  
In this way we can implement Dependency Inversion Principle - DIP using very basic concept of Object Oriented Programming concept - Abstraction. The advantages of implementing DIP in the above example are CustBL and CustDA classes are loosely coupled and CustBL does not depend on the concrete DataAccess class, it includes a reference of the ICustDA interface. With the help of this, we can easily use another class that implements ICustDA with a different implementation. And here we achieved both the points in DIP
  • High-level modules should not depend on low-level modules. Both should depend on the abstraction.
  • Abstractions should not depend on details. Details should depend on abstractions.
At last, we always need to implement loosely coupled architecture in our application to accommodate changes at any point or phase of development and support. Without loosely coupled architecture it is very difficult to maintain, test and accommodate changes in the application. DIP is one of the most important principle to implement loosely coupled.
 
Here S.O.L.I.D. article series completed. Even if we only follow SOLID principles, we will be safe and good in terms of architecture and coding standards. It will help us to maintainable, testable, readable, extensible and easily accommodate changes.
 
--Happy Coding--


Similar Articles
Logiciel Softtech Pvt. Ltd.
We help you transform your business ideas with custom products, migrate from legacy applications.