From 8aa43db90954194f3c594027dbed282d049181f3 Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 27 Nov 2025 10:09:20 +0100 Subject: [PATCH] Refactor and enhance DependencyInjection class Organized the `DependencyInjection` class into regions for better code structure: `Required Services`, `Configuration`, `LuckyPennySoftwareLicenseKey`, and `ConfigureRecActions`. Added `_requiredServices` dictionary to track the configuration status of required services. Updated `LuckyPennySoftwareLicenseKey` to include a null check before marking it as configured. Moved `_configActions` to the `Configuration` region and added `ApplyConfigurations` to process queued configuration actions. Enhanced `ConfigureRecActions` to prevent duplicate configurations by throwing an `InvalidOperationException` if already configured. These changes improve code readability, maintainability, and robustness. --- src/ReC.Application/DependencyInjection.cs | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/ReC.Application/DependencyInjection.cs b/src/ReC.Application/DependencyInjection.cs index 239eda9..c964c3c 100644 --- a/src/ReC.Application/DependencyInjection.cs +++ b/src/ReC.Application/DependencyInjection.cs @@ -35,8 +35,8 @@ public static class DependencyInjection public class ConfigurationOptions { - private readonly Queue> _configActions = new(); + #region Required Services private readonly Dictionary _requiredServices = new() { { nameof(ConfigureRecActions), false }, @@ -51,7 +51,11 @@ public static class DependencyInjection if (missingServices.Any()) throw new InvalidOperationException($"The following required services were not configured: {string.Join(", ", missingServices)}"); } - + #endregion Required Services + + #region Configuration + private readonly Queue> _configActions = new(); + internal void ApplyConfigurations(IServiceCollection services) { while (_configActions.Count > 0) @@ -60,7 +64,9 @@ public static class DependencyInjection action(services); } } + #endregion Configuration + #region LuckyPennySoftwareLicenseKey private string? _luckyPennySoftwareLicenseKey; public string? LuckyPennySoftwareLicenseKey @@ -69,22 +75,34 @@ public static class DependencyInjection set { _luckyPennySoftwareLicenseKey = value; - _requiredServices[nameof(LuckyPennySoftwareLicenseKey)] = true; + if (value is not null) + _requiredServices[nameof(LuckyPennySoftwareLicenseKey)] = true; } } + #endregion LuckyPennySoftwareLicenseKey + #region ConfigureRecActions public ConfigurationOptions ConfigureRecActions(Action configure) { _configActions.Enqueue(services => services.Configure(configure)); + + if(_requiredServices[nameof(ConfigureRecActions)]) + throw new InvalidOperationException("RecActionOptions have already been configured."); _requiredServices[nameof(ConfigureRecActions)] = true; + return this; } public ConfigurationOptions ConfigureRecActions(IConfiguration configuration) { _configActions.Enqueue(services => services.Configure(configuration)); + + if (_requiredServices[nameof(ConfigureRecActions)]) + throw new InvalidOperationException("RecActionOptions have already been configured."); _requiredServices[nameof(ConfigureRecActions)] = true; + return this; } + #endregion ConfigureRecActions } }