Dependency Injection is a design pattern which allows us to develop Loosely Coupled software components.  It enables us to make the better management and reduce the complexity of the software in future. Its main purpose is to make code reusability and maintainable.

It has three types: 

Tightly Coupled in software design: 

Tightly Coupled means two objects are dependent on each other. For example, take two classes and need to change any one of the classes. It will affect the class another class. 

It is not difficult to handle a change in small programs. But it is difficult to handle a change in big level program like organization. 

Loosely Coupled in software design: 

Loosely Coupled means two objects are independent of each other. Let’s assume that we have two classes like classA and classB.  

If we need to change an object to any one of the classes, it will not affect another class. So, it allows us to manage the complexity of the applications. 

Dependency injection involves three types of class.

Service class – is a dependency which means the class provides the actual method or properties to the client class. 

Client class – is a dependent class which means the class wants to access the method or properties from Service class(dependency). 

Injector class – is a class that injects the Service class object to Client class.

Types of Dependency Injection: 

The Injector class injects the object Service class to Client class. This injects dependencies through three ways such as Constructor, Properties, Method. 

Let’s see what Constructor Dependency Injection is and how it works, 

public interface IService 
{ 
   void Serve(); 
} 
public class Service1 : IService 
{ 
    public void Serve() 
      { 
        Console.WriteLine("Service1 Called"); 
      } 
} 
public class Service2 : IService 
{ 
    public void Serve() 
      { 
        Console.WriteLine("Service2 Called"); 
      } 
} 
public class Client 
{ 
    private IService _service; 
    public Client(IService service) 
    { 
       this._service = service; 
    } 

    public void ServeMethod() 
    { 
       this._service.Serve(); 
    } 
class Program 
{ 
     static void Main(string[] args) 
        { 
            //creating object 
            Service1 s1 = new Service1(); 
            //passing dependency 
            Client c1 = new Client(s1); 
            //TO DO: 
            c1.ServeMethod(); 
            Service2 s2 = new Service2(); 
            //passing dependency 
            c1 = new Client(s2); 
            //TO DO: 
            c1.ServeMethod(); 
         } 
} 
} 

 

Inheritance VS Dependency Injection: 

Inheritance: – 

Dependency Injection: – 

Advantages:

Conclusion:

Here we discussed the constructor injection and its example, and I hope you’ll get a clear understanding in Constructor Injection. 

We will discuss the next two types of injections in upcoming blogs. 

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.