mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 05:11:10 +00:00
Adds strongly typed IOptions<T> binding, with Fluent Validation and shared appsettings
- Adds strongly typed binding for IOptions<T> to map configuration sections from appsettings.json. - Integrates FluentValidation for validating bound properties. - Introduces appsettings.shared.json for shared configurations across applications. Updates application-specific appsettings.json to focus on individual settings.
This commit is contained in:
1 parent
b9dcb831e9
commit
433f0c2ad5
17 files changed
+383
-7
No files matched your search
@@ -0,0 +1,48 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Reflection;
|
||||
using Lumina.Infrastructure.Common.Models.Configuration;
|
||||
using Lumina.Infrastructure.Common.Utilities;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Common.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
/// Class containing the shared configuration bindings of the application.
|
||||
/// </summary>
|
||||
public static class SharedConfiguration
|
||||
{
|
||||
#region ================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Extension method for registering the shared configuration to the DI container.
|
||||
/// </summary>
|
||||
/// <param name="services">The <see cref="IServiceCollection"/> to add the configuration to.</param>
|
||||
/// <param name="configuration">The configuration manager to use for the configuration.</param>
|
||||
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
||||
/// <exception cref="DirectoryNotFoundException">Thrown when the base path where the configuration files should be located, does not exist.</exception>
|
||||
public static IServiceCollection BindSharedConfiguration(this IServiceCollection services, IConfigurationManager configuration)
|
||||
{
|
||||
var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
if (!Directory.Exists(basePath))
|
||||
throw new DirectoryNotFoundException($"The base path '{basePath}' does not exist.");
|
||||
configuration.SetBasePath(basePath);
|
||||
configuration.AddJsonFile("appsettings.shared.json", optional: false, reloadOnChange: true);
|
||||
configuration.AddJsonFile("appsettings.shared.Development.json", optional: true, reloadOnChange: true);
|
||||
configuration.AddEnvironmentVariables(); // environment variables should override the configuration files
|
||||
|
||||
// bind the common settings section
|
||||
services.AddOptions<CommonSettingsModel>()
|
||||
.Bind(configuration.GetRequiredSection(CommonSettingsModel.SectionName))
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
|
||||
// bind the database connection string section
|
||||
services.AddOptions<DatabaseSettingsModel>()
|
||||
.Bind(configuration.GetRequiredSection(DatabaseSettingsModel.SectionName))
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
return services;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user