Dependency Injection (DI) in .
NET
INTRODUCTION
Dependency Injection (DI) is one of the most important concepts in .NET and
[Link] Core. It is a design pattern that helps create loosely coupled applications by
providing dependencies from outside a class instead of creating them inside the
class.
DI is a way to implement the Dependency Inversion Principle, which is one of the
SOLID principles.
WHY DEPENDENCY INJECTION IS NEEDED
Consider a UserService class that needs an EmailService to send emails.
Without DI, the UserService creates its own EmailService object.
public class UserService
{
private readonly EmailService _emailService;
public UserService()
{
_emailService = new EmailService();
}
}
This approach creates tight coupling because UserService depends directly on
EmailService.
Problems with this approach:
Difficult to unit test.
Hard to replace EmailService with another implementation.
Changes in EmailService can affect UserService.
USING DEPENDENCY INJECTION
Instead of creating the dependency inside the class, we inject it through the
constructor.
CREATE AN INTERFACE
public interface IEmailService
{
void SendEmail(string message);
}
IMPLEMENT THE INTERFACE
public class EmailService : IEmailService
{
public void SendEmail(string message)
{
[Link](message);
}
}
INJECT DEPENDENCY THROUGH CONSTRUCTOR
public class UserService
{
private readonly IEmailService _emailService;
public UserService(IEmailService emailService)
{
_emailService = emailService;
}
}
Now UserService depends on an abstraction (IEmailService) rather than a concrete
implementation.
DEPENDENCY INJECTION CONTAINER IN [Link] CORE
[Link] Core provides a built-in DI container.
Services are registered inside [Link].
[Link]<IEmailService, EmailService>();
When [Link] Core creates an object that requires IEmailService, it automatically
provides an EmailService instance.
SERVICE LIFETIMES
TRANSIENT
A new instance is created every time the service is requested.
[Link]<IEmailService, EmailService>();
Use when:
Service is lightweight.
No state needs to be maintained.
SCOPED
One instance is created per HTTP request.
[Link]<IEmailService, EmailService>();
Use when:
Working with repositories.
Using Entity Framework DbContext.
SINGLETON
Only one instance is created for the entire application lifetime.
[Link]<IEmailService, EmailService>();
Use when:
Caching data.
Configuration services.
Logging services.
DEPENDENCY INJECTION IN CLEAN ARCHITECTURE
A typical request flow looks like this:
Controller
↓
Service
↓
Repository
↓
DbContext
Example:
ProductController
↓
IProductService
↓
IProductRepository
↓
AppDbContext
All dependencies are injected through constructors, making the application modular
and maintainable.
ADVANTAGES OF DEPENDENCY INJECTION
1. Loose coupling between classes.
2. Easier unit testing using mocks.
3. Better maintainability.
4. Improved scalability.
5. Supports SOLID principles.
6. Easier replacement of implementations.
COMMON INTERVIEW QUESTIONS
WHAT IS DEPENDENCY INJECTION?
Dependency Injection is a design pattern where dependencies are supplied to a class
instead of being created by the class itself.
WHAT ARE THE THREE SERVICE LIFETIMES IN .NET?
Transient
Scoped
Singleton
WHICH LIFETIME IS RECOMMENDED FOR DBCONTEXT?
Scoped.
WHY SHOULD WE USE INTERFACES WITH DI?
Interfaces reduce coupling and make testing and maintenance easier.
SUMMARY
Dependency Injection is a core feature of [Link] Core that allows objects to
receive their dependencies from an external source. It promotes loose coupling,
improves testability, and makes applications easier to maintain. Almost every modern
.NET application uses Dependency Injection extensively, especially in Clean
Architecture and enterprise-level projects.