This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Sunday, February 18, 2024

what is dependancey injection in asp.net core with example

 what is dependancey injection in asp.net core with example


Dependency injection (DI) in ASP.NET Core is a design pattern and technique used to create loosely coupled components within an application. In DI, instead of a class creating its own dependencies directly, those dependencies are provided (injected) from outside the class. This promotes better separation of concerns and makes the application easier to maintain, test, and extend.


In ASP.NET Core, the built-in DI container manages the injection of dependencies. Developers register their services (dependencies) with the DI container, and ASP.NET Core takes care of providing those dependencies to the classes that need them.


Here's a basic overview of how dependency injection works in ASP.NET Core:


Service Registration: You register your application's services with the built-in DI container during application startup. This is typically done in the ConfigureServices method of the Startup class.


Dependency Injection: When a component (such as a controller or service) requires a dependency, it declares that dependency as a parameter in its constructor or method. The DI container automatically resolves and injects the appropriate dependency when creating an instance of that component.


Lifetime Management: ASP.NET Core supports different lifetimes for registered services, such as singleton, scoped, and transient. This allows you to control how instances of services are created and managed throughout the application's lifecycle.


Testing: Dependency injection makes unit testing easier because dependencies can be easily mocked or replaced with test doubles. This facilitates writing tests that focus on the behavior of individual components without worrying about their dependencies.


Here's a simple example of dependency injection in an ASP.NET Core application:


csharp

public interface IMyService

{

    void DoSomething();

}


public class MyService : IMyService

{

    public void DoSomething()

    {

        Console.WriteLine("Doing something...");

    }

}


public class MyController : ControllerBase

{

    private readonly IMyService _myService;


    public MyController(IMyService myService)

    {

        _myService = myService;

    }


    public IActionResult MyAction()

    {

        _myService.DoSomething();

        return Ok();

    }

}


public class Startup

{

    public void ConfigureServices(IServiceCollection services)

    {

        services.AddTransient<IMyService, MyService>();

    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

    {

        // Configuration for middleware, routing, etc.

    }

}

In this example, MyController depends on IMyService, but it doesn't create an instance of MyService directly. Instead, the IMyService instance is provided to MyController through constructor injection, and ASP.NET Core's DI container resolves this dependency based on the service registration done in the Startup class.

No comments:

Post a Comment