1. Introduction

Explain what Dependency Injection is in simple terms.

Example:

Imagine a car needs an engine. Instead of the car building its own engine, someone provides the engine when the car is created. ASP.NET Core works similarly by providing required services to your classes.


2. What Problem Does Dependency Injection Solve?

Without DI:

public class UserService
{
    private EmailService _email = new EmailService();
}

Problems:


3. Dependency Injection with ASP.NET Core

builder.Services.AddScoped<IEmailService, EmailService>();

Then:

public class UserService
{
    private readonly IEmailService _email;

    public UserService(IEmailService email)
    {
        _email = email;
    }
}

Explain that ASP.NET Core automatically creates and injects the service.


4. Registering Services

Explain the three common registrations.

Singleton

builder.Services.AddSingleton<ISettingsService, SettingsService>();

Scoped

builder.Services.AddScoped<IUserService, UserService>();

Transient

builder.Services.AddTransient<IReportService, ReportService>();

5. Understanding Service Lifetimes

Include a simple comparison table:

LifetimeCreatedTypical Use
SingletonOnce for the applicationConfiguration, cache
ScopedOnce per requestBusiness services, DbContext
TransientEvery injectionUtility/helper services

6. Constructor Injection

Example:

public class ProductController : Controller
{
    private readonly IProductService _service;

    public ProductController(IProductService service)
    {
        _service = service;
    }
}

Explain why constructor injection is preferred over creating objects manually.


7. Dependency Injection in Blazor

Since you work with Blazor, this section will make the article stand out.

@inject NavigationManager Navigation
@inject HttpClient Http

Also show a custom service:

@inject WeatherService WeatherService

8. Dependency Injection in .NET MAUI Blazor

Connect it to your expertise.

Example:

builder.Services.AddSingleton<AppSessionService>();
builder.Services.AddScoped<AuthApiService>();
builder.Services.AddSingleton<ModuleState>();

Explain why:

Why Singleton Can Survive a Page Refresh in Blazor Hybrid

In a .NET MAUI Blazor Hybrid application, the lifetime behavior is different from a traditional ASP.NET Core web request. A Scoped service belongs to a DI scope, and that scope can be recreated when the application/page is refreshed or the relevant Blazor context is rebuilt. When that happens, values stored only inside the scoped service can be lost.

A Singleton service, on the other hand, is created once for the lifetime of the application's DI container. Therefore, in a Blazor Hybrid application, application-level state stored in a singleton can remain available after a page refresh when the same application/container remains alive.

Important: This is not a universal rule that "Singleton always survives refresh and Scoped never does." The exact behavior depends on the hosting model and the DI scope being recreated. In Blazor Hybrid, a singleton can be useful for application-level state, while scoped services should be used when state should belong to a particular DI scope.

For example, if an AppSessionService stores the current company code, plant code, or other application session information in a MAUI Blazor Hybrid app, registering it as a singleton can keep that state available when a page is refreshed, whereas putting the same state in a recreated scoped service can cause it to reset.


9. Common Mistakes

Cover things developers actually encounter:


10. Best Practices

Recommend:


11. Conclusion

Summarize why DI matters:

Dependency Injection is one of the core features of ASP.NET Core. It reduces coupling, improves testability, and makes applications easier to maintain as they grow.


Internal Links

Link this article to:

Related articles

🤖 AlgoLassi Assistant Have a question about this tutorial?

Ask AlgoLassi and get an answer plus the tutorials worth studying next.

Ask a question

đŸ’Ŧ Comments

Sign in with Google to publish immediately, or comment anonymously and wait for approval.

Comments will appear here when available.