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.
This commit is contained in:
parent
988d48581b
commit
2a9b52ebe1
@ -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<Action<IServiceCollection>> _configActions = new();
|
||||
|
||||
private readonly Dictionary<string, bool> _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<RecActionOptions> configure)
|
||||
{
|
||||
_configActions.Enqueue(services => services.Configure(configure));
|
||||
_requiredServices[nameof(ConfigureRecActions)] = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConfigurationOptions ConfigureRecActions(IConfiguration configuration)
|
||||
{
|
||||
_configActions.Enqueue(services => services.Configure<RecActionOptions>(configuration));
|
||||
_requiredServices[nameof(ConfigureRecActions)] = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user