Dependency Injection In C#

In this article, we are going to see what is DI and its type.

What is Dependency Injection?

Dependency Injection is a design pattern. It allows the creation of dependent objects outside of a class and provides those objects to a class in different ways. Using DI, we can move the creation and binding of the dependent objects outside of the class that is dependent on them.

The Dependency Injection(DI) pattern involves 3 types of classes.

  1. Client Class.
  2. Service Class.
  3. Injector Class.

Client Class: Client class is a dependent class that is dependent on the service class.

Service Class: The service class is a dependency class that provides service to the client class.

Injector Class: Injector class that helps to inject the service class object into client class.

Types of Dependency Injection

You can see in the image that the injector injects the service class object into the client class. this can be achieved in 3 different ways.

  • Constructor Injection.
  • Property Injection.
  • Method Injection.

Constructor Injection.

An injector that injects the service (dependency) object to the client class using the constructor is known as Constructor Injection.

using System;
namespace propertyinjuction  
{  
    public interface IStudent  
    {
        void StudentName();
    }
    class StudentServices : IStudent
    {
        public void StudentName()
        {
            Console.WriteLine("My Name Is Hafeezjaha");
        }      
    }
    public class constructorInjectionDemo
    {  
        private IStudent _iStudent;
        public constructorInjectionDemo(IStudent iStudent)
        {
            this._iStudent = iStudent;          
        }
        public void StudentName()
        {  
            _iStudent.StudentName();
        }
    }
    class Test
    {  
        static void Main(string[] args)
        {  
            constructorInjectionDemo cs = new constructorInjectionDemo(new StudentServices());
            cs.StudentName();
        }
    }
}
Output
My Name Is Hafeezjaha

Property Injection

It’s also known as Setter Injection.

An injector that injects the service (dependency) object to the client class using the public property then is known as Property Injection.

public class StudentBL
{
        private IStudentDAL studentDAL;
        public IStudentDAL studentDataObject
        {
            set
            {
                this.studentDAL = value;
            }
            get
            {
                if (studentDataObject == null)
                {
                    throw new Exception("Employee is not initialized");
                }
                else
                {
                    return studentDAL;
                }
            }
        }
        public List<Students> GetAllStudents()
        {
            return studentDAL.SelectAllStudents();
        }
}
 class Program
 {
        static void Main(string[] args)
        {
            StudentBL studentBL = new StudentBL();
            studentBL.studentDataObject = new IStudentDAL();
            
            List<Students> ListStudent = studentBL.GetAllStudents();
            foreach(Students stud in ListStudent)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Department = {2}", stud.ID, stud.Name, stud.Marks);
            }
        }
 }

Method Injection

An injector that injects the service (dependency) object to the client class using the public method then is known as Property Injection.

using System;
namespace propertyinjuction
{  
  public interface IStudent 
  { 
     void StudentName();
  }
  class StudentServices : IStudent 
  { 
    public void StudentName() 
    { 
      Console.WriteLine("My Name Is Hafeezjaha"); 
    } 
  }
 public class constructorInjectionDemo 
 { 
    private IStudent _iStudent; 
    public void StudentName(IStudent Student) 
    { 
        this._iStudent = Student;
        this._iStudent.StudentName(); 
    } 
 }
 class Test 
 { 
    public static void Main(string[] args) 
    { 
      constructorInjectionDemo cs = new constructorInjectionDemo(); 
      cs.StudentName(new StudentServices());
    } 
 }
}
Output
My Name Is Hafeezjaha

That’s it.

Also check, Introduction Of OOP Concept In C#

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories