Refactor EmailSender to use new Email abstraction

Introduced a new `Email` record to simplify the representation of outgoing email messages. Updated the `EmailSender.Send` method to accept `Email` instead of `OutgoingEmailEvent`, with a `ToEvent()` method handling the conversion internally.

Refactored test cases to use the `Email` record, removing redundant properties (`Id` and `QueuedAt`) that are now auto-generated. Updated XML documentation to reflect these changes. Adjusted namespaces and added necessary `using` directives for proper referencing.

These changes improve code readability, maintainability, and centralize the mapping logic for outgoing email events.
This commit is contained in:
2026-07-29 14:36:10 +02:00
parent 2f39db94ca
commit c73d8d8f36
4 changed files with 57 additions and 16 deletions

View File

@@ -0,0 +1,46 @@
using DigitalData.MessagingService.Publisher.Abstraction;
namespace DigitalData.MessagingService.Client;
/// <summary>
/// Represents an outgoing email message to be sent through the messaging service.
/// </summary>
public record Email
{
/// <summary>
/// Recipient email address
/// </summary>
public string Recipient { get; set; } = null!;
/// <summary>
/// Email subject
/// </summary>
public string Subject { get; set; } = null!;
/// <summary>
/// Email body (HTML or plain text)
/// </summary>
public string Body { get; set; } = null!;
/// <summary>
/// Is HTML email (default: true)
/// </summary>
public bool IsHtml { get; set; } = true;
/// <summary>
/// Converts this <see cref="Email"/> instance to an <see cref="OutgoingEmailEvent"/>.
/// </summary>
/// <returns>A new <see cref="OutgoingEmailEvent"/> representing this email.</returns>
internal OutgoingEmailEvent ToEvent()
{
return new OutgoingEmailEvent
{
Id = Guid.NewGuid(),
Recipient = Recipient,
Subject = Subject,
Body = Body,
IsHtml = IsHtml,
QueuedAt = DateTime.Now
};
}
}

View File

@@ -106,24 +106,25 @@ public static class EmailSender
}
/// <summary>
/// Enqueues the specified email event to the RabbitMQ messaging pipeline.
/// Enqueues the specified email to the RabbitMQ messaging pipeline.
/// </summary>
/// <param name="email">The outgoing email event to enqueue.</param>
/// <param name="email">The outgoing email data to enqueue.</param>
/// <exception cref="InvalidOperationException">
/// Thrown when <see cref="ConnectRabbitMq(Action{RabbitMqConfiguration}, OnReconnect)"/> has not been called prior to sending.
/// </exception>
/// <remarks>
/// This method resolves <see cref="IOutgoingEmailPublisher"/> from the internal
/// This method maps <see cref="Email"/> to <see cref="OutgoingEmailEvent"/>,
/// then resolves <see cref="IOutgoingEmailPublisher"/> 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.
/// </remarks>
public static void Send(OutgoingEmailEvent email)
public static void Send(Email email)
{
if(!IsConnected)
throw new InvalidOperationException("Messaging service is not connected. Call ConnectRabbitMq first.");
var publisher = LazyProvider.Value.GetRequiredService<IOutgoingEmailPublisher>();
publisher.EnqueueAsync(email);
publisher.EnqueueAsync(email.ToEvent());
}
}