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:
BinaryAlley committed 2024-08-25 16:59:19 +03:00
1 parent b9dcb831e9
commit 433f0c2ad5
17 files changed
+383 -7

No files matched your search

@@ -0,0 +1,28 @@
#region ========================================================================= USING =====================================================================================
using System.Reflection;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
#endregion
namespace Lumina.Infrastructure.Common.DependencyInjection;
/// <summary>
/// Contains all services of the Infrastructure layer.
/// </summary>
public static class InfrastructureLayerServices
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Extension method for adding the Infrastructure layer services to the DI container.
/// </summary>
/// <param name="services">The service collection to add the services to.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddInfrastructureLayerServices(this IServiceCollection services)
{
// scan the current assembly for validators and add them to the DI container
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly(), ServiceLifetime.Singleton);
return services;
}
#endregion
}
@@ -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
}