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:
- Constructor Injection
- Property Injection
- Method Injection
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.
- Client class
- Server class
- Injector 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: –
- It is Tightly coupled whenever we make a change in base class it will affect the child class.
- It can make code less flexible and less reusable.
- It takes more time to maintain a program.
- Inheritance becomes deeper and more complex. It’s harder to understand and modify.
Dependency Injection: –
- It is Loosely coupled we make any changes in one class it won’t affect another class.
- It makes easier to add new functionality or modify existing functionality in our program.
- Removing dependencies can easily swap them out or add newly create dependencies.
- Reduce the number of bugs and make our code more reliable.
Advantages:
- Decreased coupling between classes and their dependencies is a fundamental advantage of dependency injection.
- Programs become more reusable, testable, and maintainable by removing a client’s knowledge of how its dependencies are implemented.
- Additionally, it leads to more adaptability because a client can operate on anything that supports the expected interface.
Conclusion:
- Dependency injection is a powerful technique for handling class interactions in any application. The method is seemingly everywhere and is essential in several frameworks and domains.
- Objects and services in dependency injection are easier to create, maintain, and flexible to changes.
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.
No Comment! Be the first one.