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:
Code becomes tightly coupled
Hard to test
Hard to maintain
Difficult to scale
To solve this problem, ASP.NET Core uses:
Dependency Injection (DI)
In this article, we will learn:
What is Dependency Injection?
Why do we use it?
How it works?
Step-by-step example
Output explanation
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:
Classes depend on specific implementations
Difficult to change code
Hard to write unit tests
With DI:
Loose coupling
Easy testing
Clean code
Better maintainability
Real-Life Example
Think like this:
A Car needs an Engine
Instead of building the engine inside the car
The engine is provided from outside
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:
Whenever IMessageService is required
Provide MessageService object
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:
Working with services
Connecting to a database
Sending emails
Logging
Building large applications
Conclusion
In this article, we learned:
What is Dependency Injection
Why it is important
How to implement it in ASP.NET Core
Types of DI
Example with output
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.
Marcin Szolke ScholkePosted Feb 26, 2026, 5:15 PM
Perfect - i like this solution old projects without Dependency Injection - Desktop, Web - IT WAS TERRIBLE :)