Inversion of Control and Dependency Injection

Introduction

This article explains the Inversion of Control and Dependency Injection. Inversion of control is a principle, and dependency injection is a way of implementing inversion of control. This article also explains each method with an example.

What is Inversion of Control?

public class clsDAL

{
    private clsSqlServer _sql; 

    public clsDAL()
    {
        _sql = new clsSqlServer(); 
    }
}

Consider the above example, where we have a DAL class. The default constructor of the DAL class creates an object of the SqlServer class. That means the DAL Class is responsible for creating an object of the SqlServer class. So there is a tight coupling between the DAL class and SqlServer class.

Three main problems in the above code

  1. The DAL class is responsible for creating an object of the SqlServer class.
  2. The SqlServer class is directly referenced in the DAL class.
  3. The DAL class should be aware of the SqlServer class type.

Now we understand the problem. The solution is to shift the object creation part from this class. We must change the object creation control from here, i.e., Inversion of control. Now let's discuss the solution.

Principles of IOC

  1. Main classes aggregating other classes should not depend on the direct implementation of the aggregated classes. Both classes should depend on abstraction.
  2. Abstraction should not depend on details; details should depend on abstraction.

What is Dependency Injection?

Inversion of control is implemented by dependency injection because Inversion of control is a principle, and dependency injection is a way of implementing IOC.

Different ways of implementing IOC

Implementing-IOC.jpg

Now let's discuss each method with an example.

Constructor Methodology

In this methodology, we pass an object of SQL into the DAL class. In the above code, you can see a parameterized constructor in the DAL class. And the parameterized constructor accepts an object of SQL. In this case, the DAL class is not responsible for creating an object of SQL. So there is no tight coupling between these classes. This method is not helpful for the client, who only can use a default constructor.

public class clsDAL
{
    private ISql _sql;    public clsDAL(ISql obj)
    {
         _sql = obj;
    }
}    

Setter and Getter

Encapsulation means hiding the internal details of an object. In this method, we expose an object of SQL through the get/set methods of the DAL class. But it violates the encapsulation rule of OOP. So here, rather than hiding an object, we are exposing an object.

public class clsDAL
{
    private ISql _sql;    public Isql Sql
    {
        set
        { 
             _sql = value; 
        }
    }
}

Interface Implementation

In the preceding code, we implemented an interface with a setConnection method that sets the SQL object. And the DAL class implements a SQL interface. So with the help of the setConnection method, the client can inject a SQL object in the DAL class.

interface ISqlDI
{
    void setConnection(ISql obj);
}
public class clsDAL : ISqlDI
{
    private ISql _sql;    public void setConnection(ISql obj)
    {
        _sql = obj;
    }
}

Server Locator

In this method, we create a static class and a static method inside this class. The DAL class calls this static method from its default constructor. So, the SQL object is injected into the DAL class.

static class LocateConnection

{
    public static ISql getConnection() { }
}
 interface clsDAL
{
    private ISql _sql;
 
    public clsDAL()
    {
        _sql = LocateConnection.getConnection();
    }
}

 


Similar Articles