From ad2a5240d2e1f94bc61bd2cfa347c1ece6594715 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 4 Nov 2026 14:53:01 +0100 Subject: [PATCH] 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. --- .../DependencyInjection.cs | 19 ++++++++----- .../MessagingServicePublisherConfiguration.cs | 27 ------------------- .../EmailSender.cs | 17 +++++++----- .../Integration/SendingEmailPublisherTests.cs | 2 +- 4 files changed, 23 insertions(+), 42 deletions(-) delete mode 100644 src/infrastructure/DigitalData.MessagingService.Publisher/MessagingServicePublisherConfiguration.cs diff --git a/src/infrastructure/DigitalData.MessagingService.Publisher/DependencyInjection.cs b/src/infrastructure/DigitalData.MessagingService.Publisher/DependencyInjection.cs index 757c0a9..4b0f992 100644 --- a/src/infrastructure/DigitalData.MessagingService.Publisher/DependencyInjection.cs +++ b/src/infrastructure/DigitalData.MessagingService.Publisher/DependencyInjection.cs @@ -7,15 +7,20 @@ namespace DigitalData.MessagingService.Publisher; public static class DependencyInjection { - public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services, Action? configure = null) + public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services) { - var configuration = new MessagingServicePublisherConfiguration(services); + return services.AddSingleton(); + } - configure?.Invoke(configuration); + public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services, Action configureRabbitMqConnection) + { + services.AddRabbitMqConnectionFactory(configureRabbitMqConnection); + return services.AddMessagingServicePublisher(); + } - // --- Email Queue (RabbitMQ) --- - services.AddSingleton(); - - return services; + public static IServiceCollection AddMessagingServicePublisher(this IServiceCollection services, IConfiguration configuration, string rabbitMqConnectionSectionName = RabbitMqConnectionConfiguration.SectionName) + { + services.AddRabbitMqConnectionFactory(configuration.GetSection(rabbitMqConnectionSectionName)); + return services.AddMessagingServicePublisher(); } } diff --git a/src/infrastructure/DigitalData.MessagingService.Publisher/MessagingServicePublisherConfiguration.cs b/src/infrastructure/DigitalData.MessagingService.Publisher/MessagingServicePublisherConfiguration.cs deleted file mode 100644 index 3e4fc13..0000000 --- a/src/infrastructure/DigitalData.MessagingService.Publisher/MessagingServicePublisherConfiguration.cs +++ /dev/null @@ -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 configure) - { - Services.AddRabbitMqConnectionFactory(configure); - return this; - } - - public MessagingServicePublisherConfiguration AddRabbitMqConnectionFactory(IConfiguration configuration, string sectionName = RabbitMqConfiguration.SectionName) - { - Services.AddRabbitMqConnectionFactory(configuration, sectionName); - return this; - } -} diff --git a/src/presentation/DigitalData.MessagingService.Client/EmailSender.cs b/src/presentation/DigitalData.MessagingService.Client/EmailSender.cs index 9b7842e..99d7fc9 100644 --- a/src/presentation/DigitalData.MessagingService.Client/EmailSender.cs +++ b/src/presentation/DigitalData.MessagingService.Client/EmailSender.cs @@ -13,14 +13,17 @@ namespace DigitalData.MessagingService.Client; /// /// This class manages its own internal using a /// pattern so the DI container is only built once, -/// on the first call to . +/// on the first call to . /// +#if NET +[Obsolete("Use MessagingServiceClient with dependency injection instead.")] +#endif public static class EmailSender { /// /// Internal event used to accumulate service registrations before the /// is built. Handlers are added by - /// and invoked exactly once during + /// and invoked exactly once during /// lazy initialization. /// private static event Action ConfigureServices = delegate { }; @@ -43,7 +46,7 @@ public static class EmailSender /// and the internal has been initialized. /// /// - /// if has been called + /// if has been called /// and the provider is built; otherwise . /// public static bool IsConnected => LazyProvider.IsValueCreated; @@ -53,7 +56,7 @@ public static class EmailSender /// the internal dependency injection container. /// /// - /// A delegate used to configure the , + /// A delegate used to configure the , /// such as host, port, credentials, and exchange settings. /// /// @@ -64,12 +67,12 @@ public static class EmailSender /// Thrown when the service is already connected and /// is . /// - public static void ConnectRabbitMq(Action configure, OnReconnect onReconnect = OnReconnect.ThrowException) + public static void ConnectRabbitMq(Action configure, OnReconnect onReconnect = OnReconnect.ThrowException) { if(IsConnected && onReconnect == OnReconnect.ThrowException) 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 } @@ -111,7 +114,7 @@ public static class EmailSender /// /// The outgoing email data to enqueue. /// - /// Thrown when has not been called prior to sending. + /// Thrown when has not been called prior to sending. /// /// /// This method maps to , diff --git a/tests/DigitalData.MessagingService.Tests/Integration/SendingEmailPublisherTests.cs b/tests/DigitalData.MessagingService.Tests/Integration/SendingEmailPublisherTests.cs index b9f393a..6cc5328 100644 --- a/tests/DigitalData.MessagingService.Tests/Integration/SendingEmailPublisherTests.cs +++ b/tests/DigitalData.MessagingService.Tests/Integration/SendingEmailPublisherTests.cs @@ -24,7 +24,7 @@ public sealed class SendingEmailPublisherTests : IAsyncDisposable { var services = new ServiceCollection(); services.AddLogging(); - services.AddMessagingServicePublisher(cfg => cfg.AddRabbitMqConnectionFactory(RabbitMqTestConfig.Apply)); + services.AddMessagingServicePublisher(RabbitMqTestConfig.Apply); _serviceProvider = services.BuildServiceProvider(); _publisher = _serviceProvider.GetRequiredService();