Thursday, April 8, 2021

SOLID | Dependency Inversion Principle (DIP)

 The dependency Inversion Principle, it states high level modules should not depend on low level modules. This is introduced by Robert C. Martin. 

Points to remember, 

  • High level and low level modules should depend upon abstractions 
  • and abstraction should not directly be dependent on the details but details should depend upon abstractions
  • Normally we have seen a project structure like
    • Presentation Layer - Business Layer - Data Access Layer (This is highly coupled) 
    • But introducing this principle suggests going with High Level Modules - Abstractions - Low Level Modules (This makes it loosely coupled)
    • So any changes to Low level should not affect directly to high level modules

Example,

will go first with Presentation Layer - Business Layer - Data Access Layer so,

public class BusinessLayer
{
        private DataAccessLayer dataAccessLayer;
        public BusinessLayer()
        {
                dataAccessLayer = new DataAccessLayer();
        }
        public void Save()
        {
                dataAccessLayer.Save();    
        }

public class DataAccessLayer
{
        public void Save()
        {
            //Method implementation
        }
}

Now, following the example, you see it is a strongly coupled and dependent architecture. Any change coming to DataAccessLayer would affect the existing implementation. 

In place, if we introduce another layer like Repository that acts as an abstraction to make this module a decoupled one. 

public class BusinessLayer
{
        //private DataAccessLayer dataAccessLayer;    This is commented to implement the abstraction
        private IRepositoryLayer repositoryLayer;
        public BusinessLayer(IRepositoryLayer repo)
        {
                repositoryLayer = repo;
        }
        public void Save()
        {
                repositoryLayer.Save();    
        }

public class DataAccessLayer : IRepositoryLayer        
{
        public void Save()
        {
            //Method implementation
        }
}

public interface IRepositoryLayer
{
        void Save();
}

Now, this works great to implement the Dependency Inversion Principle, We have inverted the dependency directly to an abstract layer, so both are loosely coupled, and if any change is coming up then it can be easily modified and even this makes unit testing the code more easily. 

In conclusion, as this is the last from SOLID principles, we have come across the basics of it and understanding with real world scenarios and examples. I hope anyone at glance would be able to grasp the foundation of principles. 

Any doubts or suggestions or thoughts are completely welcome, let me know if you want to know more in such simple language and examples. 

No comments:

Post a Comment

C# Interview Prep: 100 Common Questions for 0-3 Years Experience

Common C# Interview Questions  Basics of C# Language: What is C#? C# (pronounced C sharp) is a modern, object-oriented programming language...