Dependency Injection in C#

What is Dependency Injection?

Dependency injection is a design pattern used in software engineering where the dependencies of an object are injected or provided to it from an external source rather than being created or managed by the object itself.

In other words, instead of an object creating its own dependencies or relying on a global state, those dependencies are provided to the object from an external source, typically through a constructor, setter method, or interface. This allows for more modular, testable, and maintainable code, as objects can be easily swapped out or tested in isolation.

Dependency injection is commonly used in object-oriented programming and is often implemented using a framework or container that manages the injection of dependencies.

Dependency Injection in C#

In C#, Dependency Injection is a technique used to achieve loose coupling between classes and their dependencies. It is a design pattern that allows for the separation of concerns in an application, making it more maintainable, testable, and flexible.

In Dependency Injection, dependencies are injected into a class from the outside rather than being created or managed by the class itself. This is typically achieved using constructor injection, where the dependencies are passed into the constructor of the class or using property or method injection.

Several frameworks are available in C# for implementing Dependency Injection, such as Microsoft's built-in Dependency Injection framework, Autofac, Ninject, and Unity. These frameworks provide a way to configure and manage the injection of dependencies, making it easier to write modular and extensible code.

Types of Dependency Injection in C#

There are three types of dependency injections in C#. 

  • Constructor Injection
  • Property Injection
  • Method Injection 

Constructor Injection

Constructor Injection is the most common type of dependency injection, where dependencies are injected into a class through its constructor. This allows for the dependencies to be set at the time of object creation, making the dependencies clear and ensuring that the class is always in a valid state.

Constructor Injection Example:

using System;  
   
namespace ConsoleApp1  
{  
   
public interface IService  
{  
   void Print();  
}  
   
public class Service1 : IService  
{  
   public void Print()  
   {  
      Console.WriteLine("Print Method 1");  
   }  
}  
   
public class Service2 : IService  
{  
   public void Print()  
   {  
      Console.WriteLine("Print Method 2");  
   }  
}  
   
class ClassA  
{  
   private IService _service;  
   public ClassA(IService service)  
   {  
      this._service = service;  
   }  
   public void PrintMethod()  
   {  
       this._service.Print();  
   }  
}  
   
class Program  
{  
   static void Main(string[] args)  
   {  
      Service1 s1 = new Service1();  
      //passing dependency  
      ClassA c1 = new ClassA(s1);  
      c1.PrintMethod();  
      Service2 s2 = new Service2();  
      //passing dependency  
      c1 = new ClassA(s2);  
      c1.PrintMethod();  
      Console.ReadLine();  
   }  
}  
}  
 **********End**************

Property Injection

Property Injection type of dependency injection, dependencies are injected into a class through its properties. This can be useful when a class has a large number of dependencies or when some dependencies are optional. However, it can also lead to problems if the dependencies are not set, as the class may not be in a valid state.

Dependency Injection is done through public property. From the above example, it is required to modify the two classes.

Property Injection Example:

class ClassA  
{  
   private IService _service;  
   public IService Service  
   {  
      set  
       {  
          this._service = value;  
       }  
   }  
   public void PrintMethod()  
   {  
      this._service.Print();  
    }  
}  
   
class Program  
{  
   static void Main(string[] args)  
   {  
      Service1 s1 = new Service1();  
      ClassA c1 = new ClassA();  
      //passing dependency  
      c1.Service = s1;  
      c1.PrintMethod();  
      Service2 s2 = new Service2();  
      //passing dependency  
      c1.Service = s2;  
      c1.PrintMethod();  
      Console.ReadLine();  
   }  
}  
 **********End**************   

Method Injection

Method Injection type of dependency injection involves injecting dependencies into a class through its methods. This can be useful when a method requires a specific dependency or when some dependencies are only required for a specific operation. However, it can also lead to problems if the dependencies are not set, as the method may not function correctly.

Property Injection Example:

class ClassA    
{    
    
    private IService _service;  
    public void PrintMethod(IService service)  
    {  
       _service = service;  
        this._service.Print();  
    }  
}    
     
class Program    
{    
   static void Main(string[] args)    
   {    
      Service1 s1 = new Service1();    
      ClassA c1 = new ClassA();    
      //passing dependency    
      c1.PrintMethod(s1);    
      Service2 s2 = new Service2();    
      //passing dependency    
      c1.PrintMethod(s2);    
      Console.ReadLine();    
   }    
}

Many Dependency Injection containers are available in the market which implement the dependency injection design pattern. A few of them are:

  • Structure Map.
  • Unity 
  • Spring.Net
  • Castle Windsor

Happy Learning...