Feb 10 2025
using MediatR;
public class UserRegisteredNotification : INotification
{
public string UserId { get; }
public string Email { get; }
public UserRegisteredNotification(string userId, string email)
{
UserId = userId;
Email = email;
}
}
public class SendWelcomeEmailHandler : INotificationHandler<UserRegisteredNotification>
{
private readonly ILogger<SendWelcomeEmailHandler> _logger;
public SendWelcomeEmailHandler(ILogger<SendWelcomeEmailHandler> logger)
{
_logger = logger;
}
public Task Handle(UserRegisteredNotification notification, CancellationToken cancellationToken)
{
_logger.LogInformation($"Sending welcome email to {notification.Email}");
// Simulate sending email logic
return Task.CompletedTask;
}
}
public class LogUserRegistrationHandler : INotificationHandler<UserRegisteredNotification>
{
private readonly ILogger<LogUserRegistrationHandler> _logger;
public LogUserRegistrationHandler(ILogger<LogUserRegistrationHandler> logger)
{
_logger = logger;
}
public Task Handle(UserRegisteredNotification notification, CancellationToken cancellationToken)
{
_logger.LogInformation($"User registered: {notification.UserId}, Email: {notification.Email}");
return Task.CompletedTask;
}
}
public class AnalyticsServiceHandler : INotificationHandler<UserRegisteredNotification>
{
private readonly ILogger<AnalyticsServiceHandler> _logger;
public AnalyticsServiceHandler(ILogger<AnalyticsServiceHandler> logger)
{
_logger = logger;
}
public Task Handle(UserRegisteredNotification notification, CancellationToken cancellationToken)
{
_logger.LogInformation($"Analytics: Tracking new user registration for {notification.UserId}");
return Task.CompletedTask;
}
}
var builder = WebApplication.CreateBuilder(args);
// Register MediatR
builder.Services.AddMediatR(Assembly.GetExecutingAssembly());
var app = builder.Build();
// Minimal API Endpoint for User Registration
app.MapPost("/register", async (UserRegistrationRequest request, IMediator mediator) =>
{
var userId = Guid.NewGuid().ToString();
// Simulating user registration (e.g., saving to database)
await mediator.Publish(new UserRegisteredNotification(userId, request.Email));
return Results.Ok(new { Message = "User registered successfully!", UserId = userId });
});
app.Run();
// DTO for user registration
public record UserRegistrationRequest(string Email);
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 15,250+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 15,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.