Dependency Injection in C#

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. (DI reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically.
We have the following ways to implement Dependency Injection.
Constructor Injection

This 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.     }  
  14.     class format : text  
  15.     {  
  16.         public void print()  
  17.         {  
  18.   
  19.             Console.WriteLine(" here is text format");  
  20.           
  21.         }  
  22.       
  23.     }  
  24.     // constructor injection  
  25.     public class constructorinjection  
  26.     {  
  27.   
  28.         private text _text;  
  29.         public constructorinjection(text t1)  
  30.         {  
  31.             this._text = t1;  
  32.           
  33.         }  
  34.         public void print()  
  35.         {  
  36.   
  37.             _text.print();  
  38.         }  
  39.   
  40.   
  41.   
  42.   
  43.       
  44.     }  
  45.     class constructor  
  46.     {  
  47.   
  48.         static void Main(string[] args)  
  49.         {  
  50.   
  51.             constructorinjection cs = new constructorinjection(new format());  
  52.             cs.print();  
  53.             Console.ReadKey();  
  54.           
  55.         }  
  56.     }  
  57. }  


By passing the services that implemented the text interface the builder assembled the dependencies.

Property Injection

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.       
  4.    void ActOnNotification(string message);  

  5. }  
  6.    class atul  
  7.    {  
  8.        INofificationAction task = null;  
  9.        public void notify(INofificationAction  at ,string messages)  
  10.        {  
  11.   
  12.        this.task = at;  
  13.        task.ActOnNotification(messages);  
  14.      
  15.      
  16.        }  
  17.      
  18.    }  
  19.    class EventLogWriter : INofificationAction  
  20.    {  
  21.        public void ActOnNotification(string message)  
  22.        {  
  23.            // Write to event log here  
  24.        }  
  25.    }  
  26.    class Program  
  27.    {  
  28.        static void Main(string[] args)  
  29.        {  
  30.            //services srv = new services();  
  31.            //other oth = new other();  
  32.            //oth.run();  
  33.            //Console.WriteLine();  
  34.            EventLogWriter elw = new EventLogWriter();  
  35.            atul at = new atul();  
  36.            at.notify(elw, "to logg");  
  37.            Console.ReadKey();  
  38.        }  
  39.    }  
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 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.   
  10.     public interface Iset  
  11.     {  
  12.         void print();  
  13.       
  14.     }  
  15.     public class servic : Iset  
  16.     {  
  17.         public void print()  
  18.         {  
  19.             Console.WriteLine("print........");  
  20.           
  21.         }  
  22.       
  23.     }  
  24.     public class client  
  25.     {  
  26.         private Iset _set;  
  27.         public void run(Iset serv)  
  28.         {  
  29.   
  30.             this._set = serv;  
  31.             Console.WriteLine("start");  
  32.             this._set.print();  
  33.         }  
  34.       
  35.     }  
  36.     class method  
  37.     {  
  38.         public static void Main()  
  39.         {  
  40.             client cn = new client();  
  41.             cn.run(new servic());  
  42.             Console.ReadKey();  
  43.           
  44.           
  45.         }  
  46.     }  
  47. }  

Summary

This article helps to reduces class coupling, increase code reuse, improve code maintainability, improve application testing and help to easily do unit testing.

Up Next
    Ebook Download
    View all

    FileInfo in C#

    Read by 9.7k people
    Download Now!
    Learn
    View all