Dependency Injection (Constructor Injection) In C#

Before starting to discuss Constructor Injection, let’s discuss Dependency Injection. What exactly it is? What is the use of it?
 
Dependency Injection is a Software Design Pattern (Answer of What). It allows developing loosely coupled code (Answer of Why). Now a days most of the enterprise level application is on Microservices architecture, in this type of architecture we treat each component separately. It means that we are not developing code tightly coupled. The purpose of DI is to make code maintainable and easy to update.
 
In addition, an object’s dependencies should be on interfaces instead of concrete objects. So, what is concrete object? An object created with "new" keyword is a concrete object. Through loose coupling, you are able to achieve better maintainability and greater reusability.
 
We can implement Dependency Injection in 3 ways. These are also called types of Dependency Injection.
  • Constructor Injection
  • Property/Setter Injection
  • Method Injection
In this article, we are going to discuss Constructor Injection.
 
Advantages
  • This is widely used to implement Dependency Injection
  • This is done by supplying the dependency through the class at the time of creating the instance/object of class
  • There can be the case when we need to use multiple dependency, at that time Constructor Injection solve this most common scenario
  • The component which is injected in the class get access within the class and can get used within the class
Check the Below code,
  1. public interface IFacility  
  2. {  
  3.     void DoSomething();  
  4. }  
  5. public class Transport : IFacility  
  6. {  
  7.     public void DoSomething() { Console.WriteLine("Transport Service Called"); }  
  8. }  
  9. public class Utility : IFacility  
  10. {  
  11.     public void DoSomething() { Console.WriteLine("Utility Service Called"); }  
  12. }  
  13. public class Facility  
  14. {  
  15.     private IFacility _service;  
  16.     public Facility(IFacility service)  
  17.     {  
  18.         this._service = service;  
  19.     }  
  20.     public ShowOutputMethod() { this._service.DoSomething(); }  
  21. }  
Here we have inherited the same interface with different implementation in different classes. Now we will try to call this with the help of Constructor Injection
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         //creating object  
  6.         Transport objT = new Transport();  
  7.         //passing dependency  
  8.         Facility c1 = new Facility(objT);  
  9.           
  10.         Utility objU = new Utility();  
  11.         //passing dependency  
  12.         c1 = new Facility(objU);  
  13.           
  14.     }  
  15. }  
This is the overall implementation of the Constructor injection. Let’s check multiple dependency in a single constructor
  1. public interface IEmail  
  2. {  
  3.     void SendEmail();  
  4. }  
  5. public class EmailCommunication : IEmail  
  6. {  
  7.     public void SendEmail() { Console.WriteLine("Email Sent Successfully"); }  
  8. }  
Now let’s rewrite the Facility class
  1. public class Facility  
  2. {  
  3.     private IFacility _service;  
  4.     private IEmail _emailService;  
  5.     public Facility(IFacility service)  
  6.     {  
  7.         this._service = service;       
  8.   
  9.     }  
  10.       
  11.     public Facility(IFacility service, IEmail emailService)  
  12.     {  
  13.         this._service = service;  
  14.      this._emailService = emailService;  
  15.   
  16.     }  
  17.   
  18.     public ShowOutputMethod() { this._service.DoSomething(); }  
  19. public SendEmailMethod() { this._emailService.SendEmail(); }  
  20.   
  21. }  
And use it in Main Program
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         //creating object  
  6.         Transport objT = new Transport();  
  7.  EmailCommunication objEmail = new EmailCommunication();  
  8.         //passing dependency  
  9.         Facility c1 = new Facility(objT, objEmail);  
  10.           
  11.         Utility objU = new Utility();  
  12.         //passing dependency  
  13.         c1 = new Facility(objU);  
  14.           
  15.     }  
  16. }  
This way we can use same class with single or multiple dependencies.
 
The Injection happens in the constructor, bypassing the Facility that implements the IFacility Interface. The dependencies are assembled according to the request, feed the abstract IFacility to the Client.
 
--Happy Coding--


Similar Articles
Blue Ocktopus Technology Systems Private Limited
Enabling revenue generation through brand loyalty and customer retention for enterprise retail and e