Dependency Injection (DI)
Mohd Saeed
Technical Lead @ Capital Numbers | Application Developer | .Net Core | C# | Web API | Entity Framework Core | Type Script | JavaScript | SQL | GIT | Docker
Dependency Injection (DI) is a technique that enables objects to receive dependencies without having to create or manage them directly. DI is widely used in .Net Core applications to facilitate better testability, maintainability, and scalability. In this Article, we will discuss Dependency Injection in .Net Core and how to use it effectively in your applications.
What is Dependency Injection?
Dependency Injection is a technique that helps to reduce the tight coupling between classes in your application. Instead of a class creating and managing its dependencies, DI allows you to define dependencies separately and inject them into the class. This way, the class can use the dependencies without having to worry about how they are created or maintained.
Why use Dependency Injection in .Net Core?
Dependency Injection has become a crucial aspect of software development in recent years, especially in .Net Core applications. Here are some reasons why you should use DI in your .Net Core applications:
How to use Dependency Injection in .Net Core?
.Net Core provides built-in support for Dependency Injection through the Microsoft.Extensions.Dependency Injection package. Here are the steps to use DI in your .Net Core application:
Step 1: Define the services you want to inject into your classes. You can do this by creating a class that implements the IServiceCollection interface and adding your services to it. For example:
public class MyServices : IServiceCollection
{
???public void ConfigureServices(IServiceCollection services)
???{
???????services.AddTransient<IMyService, MyServiceImpl>();
???}
}
In the above code, we are defining a service called IMyService and implementing it with a class called MyServiceImpl. We are also specifying that the service should be created every time it is requested using the Transient lifetime (Life time we will discuss in next topic).
Step 2: Inject the services into your classes. You can do this by declaring the services you want to inject in the constructor of your class. For example:
public class MyController : Controller
{
???private readonly IMyService _myService;
???public MyController(IMyService myService)
???{
???????_myService = myService;
???}
}
In the above code, we are injecting the IMyService service into our MyController class by declaring it in the constructor. Now, whenever we create an instance of MyController, the IMyService dependency will be automatically injected into it.
Conclusion
Dependency Injection is a powerful technique that can help you write better, more maintainable, and scalable .Net Core applications. By using DI, you can reduce the coupling between classes, making it easier to test, maintain, and extend your code. With the built-in support for DI in .Net Core, it's easy to get started and use DI in your applications.