From 977a20fd977f6329abdbf1fe7632d27b05124ec1 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 27 Jul 2026 17:10:45 +0200 Subject: [PATCH] Refactor RabbitMQ DI into extension method Refactored the registration of `RabbitMqConnectionFactory` and RabbitMQ configuration into a new `AddRabbitMqConnectionFactory` extension method for improved modularity and reusability. - Removed direct calls to `AddSingleton` and `Configure` from the main `DependencyInjection` class. - Added a new static `DependencyInjection` class under the `DigitalData.MessagingService.RabbitMQ` namespace. - The new `AddRabbitMqConnectionFactory` method encapsulates RabbitMQ DI logic and accepts `IServiceCollection` and `IConfiguration` as parameters. - Updated `DependencyInjection.cs` to use the new extension method. --- .../DependencyInjection.cs | 4 +-- .../DependencyInjection.cs | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 src/DigitalData.MessagingService.RabbitMQ/DependencyInjection.cs diff --git a/src/DigitalData.MessagingService.Infrastructure/DependencyInjection.cs b/src/DigitalData.MessagingService.Infrastructure/DependencyInjection.cs index a3ee67a..d5f1a6a 100644 --- a/src/DigitalData.MessagingService.Infrastructure/DependencyInjection.cs +++ b/src/DigitalData.MessagingService.Infrastructure/DependencyInjection.cs @@ -34,11 +34,9 @@ public static class DependencyInjection // --- Email Queue (RabbitMQ) --- services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); // --- RabbitMQ Configuration --- - services.Configure( - configuration.GetSection(RabbitMqConfiguration.SectionName)); + services.AddRabbitMqConnectionFactory(configuration); // --- Data Protection (for encryption) --- services.AddDataProtection() diff --git a/src/DigitalData.MessagingService.RabbitMQ/DependencyInjection.cs b/src/DigitalData.MessagingService.RabbitMQ/DependencyInjection.cs new file mode 100644 index 0000000..45514f4 --- /dev/null +++ b/src/DigitalData.MessagingService.RabbitMQ/DependencyInjection.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace DigitalData.MessagingService.RabbitMQ +{ + /// + /// Dependency injection configuration for Infrastructure layer + /// + public static class DependencyInjection + { + /// + /// Adds Infrastructure layer services to the DI container + /// + public static IServiceCollection AddRabbitMqConnectionFactory( + this IServiceCollection services, + IConfiguration configuration) + { + services.AddSingleton(); + + // --- RabbitMQ Configuration --- + services.Configure( + configuration.GetSection(RabbitMqConfiguration.SectionName)); + + return services; + } + } + +} \ No newline at end of file