Dependency Injection in Minimal APIs (ASP .NET) 🎯

beginner
12 min

Dependency Injection in Minimal APIs (ASP .NET) 🎯

Welcome to our deep dive into Dependency Injection in Minimal APIs using ASP .NET! This tutorial is designed to guide both beginners and intermediates through the concept, explaining why it's essential and how to implement it effectively. 📝

What is Dependency Injection?

Dependency Injection (DI) is a design pattern that helps manage dependencies between classes, making them loosely coupled and easier to test, maintain, and scale. In other words, it's a technique for providing objects with dependencies, rather than having them create those dependencies themselves. 💡

Why Dependency Injection?

  1. Improved testability: With DI, you can easily swap out dependencies during testing, making it easier to isolate and test individual components.
  2. Loose coupling: By using DI, you can minimize the direct connections between classes, which makes your code more flexible and easier to modify.
  3. Reduced boilerplate code: DI helps you avoid writing a lot of repetitive code for instantiating and configuring dependencies.
  4. Improved maintainability: With loose coupling and testability, your codebase becomes easier to maintain, refactor, and extend over time.

Dependency Injection in ASP .NET

ASP .NET provides built-in support for Dependency Injection via the IServiceCollection and IServiceProvider interfaces. Let's explore how to use it in Minimal APIs.

Setting Up Dependency Injection

First, we'll create a basic Minimal API project:

sh
dotnet new webapi -n MyMinimalApi cd MyMinimalApi

Next, let's register our services (dependencies) and create a controller:

  1. In the Program.cs, register the services in the CreateHostBuilder method:
csharp
builder.Services.AddTransient<IMyService, MyService>();

Here, IMyService is the service interface, and MyService is the service implementation. The AddTransient method tells the DI container to create a new instance of MyService each time it's requested.

  1. Create a new folder called Services and add a new class called MyService:
csharp
public interface IMyService { string GetMessage(); } public class MyService : IMyService { public string GetMessage() { return "Hello from MyService!"; } }
  1. Create a new Minimal API controller:
csharp
using Microsoft.AspNetCore.Mvc; using MyMinimalApi.Services; [ApiController] public class MyController : ControllerBase { private readonly IMyService _myService; public MyController(IMyService myService) { _myService = myService; } [HttpGet] public string GetMessage() { return _myService.GetMessage(); } }

In the constructor of the controller, we inject IMyService using Dependency Injection.

Using Dependency Injection in Minimal APIs

Now that our project is set up, let's run it:

sh
dotnet run

You can now test the API by navigating to http://localhost:5000/MyController.

📝 Note:

In the example above, we used the AddTransient method to register the service. You can also use AddSingleton to create a single instance of the service for the entire application's lifetime and AddScoped to create a new instance per request.

Quiz 🎯

Quick Quiz
Question 1 of 1

What is Dependency Injection, and why is it important?


We've just scratched the surface of Dependency Injection in ASP .NET Minimal APIs. As you continue to explore and implement this powerful design pattern, you'll find it invaluable in creating scalable, testable, and maintainable codebases. Happy coding! 🚀💻