Dependency Injection in C#

Dependency Injection (DI) is a software design pattern. It allows us to develop loosely-coupled code. The intent of Dependency Injection is to make code maintainable. Dependency Injection helps to reduce the tight coupling among software components. Dependency Injection reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically. This article explains how to implement Dependency Injection in C# and .NET code.
 
Dependency Injection CSharp 
 
We have the following ways to implement Dependency Injection. 

Constructor Injection in C#

Construction injection is the most commonly used dependency pattern in Object Oriented Programming. The constructor injection normally has only one parameterized constructor, so in this constructor dependency there is no default constructor and we need to pass the specified value at the time of object creation. We can use the injection component anywhere within the class. It addresses the most common scenario where a class requires one or more dependencies.
 
The following is an example:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace propertyinjuction  
  8. {  
  9.     public interface text  
  10.     {
  11.         void print();
  12.     }
  13.     class format : text
  14.     {
  15.         public void print()
  16.         {
  17.             Console.WriteLine(" here is text format");
  18.         }      
  19.     }
  20.     // constructor injection
  21.     public class constructorinjection
  22.     {  
  23.         private text _text;
  24.         public constructorinjection(text t1)
  25.         {
  26.             this._text = t1;          
  27.         }
  28.         public void print()
  29.         {  
  30.             _text.print();
  31.         }
  32.     }
  33.     class constructor
  34.     {  
  35.         static void Main(string[] args)
  36.         {  
  37.             constructorinjection cs = new constructorinjection(new format());
  38.             cs.print();
  39.             Console.ReadKey();          
  40.         }
  41.     }
  42. }
Dependency Injection in C#
 
By passing the services that implemented the text interface the builder assembled the dependencies.
 

Property Injection in C#

 
We use constructor injection, but there are some cases where I need a parameter-less constructor so we need to use property injection.
 
The following is an example:
  1. public interface INofificationAction
  2. {      
  3.    void ActOnNotification(string message);
  4. }
  5.    class atul     {  
  6.        INofificationAction task = null;
  7.        public void notify(INofificationAction  at ,string messages)
  8.        {  
  9.        this.task = at;
  10.        task.ActOnNotification(messages);    
  11.        }     
  12.    }
  13.    class EventLogWriter : INofificationAction
  14.    {
  15.        public void ActOnNotification(string message)
  16.        {
  17.            // Write to event log here
  18.        }
  19.    }
  20.    class Program
  21.    {
  22.        static void Main(string[] args)
  23.        {
  24.            //services srv = new services();
  25.            //other oth = new other();
  26.            //oth.run();
  27.            //Console.WriteLine();
  28.            EventLogWriter elw = new EventLogWriter();
  29.            atul at = new atul();
  30.            at.notify(elw, "to logg");
  31.            Console.ReadKey();
  32.        }
  33.    }
You cannot control when the dependency is set at all, it can be changed at any point in the object's lifetime. 
 

Method Injection in C#

 
In method injection we need to pass the dependency in the method only. The entire class does not need the dependency, just the one method. I have a class with a method that has a dependency. I do not want to use constructor injection because then I would be creating the dependent object every time this class is instantiated and most of the methods do not need this dependent object.
 
The following is an example:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.   
  7. namespace propertyinjuction
  8. {  
  9.     public interface Iset
  10.     {
  11.         void print();      
  12.     }
  13.     public class servic : Iset
  14.     {
  15.         public void print()
  16.         {  
  17.             Console.WriteLine("print........");          
  18.         }      
  19.     }
  20.     public class client
  21.     {
  22.         private Iset _set;
  23.         public void run(Iset serv)
  24.         {  
  25.             this._set = serv;
  26.             Console.WriteLine("start");
  27.             this._set.print();
  28.         }      
  29.     }
  30.     class method
  31.     {
  32.         public static void Main()
  33.         {
  34.             client cn = new client();
  35.             cn.run(new servic());
  36.             Console.ReadKey();         
  37.         }
  38.     }
  39. }
Dependency Injection in C#
 

Summary

 
This article helps to reduces class coupling, increase code reuse, improve code maintainability, improve application testing and help to easily do unit testing. In this article with code samples, we saw some use cases of dependency injection using C# and .NET.