Dependency Injection (DI) is a fundamental concept in .NET Core that promotes loose coupling and testability by managing the dependencies between classes. It is an implementation of the Inversion of Control (IoC) principle, where objects are provided their dependencies instead of being created internally.
Key Concepts
- Dependency: A class or interface that another class relies on.
- Dependency Injection: The process of providing these dependencies to a class from an external source rather than the class creating them itself.
- Service Container: A component responsible for managing the lifecycle of objects and resolving dependencies.
Use Dependency Injection
1. Define the Service Interface and Implementation.
Create an interface and its implementation for the service you want to inject.
public interface IGreetingService
{
string GetGreeting();
}
public class GreetingService : IGreetingService
{
public string GetGreeting()
{
return "Hello, Dependency Injection!";
}
}
2. Register the Service in the Dependency Injection Container.
In the Program.cs file (or Startup.cs in older versions), register your service in the DI container.
var builder = WebApplication.CreateBuilder(args);
// Add services to the DI container
builder.Services.AddTransient<IGreetingService, GreetingService>();
var app = builder.Build();
3. Inject the Service Where Needed.
In an ASP.NET Core application, inject the service into a controller via its constructor.
public class HomeController : Controller
{
private readonly IGreetingService _greetingService;
public HomeController(IGreetingService greetingService)
{
_greetingService = greetingService;
}
public IActionResult Index()
{
var message = _greetingService.GetGreeting();
return Content(message);
}
}
Service Lifetime in .NET Core
Benefits of Dependency Injection
- Loose Coupling: Reduces dependencies between components, making the system modular.
- Testability: Facilitates unit testing by allowing mock dependencies.
- Centralized Configuration: All dependencies are managed in a single place.
- Improved Maintainability: Adding, updating, or replacing services becomes easier.