ASP.NET Core  

What is Dependency Injection in ASP.NET Core?

Introduction

In modern ASP.NET Core development, writing clean, maintainable, and scalable applications is very important. One of the key design patterns that helps achieve this is Dependency Injection (DI).

Dependency Injection in ASP.NET Core is a built-in feature that helps manage how objects are created and how they depend on each other. Instead of creating dependencies manually inside classes, DI allows you to inject them from outside.

In this article, we will explain what dependency injection in ASP.NET Core is in simple words, how it works, why it is important, and how to use it with real-world examples.

What is Dependency Injection?

Dependency Injection is a design pattern where a class receives its dependencies from an external source rather than creating them itself.

In simple words, instead of saying "I will create what I need," the class says "Give me what I need."

Without Dependency Injection

public class UserService
{
    private readonly EmailService _emailService = new EmailService();

    public void RegisterUser()
    {
        _emailService.SendEmail();
    }
}

Problem:

  • Tight coupling

  • Hard to test

  • Difficult to maintain

With Dependency Injection

public class UserService
{
    private readonly IEmailService _emailService;

    public UserService(IEmailService emailService)
    {
        _emailService = emailService;
    }

    public void RegisterUser()
    {
        _emailService.SendEmail();
    }
}

Now, the dependency is injected from outside.

Why Dependency Injection is Important in ASP.NET Core

Dependency Injection plays a major role in building modern web applications.

Benefits of Dependency Injection

  • Reduces tight coupling between classes

  • Improves code maintainability

  • Makes unit testing easier

  • Supports better code organization

  • Enables flexible and scalable architecture

How Dependency Injection Works in ASP.NET Core

ASP.NET Core has a built-in Dependency Injection container.

There are three main steps:

1. Register Services

You register services in Program.cs:

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

2. Inject Services

Services are injected via constructor:

public class UserController : Controller
{
    private readonly IEmailService _emailService;

    public UserController(IEmailService emailService)
    {
        _emailService = emailService;
    }
}

3. Use Services

_emailService.SendEmail();

Types of Dependency Injection in ASP.NET Core

1. Constructor Injection (Most Common)

Dependencies are injected through constructor.

2. Method Injection

Dependencies are passed as method parameters.

3. Property Injection (Less Common)

Dependencies are set through properties.

Service Lifetimes in ASP.NET Core

ASP.NET Core provides three types of service lifetimes.

1. Transient

  • Created every time requested

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

2. Scoped

  • Created once per request

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

3. Singleton

  • Created once for entire application

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

Real-World Example

Imagine an e-commerce application:

  • OrderService depends on PaymentService

  • PaymentService depends on NotificationService

Using DI:

  • Each service is loosely coupled

  • Easy to replace or test

Dependency Injection vs Manual Object Creation

FeatureDependency InjectionManual Creation
CouplingLoose couplingTight coupling
TestingEasyDifficult
FlexibilityHighLow
MaintainabilityHighLow

Common Mistakes to Avoid

1. Injecting Too Many Dependencies

Keep classes focused.

2. Using Singleton Incorrectly

Avoid shared state issues.

3. Not Using Interfaces

Always depend on abstractions.

Best Practices for Dependency Injection

  • Use interfaces for services

  • Keep services small and focused

  • Choose correct service lifetime

  • Avoid service locator pattern

  • Use constructor injection whenever possible

Summary

Dependency Injection in ASP.NET Core is a powerful design pattern that helps create loosely coupled, maintainable, and testable applications. By injecting dependencies instead of creating them manually, developers can build scalable and flexible systems. Understanding DI is essential for modern ASP.NET Core development and is widely used in real-world enterprise applications.