Introduction
When building applications using ASP.NET Core, we often create objects inside classes.
Example:
ProductService service = new ProductService();
But this is not a good practice in large applications.
Why?
Because:
To solve this problem, ASP.NET Core uses:
Dependency Injection (DI)
In this article, we will learn:
Everything in simple words.
What is Dependency Injection?
Dependency Injection means:
Instead of creating an object inside a class, we provide (inject) the object from outside.
Simple meaning:
βDonβt create dependency. Receive dependency.β
Why Use Dependency Injection?
Without DI:
With DI:
Loose coupling
Easy testing
Clean code
Better maintainability
Real-Life Example
Think like this:
That is Dependency Injection.
Step-by-Step Example in ASP.NET Core
Step 1 β Create Interface
Create a folder: Services
Add Interface:
public interface IMessageService
{
string GetMessage();
}
Step 2 β Create Service Class
public class MessageService : IMessageService
{
public string GetMessage()
{
return "Hello from Dependency Injection!";
}
}
Step 3 β Register Service in Program.cs
Open Program.cs:
builder.Services.AddScoped<IMessageService, MessageService>();
This tells ASP.NET Core:
Step 4 β Use in Controller
using Microsoft.AspNetCore.Mvc;
public class HomeController : Controller
{
private readonly IMessageService _messageService;
public HomeController(IMessageService messageService)
{
_messageService = messageService;
}
public IActionResult Index()
{
var message = _messageService.GetMessage();
ViewBag.Message = message;
return View();
}
}
Notice:
We did NOT create an object using new.
ASP.NET Core automatically injects it.
Step 5 β Create View
Index.cshtml:
<h2>@ViewBag.Message</h2>
Output
When you run the project:
https://localhost:xxxx/Home/Index
You will see:
Hello from Dependency Injection!
How Dependency Injection Works Internally
We register the service in Program.cs
ASP.NET Core creates the object automatically
It injects the object into the constructor
The controller uses the injected service
Types of Dependency Injection in ASP.NET Core
AddTransient
New object every time requested.
AddScoped
One object per request.
AddSingleton
One object for the entire application lifetime.
Difference Table
| Type | Lifetime |
|---|
| Transient | New instance every time |
| Scoped | One per request |
| Singleton | One for entire application |
When Should You Use DI?
Use Dependency Injection when:
Conclusion
In this article, we learned:
Dependency Injection is one of the most important concepts in ASP.NET Core and is frequently asked in interviews.
If you understand DI clearly, your code becomes professional and scalable.
Summary
Dependency Injection in ASP.NET Core helps reduce tight coupling by injecting dependencies from outside instead of creating them inside classes. It improves testability, maintainability, and scalability. By registering services in Program.cs and using constructor injection, ASP.NET Core automatically manages object creation based on the configured lifetime (Transient, Scoped, or Singleton). Understanding DI is essential for building clean and professional applications.