refactor(publisher)!: simplify AddMessagingServicePublisher registration
Replace the MessagingServicePublisherConfiguration builder with plain overloads that accept either a configuration delegate or IConfiguration. Update EmailSender and the publisher integration test to the new API, and mark the static EmailSender obsolete ahead of the new client. BREAKING CHANGE: AddMessagingServicePublisher no longer accepts a builder configuration delegate.
This commit is contained in:
@@ -7,15 +7,20 @@ namespace DigitalData.MessagingService.Publisher;
|
|||||||
|
|
||||||
public static class DependencyInjection
|
public static class DependencyInjection
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services, Action<MessagingServicePublisherConfiguration>? configure = null)
|
public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
var configuration = new MessagingServicePublisherConfiguration(services);
|
return services.AddSingleton<ISendingEmailPublisher, SendingEmailPublisher>();
|
||||||
|
}
|
||||||
|
|
||||||
configure?.Invoke(configuration);
|
public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services, Action<RabbitMqConnectionConfiguration> configureRabbitMqConnection)
|
||||||
|
{
|
||||||
|
services.AddRabbitMqConnectionFactory(configureRabbitMqConnection);
|
||||||
|
return services.AddMessagingServicePublisher();
|
||||||
|
}
|
||||||
|
|
||||||
// --- Email Queue (RabbitMQ) ---
|
public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services, IConfiguration configuration, string rabbitMqConnectionSectionName = RabbitMqConnectionConfiguration.SectionName)
|
||||||
services.AddSingleton<ISendingEmailPublisher, SendingEmailPublisher>();
|
{
|
||||||
|
services.AddRabbitMqConnectionFactory(configuration.GetSection(rabbitMqConnectionSectionName));
|
||||||
return services;
|
return services.AddMessagingServicePublisher();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
using DigitalData.MessagingService.RabbitMQ;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace DigitalData.MessagingService.Publisher;
|
|
||||||
|
|
||||||
public class MessagingServicePublisherConfiguration
|
|
||||||
{
|
|
||||||
internal IServiceCollection Services { get; }
|
|
||||||
|
|
||||||
internal MessagingServicePublisherConfiguration(IServiceCollection services)
|
|
||||||
{
|
|
||||||
Services = services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessagingServicePublisherConfiguration AddRabbitMqConnectionFactory(Action<RabbitMqConfiguration> configure)
|
|
||||||
{
|
|
||||||
Services.AddRabbitMqConnectionFactory(configure);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public MessagingServicePublisherConfiguration AddRabbitMqConnectionFactory(IConfiguration configuration, string sectionName = RabbitMqConfiguration.SectionName)
|
|
||||||
{
|
|
||||||
Services.AddRabbitMqConnectionFactory(configuration, sectionName);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,14 +13,17 @@ namespace DigitalData.MessagingService.Client;
|
|||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This class manages its own internal <see cref="IServiceProvider"/> using a
|
/// This class manages its own internal <see cref="IServiceProvider"/> using a
|
||||||
/// <see cref="Lazy{T}"/> pattern so the DI container is only built once,
|
/// <see cref="Lazy{T}"/> pattern so the DI container is only built once,
|
||||||
/// on the first call to <see cref="ConnectRabbitMq(Action{RabbitMqConfiguration}, OnReconnect)"/>.
|
/// on the first call to <see cref="ConnectRabbitMq(Action{RabbitMqConnectionConfiguration}, OnReconnect)"/>.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
|
#if NET
|
||||||
|
[Obsolete("Use MessagingServiceClient with dependency injection instead.")]
|
||||||
|
#endif
|
||||||
public static class EmailSender
|
public static class EmailSender
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Internal event used to accumulate service registrations before the
|
/// Internal event used to accumulate service registrations before the
|
||||||
/// <see cref="IServiceProvider"/> is built. Handlers are added by
|
/// <see cref="IServiceProvider"/> is built. Handlers are added by
|
||||||
/// <see cref="ConnectRabbitMq(Action{RabbitMqConfiguration}, OnReconnect)"/> and invoked exactly once during
|
/// <see cref="ConnectRabbitMq(Action{RabbitMqConnectionConfiguration}, OnReconnect)"/> and invoked exactly once during
|
||||||
/// lazy initialization.
|
/// lazy initialization.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static event Action<IServiceCollection> ConfigureServices = delegate { };
|
private static event Action<IServiceCollection> ConfigureServices = delegate { };
|
||||||
@@ -43,7 +46,7 @@ public static class EmailSender
|
|||||||
/// and the internal <see cref="IServiceProvider"/> has been initialized.
|
/// and the internal <see cref="IServiceProvider"/> has been initialized.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// <see langword="true"/> if <see cref="ConnectRabbitMq(Action{RabbitMqConfiguration}, OnReconnect)"/> has been called
|
/// <see langword="true"/> if <see cref="ConnectRabbitMq(Action{RabbitMqConnectionConfiguration}, OnReconnect)"/> has been called
|
||||||
/// and the provider is built; otherwise <see langword="false"/>.
|
/// and the provider is built; otherwise <see langword="false"/>.
|
||||||
/// </value>
|
/// </value>
|
||||||
public static bool IsConnected => LazyProvider.IsValueCreated;
|
public static bool IsConnected => LazyProvider.IsValueCreated;
|
||||||
@@ -53,7 +56,7 @@ public static class EmailSender
|
|||||||
/// the internal dependency injection container.
|
/// the internal dependency injection container.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="configure">
|
/// <param name="configure">
|
||||||
/// A delegate used to configure the <see cref="RabbitMqConfiguration"/>,
|
/// A delegate used to configure the <see cref="RabbitMqConnectionConfiguration"/>,
|
||||||
/// such as host, port, credentials, and exchange settings.
|
/// such as host, port, credentials, and exchange settings.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="onReconnect">
|
/// <param name="onReconnect">
|
||||||
@@ -64,12 +67,12 @@ public static class EmailSender
|
|||||||
/// Thrown when the service is already connected and
|
/// Thrown when the service is already connected and
|
||||||
/// <paramref name="onReconnect"/> is <see cref="OnReconnect.ThrowException"/>.
|
/// <paramref name="onReconnect"/> is <see cref="OnReconnect.ThrowException"/>.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
public static void ConnectRabbitMq(Action<RabbitMqConfiguration> configure, OnReconnect onReconnect = OnReconnect.ThrowException)
|
public static void ConnectRabbitMq(Action<RabbitMqConnectionConfiguration> configure, OnReconnect onReconnect = OnReconnect.ThrowException)
|
||||||
{
|
{
|
||||||
if(IsConnected && onReconnect == OnReconnect.ThrowException)
|
if(IsConnected && onReconnect == OnReconnect.ThrowException)
|
||||||
throw new InvalidOperationException("Messaging service is already connected.");
|
throw new InvalidOperationException("Messaging service is already connected.");
|
||||||
|
|
||||||
ConfigureServices += services => services.AddMessagingServicePublisher(cfg => cfg.AddRabbitMqConnectionFactory(configure));
|
ConfigureServices += services => services.AddMessagingServicePublisher(configure);
|
||||||
_ = LazyProvider.Value; // Force initialization
|
_ = LazyProvider.Value; // Force initialization
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +114,7 @@ public static class EmailSender
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="email">The outgoing email data 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{RabbitMqConnectionConfiguration}, OnReconnect)"/> has not been called prior to sending.
|
||||||
/// </exception>
|
/// </exception>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// This method maps <see cref="EmailContext"/> to <see cref="SendingEmailEvent"/>,
|
/// This method maps <see cref="EmailContext"/> to <see cref="SendingEmailEvent"/>,
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public sealed class SendingEmailPublisherTests : IAsyncDisposable
|
|||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
services.AddLogging();
|
services.AddLogging();
|
||||||
services.AddMessagingServicePublisher(cfg => cfg.AddRabbitMqConnectionFactory(RabbitMqTestConfig.Apply));
|
services.AddMessagingServicePublisher(RabbitMqTestConfig.Apply);
|
||||||
|
|
||||||
_serviceProvider = services.BuildServiceProvider();
|
_serviceProvider = services.BuildServiceProvider();
|
||||||
_publisher = _serviceProvider.GetRequiredService<ISendingEmailPublisher>();
|
_publisher = _serviceProvider.GetRequiredService<ISendingEmailPublisher>();
|
||||||
|
|||||||
Reference in New Issue
Block a user