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:
@@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -106,24 +106,25 @@ public static class EmailSender
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enqueues the specified email event to the RabbitMQ messaging pipeline.
|
/// Enqueues the specified email to the RabbitMQ messaging pipeline.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="email">The outgoing email event to enqueue.</param>
|
/// <param name="email">The outgoing email data to enqueue.</param>
|
||||||
/// <exception cref="InvalidOperationException">
|
/// <exception cref="InvalidOperationException">
|
||||||
/// Thrown when <see cref="ConnectRabbitMq(Action{RabbitMqConfiguration}, OnReconnect)"/> has not been called prior to sending.
|
/// Thrown when <see cref="ConnectRabbitMq(Action{RabbitMqConfiguration}, OnReconnect)"/> has not been called prior to sending.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
/// <remarks>
|
/// <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.
|
/// service provider and calls <c>EnqueueAsync</c> in a fire-and-forget manner.
|
||||||
/// Ensure that any unhandled exceptions from the async operation are handled
|
/// Ensure that any unhandled exceptions from the async operation are handled
|
||||||
/// at the publisher level.
|
/// at the publisher level.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public static void Send(OutgoingEmailEvent email)
|
public static void Send(Email email)
|
||||||
{
|
{
|
||||||
if(!IsConnected)
|
if(!IsConnected)
|
||||||
throw new InvalidOperationException("Messaging service is not connected. Call ConnectRabbitMq first.");
|
throw new InvalidOperationException("Messaging service is not connected. Call ConnectRabbitMq first.");
|
||||||
|
|
||||||
var publisher = LazyProvider.Value.GetRequiredService<IOutgoingEmailPublisher>();
|
var publisher = LazyProvider.Value.GetRequiredService<IOutgoingEmailPublisher>();
|
||||||
publisher.EnqueueAsync(email);
|
publisher.EnqueueAsync(email.ToEvent());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,14 +75,12 @@ public sealed class EmailSenderTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void Send_WithValidEmail_DoesNotThrow()
|
public void Send_WithValidEmail_DoesNotThrow()
|
||||||
{
|
{
|
||||||
var email = new OutgoingEmailEvent
|
var email = new Email
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(),
|
|
||||||
Recipient = "hakanttek@gmail.com",
|
Recipient = "hakanttek@gmail.com",
|
||||||
Subject = "EmailSender.Send Integration Test",
|
Subject = "EmailSender.Send Integration Test",
|
||||||
Body = "<p>Sent via EmailSender static client.</p>",
|
Body = "<p>Sent via EmailSender static client.</p>",
|
||||||
IsHtml = true,
|
IsHtml = true,
|
||||||
QueuedAt = DateTime.Now
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var exception = Record.Exception(() => EmailSender.Send(email));
|
var exception = Record.Exception(() => EmailSender.Send(email));
|
||||||
@@ -93,14 +91,12 @@ public sealed class EmailSenderTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void Send_WithPlainTextBody_DoesNotThrow()
|
public void Send_WithPlainTextBody_DoesNotThrow()
|
||||||
{
|
{
|
||||||
var email = new OutgoingEmailEvent
|
var email = new Email
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(),
|
|
||||||
Recipient = "hakanttek@gmail.com",
|
Recipient = "hakanttek@gmail.com",
|
||||||
Subject = "Plain Text Test",
|
Subject = "Plain Text Test",
|
||||||
Body = "This is a plain text email.",
|
Body = "This is a plain text email.",
|
||||||
IsHtml = false,
|
IsHtml = false
|
||||||
QueuedAt = DateTime.Now
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var exception = Record.Exception(() => EmailSender.Send(email));
|
var exception = Record.Exception(() => EmailSender.Send(email));
|
||||||
|
|||||||
@@ -55,14 +55,12 @@ public sealed class EmailSenderUrlOverloadTests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void Send_AfterUrlOverloadConnection_DoesNotThrow()
|
public void Send_AfterUrlOverloadConnection_DoesNotThrow()
|
||||||
{
|
{
|
||||||
var email = new OutgoingEmailEvent
|
var email = new Email
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid(),
|
|
||||||
Recipient = "url-overload-test@example.com",
|
Recipient = "url-overload-test@example.com",
|
||||||
Subject = "URL Overload Integration Test",
|
Subject = "URL Overload Integration Test",
|
||||||
Body = "<p>Sent after URL-based connection.</p>",
|
Body = "<p>Sent after URL-based connection.</p>",
|
||||||
IsHtml = true,
|
IsHtml = true
|
||||||
QueuedAt = DateTime.Now
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Connection was established via Action<> overload in the fixture;
|
// Connection was established via Action<> overload in the fixture;
|
||||||
|
|||||||
Reference in New Issue
Block a user