Understand Dependency Injection: Constructor Injection

In this article we will learn Dependency Injection in application development. If you are an experienced developer or very much interested in learning various design patterns and software architectures then you are probably familiar with Dependency Injection. Anyway we will proceed with our explanation.

What is Dependency Injection?

Dependenct Injection is one kind of architectural style that helps to create a loosely coupled architecture. The question may arise, "What are the benefits of implementing a loosely couple architecture"? The answer is:

  • Increase code reusability
  • Increase code maintainability
  • Improve application testing

So, those are the benefits of Dependency Injection in applications. The main goal of Dependency Injection is that it helps to implement a decoupled architecture that will help the application in the long run. (The word long run indicates the nature of the customer's requirements).

The Dependency Injection pattern uses a builder object to initialize an object and provide the required dependencies to the object. In other words it allows you to "inject" a dependency from outside of the class.

The lines above are the corporate definition and very tough to understand. Let's try to understand this in simple and practical terms.

Assume that we have a minister in parliament. Each minister has their own secretary to do all his official work. Who is a government employee? Most often common people are not permitted to meet with a minister. But they are allowed to meet with their secretary. (It's our Indian scenario; if you are outside of India then it might be different. Ha.. Ha.. Anyway share your country's rules in the comment section). Now the scenario is "Common people communicate with a minister via his secretary". And we know that ministers are not government employees (again Indian scenario) and anytime they can change. Though ministers are changeable, the secretaries remain unchanged.

Dependency injection.gif

Now, think that ministers are the base class and the secretary are the interface and the common persons are another class who want to talk with the minister class. The implementation is as in the following.

The question is, "What is the benefit of keeping the secretary between the minister and the common person?". As we explained, the minister may change but the secretary does not change. If we change something in the implementation of a base class also then it will not effect the consumer class. Because the consumer class (here the Person class) is talking to the base class via an interface. Hence the architecture became loosely coupled.

We can implement Dependency Injection in various ways in our application and Constructor Injection is one of them.

Understand Constructor Injection

In the following example we will implement the preceding scenario. In the following code we have implemented an ISecrutary interface that we have implemented in a Minister Class. The reason is, our minister class will talk using an ISecretuary interface with the outside world. Have a look at the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Collections;
using Client;
namespace Client
{
   
public interface ISecrutary
    {
       
void Service();
    } 
   
public class Minister : ISecrutary
    {
       
public void Service()
        {
           
Console.WriteLine("Service by Minister");
        }
    } 
   
public class CommonPerson
    {
        
private ISecrutary _service; 
        
public CommonPerson(ISecrutary service)
         {
           
this._service = service;
         }
        
public void Start()
         {
            
this._service.Service();
         }
    }
   
class Program
    {
       
static void Main(string[] args)
        {
           
CommonPerson c = new CommonPerson(new Minister());
            c.Start();
           
Console.ReadLine();
        }   
    }
}

The output is very simple. It's like the following.

constructor Injection.gif

You can see that when an object of the CommonPerson class calls the minister class, it uses the ISectuary interface, now even if we change something in the Minister Class, it will not effect the CommonPerson class.

Conclusion

This article has attempted to explain how to implement Dependency Injection in a C# application. Hope you have understood the concept. In a future article we will explore various patterns of Dependency Injection.


Similar Articles