GoogleTag

Google Search

How does dependency injection work in .NET Core?

 Dependency Injection (DI) is a design pattern in which an object receives its dependencies from external sources rather than creating them itself. This pattern promotes loose coupling between components and increases testability.

In .NET Core, DI is built-in, making it easy to implement. It allows you to register services (interfaces and their implementations) with a built-in IoC (Inversion of Control) container, and the container automatically injects the services where needed.

How Dependency Injection Works in .NET Core

  1. Service Registration: You register the services (interfaces and their implementations) in the Startup.cs file in the ConfigureServices method.

  2. Service Resolution: The framework resolves dependencies and injects them into constructors, methods, or properties where needed.

  3. Service Lifetime: .NET Core provides three main lifetimes for services:

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

    • Scoped: A new instance is created per request (or per scope).

    • Singleton: A single instance is created and shared throughout the application lifetime.

Example of Setting Up Dependency Injection in a .NET Core Application

Here’s a simple example demonstrating how to set up DI in a .NET Core application.

1. Define a Service Interface and Its Implementation


// Services/IMyService.cs

public interface IMyService

{

string GetMessage();

}


// Services/MyService.cs

public class MyService : IMyService

{

public string GetMessage()

{

     return "Hello from MyService!";

}

}


2. Register the Service in Startup.cs

In .NET Core, you register services in the ConfigureServices method inside Startup.cs


// Startup.cs

public class Startup

{

public void ConfigureServices(IServiceCollection services)

{

     // Register the service with a transient lifetime

     services.AddTransient<IMyService, MyService>();

     

     // Other services like MVC can be added here as well

     services.AddControllersWithViews();

}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

     // Usual configuration for middleware

     if (env.IsDevelopment())

     {

         app.UseDeveloperExceptionPage();

     }

     else

     {

         app.UseExceptionHandler("/Home/Error");

         app.UseHsts();

     }


     app.UseHttpsRedirection();

     app.UseStaticFiles();

     app.UseRouting();

     app.UseAuthorization();


     app.UseEndpoints(endpoints =>

     {

         endpoints.MapControllerRoute(

             name: "default",

             pattern: "{controller=Home}/{action=Index}/{id?}");

     });

}

}



3. Inject the Service in a Controller

Once the service is registered, you can inject it into a controller or any other class by simply adding it to the constructor.


// Controllers/HomeController.cs

public class HomeController : Controller

{

private readonly IMyService _myService;


// Constructor injection

public HomeController(IMyService myService)

{

     _myService = myService;

}


public IActionResult Index()

{

     // Use the service

     var message = _myService.GetMessage();

     ViewBag.Message = message;

     return View();

}

}


4. Use the Service in the View


<!-- Views/Home/Index.cshtml -->

@{

ViewBag.Title = "Home Page";

}


<h2>@ViewBag.Message</h2>


Service Lifetimes in DI

  • Transient (AddTransient<TService, TImplementation>()): A new instance of the service is created each time it is requested.

  • Scoped (AddScoped<TService, TImplementation>()): A new instance is created once per request.

  • Singleton (AddSingleton<TService, TImplementation>()): A single instance is shared across the entire application.

Benefits of Using DI

  • Loose Coupling: Classes are decoupled from the concrete implementations of their dependencies.

  • Testability: Dependencies can be mocked or replaced for testing.

  • Maintainability: Centralized service configuration makes it easier to manage dependencies.


(image taken from wiki)

Featured Posts

SQL Interview Questions Topics

 SQL Topics to prepare for interviews,   SQL Basics: Introduction to SQL SQL Data Types DDL (Data Definition Language): C...

Popular Posts