Monday, 7 August 2023

Dependency Injection in Dot net core. how it works in dot net core?

Dependency Injection (DI) is a design pattern used in software development to manage the dependencies between different components or classes of an application. It allows the application to be loosely coupled, making it easier to maintain, test, and extend. In the context of .NET Core, DI is an integral part of the framework and is used to manage the dependencies of classes within the application.


In DI, instead of having classes create their dependencies directly, the dependencies are provided to the class from the outside, typically through constructor parameters or properties. This way, the class doesn't need to worry about how to create its dependencies, and it can focus on its primary functionality.


How Dependency Injection works in .NET Core:

Registration: In .NET Core, you need to register the types and their associated dependencies with the DI container. The DI container is a component that keeps track of registered services and provides them when requested. Registration typically occurs during application startup.


Service Lifetimes: 

When registering a type with the DI container, you can specify the lifetime of the service. There are three common lifetimes in .NET Core:

Transient: A new instance is created every time the service is requested.

Scoped: A single instance is created per scope. In the case of web applications, a scope typically corresponds to a single HTTP request.

Singleton: A single instance is created for the entire lifetime of the application.

Dependency Resolution: When a class requires a dependency, it specifies that dependency through its constructor or properties. The DI container is responsible for providing the correct instance of the dependency based on the registered services and their lifetimes.


Here's an example of how to use Dependency Injection in .NET Core:

Suppose you have an interface IMyService and its implementation MyService.


public interface IMyService
{
    void DoSomething();
}

public class MyService : IMyService
{
    public void DoSomething()
    {
        Console.WriteLine("Doing something!");
    }
}

In your application's Startup class, you register the service:


public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Register the service with the DI container
        services.AddTransient(IMyService, MyService);
    }
}

Now, in your controller or other classes that need the IMyService, you can request it through constructor injection:


public class MyController : Controller
{
    private readonly IMyService _myService;

    public MyController(IMyService myService)
    {
        _myService = myService;
    }

    public IActionResult DoSomething()
    {
        _myService.DoSomething();
        return View();
    }
}

With this setup, .NET Core's DI container will automatically provide the MyService instance to the MyController class whenever it's needed.

By using Dependency Injection, your code becomes more modular, testable, and easier to maintain, making it a best practice in modern software development, especially with .NET Core.

No comments:

Post a Comment