Add support for configuring DbModelOptions via DI
Introduce ConfigureDbModel methods to DependencyInjection for setting up DbModelOptions from code or configuration. Update required services tracking and add usage in Program.cs to enable structured DbModelOptions injection.
This commit is contained in:
@@ -38,6 +38,7 @@ try
|
||||
options.LuckyPennySoftwareLicenseKey = builder.Configuration["LuckyPennySoftwareLicenseKey"];
|
||||
options.ConfigureRecActions(config.GetSection("RecAction"));
|
||||
options.ConfigureSqlException(config.GetSection("SqlException"));
|
||||
options.ConfigureDbModel(config.GetSection("DbModel"));
|
||||
});
|
||||
|
||||
builder.Services.AddRecInfrastructure(options =>
|
||||
|
||||
@@ -6,6 +6,7 @@ using ReC.Application.Common.Behaviors;
|
||||
using ReC.Application.Common.Behaviors.InvokeAction;
|
||||
using ReC.Application.Common.Constants;
|
||||
using ReC.Application.Common.Options;
|
||||
using ReC.Application.Common.Options.DbModel;
|
||||
using ReC.Application.RecActions.Commands;
|
||||
using System.Reflection;
|
||||
|
||||
@@ -56,7 +57,8 @@ public static class DependencyInjection
|
||||
private readonly Dictionary<string, bool> _requiredServices = new()
|
||||
{
|
||||
{ nameof(ConfigureRecActions), false },
|
||||
{ nameof(LuckyPennySoftwareLicenseKey), false }
|
||||
{ nameof(LuckyPennySoftwareLicenseKey), false },
|
||||
{ nameof(ConfigureDbModel), false }
|
||||
};
|
||||
|
||||
internal void EnsureRequiredServices()
|
||||
@@ -138,5 +140,29 @@ public static class DependencyInjection
|
||||
return this;
|
||||
}
|
||||
#endregion ConfigureSqlException
|
||||
|
||||
#region ConfigureDbModel
|
||||
public ConfigurationOptions ConfigureDbModel(Action<DbModelOptions> configure)
|
||||
{
|
||||
_configActions.Enqueue(services => services.Configure(configure));
|
||||
|
||||
if (_requiredServices[nameof(ConfigureDbModel)])
|
||||
throw new InvalidOperationException("DbModelOptions have already been configured.");
|
||||
_requiredServices[nameof(ConfigureDbModel)] = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConfigurationOptions ConfigureDbModel(IConfiguration configuration)
|
||||
{
|
||||
_configActions.Enqueue(services => services.Configure<DbModelOptions>(configuration));
|
||||
|
||||
if (_requiredServices[nameof(ConfigureDbModel)])
|
||||
throw new InvalidOperationException("DbModelOptions have already been configured.");
|
||||
_requiredServices[nameof(ConfigureDbModel)] = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
#endregion ConfigureDbModel
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user