Dependency Injection

What is Dependency Injection

In simple words, Dependency Injection is a software design pattern, and object instances are initiated with the help of a few mechanisms instead of creating manually.

Why Dependency Injection

The following thing can be achieved while using DI

  • Loosely coupling
  • Maintainability
  • Testability
  • Extensibility
  • Modularity

Types of Dependency Injection

  • Constructor Injection
  • Property/Setter Injection
  • Method Injection

Old method or Manually Creating object 

Usually, we initiate an object like the below snippet, 

For eg., Consider we have Employee Class with us 

Employee employee = new Employee(); 

Whenever we do like above, wherever we use this Employee Class is coupled. So in the future, if you want to swift anything we need to change all the places accordingly.

Example of DI Injection 

Let's go we create an example with the following class

  • SqlService
  • MockSqlService
  • NonSqlService
  • MockNonSqlService

This is a High-Level interface and point of the contact interface

/// <summary>
/// High Level Interface
/// </summary>
public interface IDataService {
    void ConnectToData();
}

 Here, we created the above classes and inherited a high-level interface. We inherited because the point of contact is abstract level.

public class SqlService: IDataService {
    public void ConnectToData() {
        Console.WriteLine("SqlService Connecting");
    }
}
public class NoSqlService: IDataService {
    public void ConnectToData() {
        Console.WriteLine("NoSqlService Connecting");
    }
}
public class MockSqlService: IDataService {
    public void ConnectToData() {
        Console.WriteLine("MockSqlService Connecting");
    }
}
public class MockNoSqlService: IDataService {
    public void ConnectToData() {
        Console.WriteLine("MockNoSqlService Connecting");
    }
}

This is the kind of concrete factory class where it doesn't have any dependency on child-level module

public class Connector {
    private IDataService _dataService;
    public Connector(IDataService dataService) {
        this._dataService = dataService;
    }
    public void ConnectToData() {
        this._dataService.ConnectToData();
    }
}

This is our kick-start module. Here you can get a clear vision about how we are making loosely couple concepts. 

class Program {
    static void Main(string[] args) {
        //Without DI
        //In both case 
        //First Style MockSqlService is dependant on here
        MockSqlService mockSqlService = new MockSqlService();
        mockSqlService.ConnectToData();
        //Second Style
        IDataService dataService = new MockSqlService();
        dataService.ConnectToData();
        //With DI
        //creating object
        //May be for development I am using MockSqlService
        //After Schema ready i need to change only next line as SqlService
        //MockSqlService Development
        MockSqlService mockSqlServiceDI = new MockSqlService();
        Connector connector = new Connector(mockSqlServiceDI);
        connector.ConnectToData();
        SqlService sqlServiceDI = new SqlService();
        Connector sqlconnector = new Connector(sqlServiceDI);
        connector.ConnectToData();
        Console.ReadLine();
    }
}

Note
Kindly use DI container while using Dependency Injection Concept. Without a DI container, it's not advisable. 

What do you think?

I gave the basic concept of Dependency Injection and how to make a loose coupling code. I hope you enjoyed it.

I would like to have feedback from my readers. Your valuable feedback, question, or comments about this article are always welcome.