Dependency Injection (DI)
🔌 8. Dependency Injection (DI) in .NET
DI is a core pattern for building testable and maintainable software.
interface IEmailService
{
void Send(string msg);
}
class EmailService : IEmailService
{
public void Send(string msg) => Console.WriteLine($"Sent: {msg}");
}
class Notification
{
private readonly IEmailService _email;
public Notification(IEmailService email) => _email = email;
public void Notify() => _email.Send("Hello!");
}
🔹 Registering with .NET Core DI
services.AddTransient<IEmailService, EmailService>();