C#
Beginner
1 min read
Dependency Injection in .NET
Example
// Dependency Injection lifetimes and IOptions<T>
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
// --- Configuration options class ---
public class EmailSettings
{
public string Host { get; set; } = "smtp.example.com";
public int Port { get; set; } = 587;
public bool UseSsl { get; set; } = true;
public string FromName { get; set; } = "App";
}
// --- Service abstractions ---
public interface IEmailSender { Task SendAsync(string to, string subject, string body); }
public interface INotifier { Task NotifyAsync(string userId, string msg); }
// --- Concrete services ---
public class SmtpEmailSender : IEmailSender
{
private readonly EmailSettings _cfg;
public SmtpEmailSender(IOptions<EmailSettings> opts) => _cfg = opts.Value;
public Task SendAsync(string to, string subject, string body)
{
Console.WriteLine($"SMTP [{_cfg.Host}:{_cfg.Port}] To={to} Subject={subject}");
return Task.CompletedTask;
}
}
public class UserNotifier : INotifier
{
private readonly IEmailSender _email;
public UserNotifier(IEmailSender email) => _email = email; // injected!
public Task NotifyAsync(string userId, string msg) =>
_email.SendAsync($"{userId}@example.com", "Notification", msg);
}
// --- Composition root ---
var services = new ServiceCollection();
services.Configure<EmailSettings>(opts => { opts.Host = "mail.myapp.com"; opts.Port = 465; });
services.AddSingleton<IEmailSender, SmtpEmailSender>();
services.AddTransient<INotifier, UserNotifier>();
using var sp = services.BuildServiceProvider();
using var scope = sp.CreateScope();
var notifier = scope.ServiceProvider.GetRequiredService<INotifier>();
await notifier.NotifyAsync("alice", "Your order has shipped!");