From 969c740f2d9850eac2114245f0455a519cf48e19 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sun, 21 Apr 2024 21:10:26 +0100 Subject: [PATCH] WIP --- Toolkit.Foundation/ComponentBuilder.cs | 33 ++++++++++++++----- Toolkit.Foundation/Configuration.cs | 5 ++- .../ConfigurationInitializer.cs | 4 +-- Toolkit.Foundation/ConfigurationSource.cs | 4 +-- Toolkit.Foundation/IComponentBuilder.cs | 7 ++++ Toolkit.Foundation/IConfiguration.cs | 2 ++ .../IServiceCollectionExtensions.cs | 24 +++++++------- 7 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Toolkit.Foundation/ComponentBuilder.cs b/Toolkit.Foundation/ComponentBuilder.cs index eef2252..3afcb6e 100644 --- a/Toolkit.Foundation/ComponentBuilder.cs +++ b/Toolkit.Foundation/ComponentBuilder.cs @@ -52,6 +52,15 @@ public class ComponentBuilder : public IComponentBuilder AddConfiguration(Action configurationDelegate) where TConfiguration : ComponentConfiguration, new() + { + AddConfiguration(typeof(TConfiguration).Name, configurationDelegate); + return this; + } + + public IComponentBuilder AddConfiguration(string section, + Action? configurationDelegate = null) + where TConfiguration : + ComponentConfiguration, new() { if (configurationRegistered) { @@ -68,8 +77,8 @@ public class ComponentBuilder : hostBuilder.ConfigureServices(services => { - services.AddConfiguration(section: configuration.GetType().Name, - configuration: configuration); + services.AddConfiguration(section: section, + defaultConfiguration: configuration); services.AddConfiguration(configuration); }); @@ -77,15 +86,23 @@ public class ComponentBuilder : return this; } + public IComponentBuilder AddConfiguration(string section) + where TConfiguration : + ComponentConfiguration, new() + { + AddConfiguration(section, null); + return this; + } + + public IComponentBuilder AddServices(Action configureDelegate) + { + hostBuilder.ConfigureServices(configureDelegate); + return this; + } + public IComponentHost Build() { IHost host = hostBuilder.Build(); return host.Services.GetRequiredService(); } - - public IComponentBuilder AddServices(Action configureDelegate) - { - hostBuilder.ConfigureServices(configureDelegate); - return this; - } } \ No newline at end of file diff --git a/Toolkit.Foundation/Configuration.cs b/Toolkit.Foundation/Configuration.cs index f577181..fc9249d 100644 --- a/Toolkit.Foundation/Configuration.cs +++ b/Toolkit.Foundation/Configuration.cs @@ -1,9 +1,12 @@ namespace Toolkit.Foundation; -public class Configuration(IConfigurationReader reader) : +public class Configuration(string section, + IConfigurationReader reader) : IConfiguration where TConfiguration : class { public TConfiguration Value => reader.Read(); + + public string Section => section; } diff --git a/Toolkit.Foundation/ConfigurationInitializer.cs b/Toolkit.Foundation/ConfigurationInitializer.cs index 537f9a5..f969c89 100644 --- a/Toolkit.Foundation/ConfigurationInitializer.cs +++ b/Toolkit.Foundation/ConfigurationInitializer.cs @@ -1,7 +1,6 @@ namespace Toolkit.Foundation; -public class ConfigurationInitializer(string section, - IConfigurationReader reader, +public class ConfigurationInitializer(IConfigurationReader reader, IConfigurationWriter writer, IConfigurationFactory factory, IPublisher publisher) : @@ -12,7 +11,6 @@ public class ConfigurationInitializer(string section, { public async Task Initialize() { - var d = section; if (!reader.TryRead(out TConfiguration? configuration)) { if (factory.Create() is object defaultConfiguration) diff --git a/Toolkit.Foundation/ConfigurationSource.cs b/Toolkit.Foundation/ConfigurationSource.cs index 18800d6..f9fd923 100644 --- a/Toolkit.Foundation/ConfigurationSource.cs +++ b/Toolkit.Foundation/ConfigurationSource.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.FileProviders; -using System; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Json.Nodes; @@ -154,7 +153,8 @@ public class ConfigurationSource(IConfigurationFile(currentNode[segments[lastIndex]], serializerOptions ?? defaultSerializerOptions()); + value = JsonSerializer.Deserialize(currentNode[segments[lastIndex]], + serializerOptions ?? defaultSerializerOptions()); return true; } } diff --git a/Toolkit.Foundation/IComponentBuilder.cs b/Toolkit.Foundation/IComponentBuilder.cs index d65586e..571f207 100644 --- a/Toolkit.Foundation/IComponentBuilder.cs +++ b/Toolkit.Foundation/IComponentBuilder.cs @@ -7,6 +7,13 @@ public interface IComponentBuilder IComponentBuilder AddConfiguration(Action configurationDelegate) where TConfiguration : ComponentConfiguration, new(); + IComponentBuilder AddConfiguration(string section, + Action? configurationDelegate = null) + where TConfiguration : ComponentConfiguration, new(); + + IComponentBuilder AddConfiguration(string section) + where TConfiguration : ComponentConfiguration, new(); + IComponentHost Build(); IComponentBuilder AddServices(Action configureDelegate); diff --git a/Toolkit.Foundation/IConfiguration.cs b/Toolkit.Foundation/IConfiguration.cs index cf4ef25..0af78a7 100644 --- a/Toolkit.Foundation/IConfiguration.cs +++ b/Toolkit.Foundation/IConfiguration.cs @@ -5,4 +5,6 @@ public interface IConfiguration class { TConfiguration Value { get; } + + string Section { get; } } diff --git a/Toolkit.Foundation/IServiceCollectionExtensions.cs b/Toolkit.Foundation/IServiceCollectionExtensions.cs index 10e5def..cba129e 100644 --- a/Toolkit.Foundation/IServiceCollectionExtensions.cs +++ b/Toolkit.Foundation/IServiceCollectionExtensions.cs @@ -51,11 +51,11 @@ public static class IServiceCollectionExtensions public static IServiceCollection AddConfiguration(this IServiceCollection services, string section) - where TConfiguration : class => + where TConfiguration : class, new() => services.AddConfiguration(section, "Settings.json", null); public static IServiceCollection AddConfiguration(this IServiceCollection services) - where TConfiguration : class => + where TConfiguration : class, new() => services.AddConfiguration(typeof(TConfiguration).Name, "Settings.json", null); public static IServiceCollection AddConfiguration(this IServiceCollection services, @@ -81,21 +81,21 @@ public static class IServiceCollectionExtensions public static IServiceCollection AddConfiguration(this IServiceCollection services, TConfiguration configuration) - where TConfiguration : class => + where TConfiguration : class, new() => services.AddConfiguration(configuration.GetType().Name, "Settings.json", configuration); public static IServiceCollection AddConfiguration(this IServiceCollection services, object configuration) - where TConfiguration : class => + where TConfiguration : class, new() => services.AddConfiguration(configuration.GetType().Name, "Settings.json", (TConfiguration?)configuration); public static IServiceCollection AddConfiguration(this IServiceCollection services, string section, string path = "Settings.json", - TConfiguration? configuration = null, + TConfiguration? defaultConfiguration = null, Action? serializerDelegate = null) - where TConfiguration : class + where TConfiguration : class, new() { services.TryAddSingleton>(provider => { @@ -132,11 +132,10 @@ public static class IServiceCollectionExtensions new ConfigurationWriter(provider.GetRequiredKeyedService>(key))); services.TryAddKeyedTransient>(section, (provider, key) => - new ConfigurationFactory(() => configuration ?? provider.GetRequiredService())); + new ConfigurationFactory(() => defaultConfiguration ?? new TConfiguration())); services.AddTransient>(provider => - new ConfigurationInitializer(section, - provider.GetRequiredKeyedService>(section), + new ConfigurationInitializer(provider.GetRequiredKeyedService>(section), provider.GetRequiredKeyedService>(section), provider.GetRequiredKeyedService>(section), provider.GetRequiredService())); @@ -146,8 +145,11 @@ public static class IServiceCollectionExtensions services.AddTransient, WritableConfiguration>(); - services.AddTransient, Configuration>(); - services.AddTransient(provider => provider.GetRequiredService>().Value); + services.TryAddKeyedTransient>(section, (provider, key) => + new Configuration(section, provider.GetRequiredKeyedService>(key))); + + services.AddTransient(provider => provider.GetRequiredKeyedService>(section)); + services.AddTransient(provider => provider.GetRequiredKeyedService>(section).Value); return services; }