Understanding of the Dependency Injection and different types of Dependency Injection in .Net.
Hello friend today I’m trying to explain what is Dependency Injection and type of Dependency Injection. Most of the interviewer asked this question during the interview. Don’t worry today I’m explaining to you in the easiest and simplest way.
Definition of Dependency Injection.
Dependency Injection (DI) is a technique that provides a dependent class object. This technique works outside of the Client class and provides the object of the Service class.
Dependency Injection is a relation of mainly 3 types of classes.
- Client Class: This class is a dependent class. Which is depends on the service class.
- Service Class: Service class is a dependency class that provides service to the client class.
- Injector Class: The injector class injects the service class and provides an object to the client class.
So we can say the client class uses service class, where the injector class which helps to client class to create the object of the service
class. This is the simple mechanism of DI.
- Constructor Injection: Inject dependency at the constructor level.
- Property Injection: Inject dependency using setter.
- Method Injection: Inject dependency using method.
1) Example of
Constructor Injection
public interface IService
{
string Mthod();
}
public class Service1 : IService
{
public string Mthod()
{
return "Mthod of service 1.";
}
}
public class Service2 : IService
{
public string Mthod()
{
return "Mthod of service 2.";
}
}
public class Client
{
private readonly IService _iService;
public Client(IService iService)
{
this._iService = iService;
}
public string GetMethod()
{
return _iService.Mthod();
}
}
class Program
{
static void Main(string[] args)
{
//Service 1
Service1 s1 = new Service1();
Client c1 = new Client(s1);
//Service 2
Service2 s2 = new Service2();
c1 = new Client(s2);
}
}
In the above example, you can see Client class is not responsible to create a service class object. We can’t use the new keyword to create an object of class Service1 and Service2 into the Client class. Instead of that, we are passing the object parameter of Service1 and Service2 class when calling client class.
2) Example of Property Injection
public interface IService
{
string Mthod();
}
public class Service1 : IService
{
public string Mthod()
{
return "Mthod of service 1.";
}
}
public class Service2 : IService
{
public string Mthod()
{
return "Mthod of service 2.";
}
}
public class Client
{
private IService _iService;
public IService Service
{
set
{
this._iService = value;
}
}
public string GetMethod()
{
return _iService.Mthod();
}
}
class Program
{
static void Main(string[] args)
{
//Service 1
Service1 s1 = new Service1();
Client c1 = new Client();
c1.Service = s1;
//Service 2
Service2 s2 = new Service2();
c1.Service = s2;
}
}
In the above example, you can see that we are injecting the Service class object using setter property when calling client class. We set the Service1 and Service2 class object during the calling of Client class so client class is not responsible to create the object of Service class. So this way we achieve the property injection.
3) Example of Method Injection
public interface IService
{
string Mthod();
}
public class Service1 : IService
{
public string Mthod()
{
return "Mthod of service 1.";
}
}
public class Service2 : IService
{
public string Mthod()
{
return "Mthod of service 2.";
}
}
public class Client
{
private IService _iService;
public void Serve(IService iService)
{
_iService = iService;
}
public string GetMethod()
{
return _iService.Mthod();
}
}
class Program
{
static void Main(string[] args)
{
//Service 1
Service1 s1 = new Service1();
Client c1 = new Client();
c1.Serve(s1);
var result = c1.GetMethod();
//Service 2
Service2 s2 = new Service2();
c1.Serve(s2);
var result2 = c1.GetMethod();
}
}
As you have seen in the above example here we are injecting the Service class object through the Serve method of Client class. So this way we achieve the Method Injection.
Advantage of Dependency Injection
Achieve loose coupling.
Code reusability increases.
Easy to unit testing
Improve code management due to separation of class and less responsibility.
- Unity
- Autofac
- Ninject
- Simple Injector
- DryIoc
- Light Inject
- Castle Windsor
- StuructureMap
I hope you have liked this article for the understanding of Dependency Injection and types of dependency injection.
Please feel free to comment if you have found anything incorrect, or you want to share more information about the topic discussed above.
Please Share this article so it will help to another one because “Helping millions grow better”.
Follow my blog https://mydotnetguru.blogspot.com to read the more interesting technical articles.
Next Recommended Article:
- Optimized top 2 ways to find 2nd or Nth highest salary in SQL Server.
- Different ways to remove or delete duplicate rows from a SQL Table.
- When to use abstract class and interface in C#?
- Async and Await in C#
- Top 100+ popular ASP.Net MVC Interview Questions and Answers.
- Top 100+ popular SQL Interview Questions and Answers Blog.
- Top 100+ popular C# Interview Questions and Answers.
- @Input, @Output decorator and EventEmitter class in Angular.
No comments:
Post a Comment