Refactor: Rename OutgoingEmail to SendingEmail
This commit renames and refactors all instances of `OutgoingEmail` to `SendingEmail` across the codebase to improve terminology consistency and align with domain language. - Renamed classes, interfaces, and records (e.g., `OutgoingEmailPublisher` → `SendingEmailPublisher`, `OutgoingEmailEvent` → `SendingEmailEvent`). - Updated method signatures, parameters, and return types to use `SendingEmail`. - Adjusted dependency injection registrations to reflect the new naming. - Updated mappings in `EmailMappingProfile` to map `SendEmailCommand` to `SendingEmailEvent`. - Refactored `SendEmailCommand` and its handler to work with `SendingEmailEvent`. - Updated `EmailsController` to use `SendingEmailEvent` in the `SendEmail` action. - Refactored integration tests to test `SendingEmailPublisher` and updated test data accordingly. - Updated log messages, error handling, and comments to reflect the new terminology. - Revised documentation and utility methods to use `SendingEmailEvent`. This refactor ensures consistency, improves readability, and reduces ambiguity in the codebase.
This commit is contained in:
@@ -3,9 +3,9 @@ namespace DigitalData.MessagingService.Abstraction;
|
||||
/// <summary>
|
||||
/// Email queue interface for outgoing emails.
|
||||
/// </summary>
|
||||
public interface IOutgoingEmailPublisher
|
||||
public interface ISendingEmailPublisher
|
||||
{
|
||||
Task EnqueueAsync(OutgoingEmailEvent outgoingEmailEvent, CancellationToken cancellationToken = default);
|
||||
Task EnqueueAsync(SendingEmailEvent sendingEmailEvent, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<int> GetQueueDepthAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace DigitalData.MessagingService.Abstraction;
|
||||
|
||||
public record OutgoingEmailCreateDto
|
||||
public record SendingEmailCreateDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Recipient email address
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace DigitalData.MessagingService.Abstraction;
|
||||
|
||||
public record OutgoingEmailEvent
|
||||
public record SendingEmailEvent
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@ public class EmailMappingProfile : Profile
|
||||
{
|
||||
public EmailMappingProfile()
|
||||
{
|
||||
// SendEmailCommand -> OutgoingEmailEvent
|
||||
// SendEmailCommand -> SendingEmailEvent
|
||||
// Sender is resolved via MediatR in the handler and set separately after mapping.
|
||||
CreateMap<SendEmailCommand, OutgoingEmailEvent>()
|
||||
CreateMap<SendEmailCommand, SendingEmailEvent>()
|
||||
.ForMember(dest => dest.Id, opt => opt.MapFrom(_ => Guid.NewGuid()))
|
||||
.ForMember(dest => dest.QueuedAt, opt => opt.MapFrom(_ => DateTime.Now))
|
||||
.ForMember(dest => dest.Sender, opt => opt.Ignore());
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace DigitalData.MessagingService.Application.EmailSending.Commands;
|
||||
/// <summary>
|
||||
/// Command to send an email (enqueue to RabbitMQ)
|
||||
/// </summary>
|
||||
public record SendEmailCommand : IRequest<OutgoingEmailEvent>
|
||||
public record SendEmailCommand : IRequest<SendingEmailEvent>
|
||||
{
|
||||
public required GetSenderQuery Sender { get; init; }
|
||||
|
||||
@@ -36,23 +36,23 @@ public record SendEmailCommand : IRequest<OutgoingEmailEvent>
|
||||
|
||||
/// <summary>
|
||||
/// Handler for SendEmailCommand
|
||||
/// Resolves the sender account via MediatR, maps to OutgoingEmailEvent and enqueues to RabbitMQ
|
||||
/// Resolves the sender account via MediatR, maps to SendingEmailEvent and enqueues to RabbitMQ
|
||||
/// </summary>
|
||||
public class SendEmailCommandHandler(
|
||||
ISender Sender,
|
||||
IOutgoingEmailPublisher Publisher,
|
||||
IMapper Mapper) : IRequestHandler<SendEmailCommand, OutgoingEmailEvent>
|
||||
ISendingEmailPublisher Publisher,
|
||||
IMapper Mapper) : IRequestHandler<SendEmailCommand, SendingEmailEvent>
|
||||
{
|
||||
public async Task<OutgoingEmailEvent> Handle(SendEmailCommand request, CancellationToken cancellationToken)
|
||||
public async Task<SendingEmailEvent> Handle(SendEmailCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var senderAccount = await Sender.Send(request.Sender, cancellationToken)
|
||||
?? throw new NotFoundException(
|
||||
$"No email account found for the given sender criteria (Id: {request.Sender.Id}, Username: {request.Sender.Username}).");
|
||||
|
||||
var outgoingEmailEvent = Mapper.Map<OutgoingEmailEvent>(request) with { Sender = senderAccount };
|
||||
var sendingEmailEvent = Mapper.Map<SendingEmailEvent>(request) with { Sender = senderAccount };
|
||||
|
||||
// Enqueue to RabbitMQ
|
||||
await Publisher.EnqueueAsync(outgoingEmailEvent, cancellationToken);
|
||||
return outgoingEmailEvent;
|
||||
await Publisher.EnqueueAsync(sendingEmailEvent, cancellationToken);
|
||||
return sendingEmailEvent;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ public static class DependencyInjection
|
||||
services.AddSingleton<IEncryptionService, DataProtectionEncryptionService>();
|
||||
|
||||
// --- Email Queue (RabbitMQ) ---
|
||||
services.AddSingleton<OutgoingEmailConsumer>();
|
||||
services.AddSingleton<SendingEmailConsumer>();
|
||||
services.AddMessagingServicePublisher();
|
||||
|
||||
// --- RabbitMQ Configuration ---
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace DigitalData.MessagingService.Infrastructure.Queue;
|
||||
/// Provides message persistence, scalability, and reliability.
|
||||
/// Uses Lazy<T> initialization pattern to avoid blocking constructor.
|
||||
/// </summary>
|
||||
public sealed class OutgoingEmailConsumer : IAsyncDisposable
|
||||
public sealed class SendingEmailConsumer : IAsyncDisposable
|
||||
{
|
||||
private readonly RabbitMqConfiguration _config;
|
||||
|
||||
@@ -23,9 +23,9 @@ public sealed class OutgoingEmailConsumer : IAsyncDisposable
|
||||
|
||||
private readonly Lazy<Task> _lazyInit;
|
||||
|
||||
private readonly ILogger<OutgoingEmailConsumer>? _logger;
|
||||
private readonly ILogger<SendingEmailConsumer>? _logger;
|
||||
|
||||
public OutgoingEmailConsumer(IOptions<RabbitMqConfiguration> config, IEmailService EmailService, RabbitMqConnectionFactory CnnFactory, ILogger<OutgoingEmailConsumer>? logger = null)
|
||||
public SendingEmailConsumer(IOptions<RabbitMqConfiguration> config, IEmailService EmailService, RabbitMqConnectionFactory CnnFactory, ILogger<SendingEmailConsumer>? logger = null)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config.Value;
|
||||
@@ -38,11 +38,11 @@ public sealed class OutgoingEmailConsumer : IAsyncDisposable
|
||||
|
||||
consumer.ReceivedAsync += async (sender, args) =>
|
||||
{
|
||||
OutgoingEmailEvent? oMailEvent = null;
|
||||
SendingEmailEvent? oMailEvent = null;
|
||||
try
|
||||
{
|
||||
var json = Encoding.UTF8.GetString(args.Body.ToArray());
|
||||
oMailEvent = JsonSerializer.Deserialize<OutgoingEmailEvent>(json);
|
||||
oMailEvent = JsonSerializer.Deserialize<SendingEmailEvent>(json);
|
||||
|
||||
if (oMailEvent is not null)
|
||||
{
|
||||
@@ -69,7 +69,7 @@ public sealed class OutgoingEmailConsumer : IAsyncDisposable
|
||||
|
||||
// TODO: Error Reporting Strategy
|
||||
// Option 1: Separate RabbitMQ Queue (emailprofiler.errors)
|
||||
// - Create EmailErrorReport entity { OutgoingEmailEventId, Exception, StackTrace, Timestamp, RetryAttempt }
|
||||
// - Create EmailErrorReport entity { SendingEmailEventId, Exception, StackTrace, Timestamp, RetryAttempt }
|
||||
// - Publish to error queue: await _errorQueue.EnqueueAsync(errorReport)
|
||||
// - Separate worker processes error queue → Log to DB/File/External monitoring
|
||||
//
|
||||
@@ -111,7 +111,7 @@ public sealed class OutgoingEmailConsumer : IAsyncDisposable
|
||||
public async Task InitAsync()
|
||||
{
|
||||
if (_lazyInit.IsValueCreated)
|
||||
_logger?.LogWarning("OutgoingEmailConsumer already initialized. InitAsync() called multiple times.");
|
||||
_logger?.LogWarning("SendingEmailConsumer already initialized. InitAsync() called multiple times.");
|
||||
|
||||
await _lazyInit.Value;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace DigitalData.MessagingService.Infrastructure.Services.Background;
|
||||
/// Leverages a push-based, event-driven RabbitMQ consumer to eliminate polling overhead.
|
||||
/// Email account configuration is resolved exclusively from application settings; no database access is performed.
|
||||
/// </summary>
|
||||
public class AsyncInitWorker(OutgoingEmailConsumer EmailConsumer) : BackgroundService
|
||||
public class AsyncInitWorker(SendingEmailConsumer EmailConsumer) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
|
||||
@@ -13,7 +13,7 @@ public static class DependencyInjection
|
||||
services.AddRabbitMqConnectionFactory(configure);
|
||||
|
||||
// --- Email Queue (RabbitMQ) ---
|
||||
services.AddSingleton<IOutgoingEmailPublisher, OutgoingEmailPublisher>();
|
||||
services.AddSingleton<ISendingEmailPublisher, SendingEmailPublisher>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -13,14 +13,14 @@ namespace DigitalData.MessagingService.Publisher;
|
||||
/// Provides message persistence, scalability, and reliability.
|
||||
/// Uses Lazy<T> initialization pattern to avoid blocking constructor.
|
||||
/// </summary>
|
||||
public sealed class OutgoingEmailPublisher : IOutgoingEmailPublisher, IAsyncDisposable
|
||||
public sealed class SendingEmailPublisher : ISendingEmailPublisher, IAsyncDisposable
|
||||
{
|
||||
private readonly RabbitMqConfiguration _config;
|
||||
private readonly ILogger<OutgoingEmailPublisher> _logger;
|
||||
private readonly ILogger<SendingEmailPublisher> _logger;
|
||||
private readonly RabbitMqConnectionFactory _cnnFactory;
|
||||
private readonly Lazy<Task<IChannel>> _lazyChannel;
|
||||
|
||||
public OutgoingEmailPublisher(IOptions<RabbitMqConfiguration> config, ILogger<OutgoingEmailPublisher> logger, RabbitMqConnectionFactory cnnFactory)
|
||||
public SendingEmailPublisher(IOptions<RabbitMqConfiguration> config, ILogger<SendingEmailPublisher> logger, RabbitMqConnectionFactory cnnFactory)
|
||||
{
|
||||
_config = config.Value;
|
||||
_logger = logger;
|
||||
@@ -66,9 +66,9 @@ public sealed class OutgoingEmailPublisher : IOutgoingEmailPublisher, IAsyncDisp
|
||||
return channel;
|
||||
}
|
||||
|
||||
public async Task EnqueueAsync(OutgoingEmailEvent outgoingEmailEvent, CancellationToken cancellationToken = default)
|
||||
public async Task EnqueueAsync(SendingEmailEvent sendingEmailEvent, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var json = JsonSerializer.Serialize(outgoingEmailEvent);
|
||||
var json = JsonSerializer.Serialize(sendingEmailEvent);
|
||||
var body = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
var properties = new BasicProperties
|
||||
|
||||
@@ -23,11 +23,11 @@ public class EmailsController(IMediator mediator) : ControllerBase
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> SendEmail([FromBody] SendEmailCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var outgoingEmailEvent = await mediator.Send(command, cancellationToken);
|
||||
var sendingEmailEvent = await mediator.Send(command, cancellationToken);
|
||||
|
||||
return Accepted(new
|
||||
{
|
||||
outgoingEmailEvent.Id,
|
||||
sendingEmailEvent.Id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,12 @@ public record Email
|
||||
public bool IsHtml { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Converts this <see cref="Email"/> instance to an <see cref="OutgoingEmailEvent"/>.
|
||||
/// Converts this <see cref="Email"/> instance to an <see cref="SendingEmailEvent"/>.
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="OutgoingEmailEvent"/> representing this email.</returns>
|
||||
internal OutgoingEmailEvent ToEvent()
|
||||
/// <returns>A new <see cref="SendingEmailEvent"/> representing this email.</returns>
|
||||
internal SendingEmailEvent ToEvent()
|
||||
{
|
||||
return new OutgoingEmailEvent
|
||||
return new SendingEmailEvent
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Recipients = Recipients,
|
||||
|
||||
@@ -113,8 +113,8 @@ public static class EmailSender
|
||||
/// Thrown when <see cref="ConnectRabbitMq(Action{RabbitMqConfiguration}, OnReconnect)"/> has not been called prior to sending.
|
||||
/// </exception>
|
||||
/// <remarks>
|
||||
/// This method maps <see cref="Email"/> to <see cref="OutgoingEmailEvent"/>,
|
||||
/// then resolves <see cref="IOutgoingEmailPublisher"/> from the internal
|
||||
/// This method maps <see cref="Email"/> to <see cref="SendingEmailEvent"/>,
|
||||
/// then resolves <see cref="ISendingEmailPublisher"/> from the internal
|
||||
/// service provider and calls <c>EnqueueAsync</c> in a fire-and-forget manner.
|
||||
/// Ensure that any unhandled exceptions from the async operation are handled
|
||||
/// at the publisher level.
|
||||
@@ -124,7 +124,7 @@ public static class EmailSender
|
||||
if(!IsConnected)
|
||||
throw new InvalidOperationException("Messaging service is not connected. Call ConnectRabbitMq first.");
|
||||
|
||||
var publisher = LazyProvider.Value.GetRequiredService<IOutgoingEmailPublisher>();
|
||||
var publisher = LazyProvider.Value.GetRequiredService<ISendingEmailPublisher>();
|
||||
publisher.EnqueueAsync(email.ToEvent());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user