Dependency Injection (DI) In C#

Introduction

 
Dependency Injection (DI) is a software design pattern that allows us to develop loosely-coupled code. The purpose of DI is to make code maintainable and easy to update.
 
Loosely coupling means that two objects are independent. If we change one object then it will not affect the other. 
 
Types of Dependency Injection Design Pattern in C#,
  1. Constructor Injection
  2. Property Injection
  3. Method Injection 

Constructor Injection

 
Dependency Injection is done through the class’s constructor when creating the instance of that class. 
 
It is used frequently. 
 
Example
  1. using System;  
  2.    
  3. namespace ConsoleApp1  
  4. {  
  5.    
  6. public interface IService  
  7. {  
  8.    void Print();  
  9. }  
  10.    
  11. public class Service1 : IService  
  12. {  
  13.    public void Print()  
  14.    {  
  15.       Console.WriteLine("Print Method 1");  
  16.    }  
  17. }  
  18.    
  19. public class Service2 : IService  
  20. {  
  21.    public void Print()  
  22.    {  
  23.       Console.WriteLine("Print Method 2");  
  24.    }  
  25. }  
  26.    
  27. class ClassA  
  28. {  
  29.    private IService _service;  
  30.    public ClassA(IService service)  
  31.    {  
  32.       this._service = service;  
  33.    }  
  34.    public void PrintMethod()  
  35.    {  
  36.        this._service.Print();  
  37.    }  
  38. }  
  39.    
  40. class Program  
  41. {  
  42.    static void Main(string[] args)  
  43.    {  
  44.       Service1 s1 = new Service1();  
  45.       //passing dependency  
  46.       ClassA c1 = new ClassA(s1);  
  47.       c1.PrintMethod();  
  48.       Service2 s2 = new Service2();  
  49.       //passing dependency  
  50.       c1 = new ClassA(s2);  
  51.       c1.PrintMethod();  
  52.       Console.ReadLine();  
  53.    }  
  54. }  
  55. }  
  56.  **********End**************   

Property Injection

Dependency Injection is done through the public property.
 
From the above example, it is required to modify the below two classes.
 
Example
  1. class ClassA  
  2. {  
  3.    private IService _service;  
  4.    public IService Service  
  5.    {  
  6.       set  
  7.        {  
  8.           this._service = value;  
  9.        }  
  10.    }  
  11.    public void PrintMethod()  
  12.    {  
  13.       this._service.Print();  
  14.     }  
  15. }  
  16.    
  17. class Program  
  18. {  
  19.    static void Main(string[] args)  
  20.    {  
  21.       Service1 s1 = new Service1();  
  22.       ClassA c1 = new ClassA();  
  23.       //passing dependency  
  24.       c1.Service = s1;  
  25.       c1.PrintMethod();  
  26.       Service2 s2 = new Service2();  
  27.       //passing dependency  
  28.       c1.Service = s2;  
  29.       c1.PrintMethod();  
  30.       Console.ReadLine();  
  31.    }  
  32. }  
  33.  **********End**************   

Method Injection

 
It injects the dependency into a single method to be utilized by that method.
 
It could be useful where the whole class does not need dependency, only one method having that dependency. This is why it is used rarely.
Modify the two classes again. 
 
Example
  1. class ClassA    
  2. {    
  3.     
  4.     private IService _service;  
  5.     public void PrintMethod(IService service)  
  6.     {  
  7.        _service = service;  
  8.         this._service.Print();  
  9.     }  
  10. }    
  11.      
  12. class Program    
  13. {    
  14.    static void Main(string[] args)    
  15.    {    
  16.       Service1 s1 = new Service1();    
  17.       ClassA c1 = new ClassA();    
  18.       //passing dependency    
  19.       c1.PrintMethod(s1);    
  20.       Service2 s2 = new Service2();    
  21.       //passing dependency    
  22.       c1.PrintMethod(s2);    
  23.       Console.ReadLine();    
  24.    }    
  25. }    
Many Dependency Injection containers are available in the market which implements the dependency injection design pattern.Few of them are,
  1. Structure Map.
  2. Unity 
  3. Spring.Net
  4. Castle Windsor
Happy Learning...