From 2a9b52ebe1d01645db70cc5f5fdcea1d9e21660d Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 27 Nov 2025 09:58:49 +0100 Subject: [PATCH] Ensure required services are configured in DI setup Added `EnsureRequiredServices` to validate required services in `ConfigurationOptions`, throwing an exception if any are missing. Introduced `_requiredServices` dictionary to track configuration status for `ConfigureRecActions` and `LuckyPennySoftwareLicenseKey`. Updated property and methods to mark services as configured. Integrated validation into `AddRecServices` to enforce proper setup. --- src/ReC.Application/DependencyInjection.cs | 32 +++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ReC.Application/DependencyInjection.cs b/src/ReC.Application/DependencyInjection.cs index 070a261..239eda9 100644 --- a/src/ReC.Application/DependencyInjection.cs +++ b/src/ReC.Application/DependencyInjection.cs @@ -11,6 +11,9 @@ public static class DependencyInjection { var configOpt = new ConfigurationOptions(); options.Invoke(configOpt); + + configOpt.EnsureRequiredServices(); + configOpt.ApplyConfigurations(services); services.AddAutoMapper(cfg => @@ -34,6 +37,21 @@ public static class DependencyInjection { private readonly Queue> _configActions = new(); + private readonly Dictionary _requiredServices = new() + { + { nameof(ConfigureRecActions), false }, + { nameof(LuckyPennySoftwareLicenseKey), false } + }; + + internal void EnsureRequiredServices() + { + var missingServices = _requiredServices + .Where(kvp => !kvp.Value) + .Select(kvp => kvp.Key.Replace("Configure", string.Empty)); + if (missingServices.Any()) + throw new InvalidOperationException($"The following required services were not configured: {string.Join(", ", missingServices)}"); + } + internal void ApplyConfigurations(IServiceCollection services) { while (_configActions.Count > 0) @@ -43,17 +61,29 @@ public static class DependencyInjection } } - public string? LuckyPennySoftwareLicenseKey { get; set; } + private string? _luckyPennySoftwareLicenseKey; + + public string? LuckyPennySoftwareLicenseKey + { + get => _luckyPennySoftwareLicenseKey; + set + { + _luckyPennySoftwareLicenseKey = value; + _requiredServices[nameof(LuckyPennySoftwareLicenseKey)] = true; + } + } public ConfigurationOptions ConfigureRecActions(Action configure) { _configActions.Enqueue(services => services.Configure(configure)); + _requiredServices[nameof(ConfigureRecActions)] = true; return this; } public ConfigurationOptions ConfigureRecActions(IConfiguration configuration) { _configActions.Enqueue(services => services.Configure(configuration)); + _requiredServices[nameof(ConfigureRecActions)] = true; return this; } }