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,33 @@
#region ========================================================================= USING =====================================================================================
using System.Reflection;
#endregion
namespace Lumina.Presentation.Api.Common.DependencyInjection;
/// <summary>
/// Class containing the configuration bindings of the Presentation API layer.
/// </summary>
internal static class PresentationApiLayerConfiguration
{
#region ================================================================== METHODS ===================================================================================
/// <summary>
/// Extension method for adding the Presentation API layer 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 BindPresentationApiLayerConfiguration(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.json", optional: false, reloadOnChange: true);
configuration.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true);
return services;
}
#endregion
}
@@ -21,7 +21,7 @@ public static class PresentationApiLayerServices
/// Registers the services of the Presentation API layer into the dependency injection container.
/// </summary>
/// <param name="services">The service collection to add the services to.</param>
/// <returns>The service collection with the added services.</returns>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddPresentationApiLayerServices(this IServiceCollection services)
{
// add services to the container
@@ -20,6 +20,16 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<DependentUpon>appsettings.shared.json</DependentUpon>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lumina.Application\Lumina.Application.csproj" />
+5
View File
@@ -1,6 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Api.Common.DependencyInjection;
using Lumina.Application.Common.DependencyInjection;
using Lumina.Infrastructure.Common.DependencyInjection;
#endregion
namespace Lumina.Presentation.Api;
@@ -19,8 +20,12 @@ public class Program
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.BindSharedConfiguration(builder.Configuration);
builder.Services.BindPresentationApiLayerConfiguration(builder.Configuration);
builder.Services.AddApplicationLayerServices();
builder.Services.AddPresentationApiLayerServices();
builder.Services.AddInfrastructureLayerServices();
var app = builder.Build();
+17 -5
View File
@@ -1,9 +1,21 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
"Serilog": {
"WriteTo": [
{
"Name": "Debug"
},
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "Logs/lumina_api_.log",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
}
}
]
},
"AllowedHosts": "*"
}