WIP
This commit is contained in:
@@ -52,6 +52,15 @@ public class ComponentBuilder :
|
||||
|
||||
public IComponentBuilder AddConfiguration<TConfiguration>(Action<TConfiguration> configurationDelegate)
|
||||
where TConfiguration : ComponentConfiguration, new()
|
||||
{
|
||||
AddConfiguration(typeof(TConfiguration).Name, configurationDelegate);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IComponentBuilder AddConfiguration<TConfiguration>(string section,
|
||||
Action<TConfiguration>? configurationDelegate = null)
|
||||
where TConfiguration :
|
||||
ComponentConfiguration, new()
|
||||
{
|
||||
if (configurationRegistered)
|
||||
{
|
||||
@@ -68,8 +77,8 @@ public class ComponentBuilder :
|
||||
|
||||
hostBuilder.ConfigureServices(services =>
|
||||
{
|
||||
services.AddConfiguration<ComponentConfiguration>(section: configuration.GetType().Name,
|
||||
configuration: configuration);
|
||||
services.AddConfiguration<ComponentConfiguration>(section: section,
|
||||
defaultConfiguration: configuration);
|
||||
|
||||
services.AddConfiguration(configuration);
|
||||
});
|
||||
@@ -77,15 +86,23 @@ public class ComponentBuilder :
|
||||
return this;
|
||||
}
|
||||
|
||||
public IComponentBuilder AddConfiguration<TConfiguration>(string section)
|
||||
where TConfiguration :
|
||||
ComponentConfiguration, new()
|
||||
{
|
||||
AddConfiguration<TConfiguration>(section, null);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate)
|
||||
{
|
||||
hostBuilder.ConfigureServices(configureDelegate);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IComponentHost Build()
|
||||
{
|
||||
IHost host = hostBuilder.Build();
|
||||
return host.Services.GetRequiredService<IComponentHost>();
|
||||
}
|
||||
|
||||
public IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate)
|
||||
{
|
||||
hostBuilder.ConfigureServices(configureDelegate);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class Configuration<TConfiguration>(IConfigurationReader<TConfiguration> reader) :
|
||||
public class Configuration<TConfiguration>(string section,
|
||||
IConfigurationReader<TConfiguration> reader) :
|
||||
IConfiguration<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
public TConfiguration Value => reader.Read();
|
||||
|
||||
public string Section => section;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ConfigurationInitializer<TConfiguration>(string section,
|
||||
IConfigurationReader<TConfiguration> reader,
|
||||
public class ConfigurationInitializer<TConfiguration>(IConfigurationReader<TConfiguration> reader,
|
||||
IConfigurationWriter<TConfiguration> writer,
|
||||
IConfigurationFactory<TConfiguration> factory,
|
||||
IPublisher publisher) :
|
||||
@@ -12,7 +11,6 @@ public class ConfigurationInitializer<TConfiguration>(string section,
|
||||
{
|
||||
public async Task Initialize()
|
||||
{
|
||||
var d = section;
|
||||
if (!reader.TryRead(out TConfiguration? configuration))
|
||||
{
|
||||
if (factory.Create() is object defaultConfiguration)
|
||||
|
||||
@@ -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<TConfiguration>(IConfigurationFile<TConfigurati
|
||||
|
||||
if (currentNode is not null)
|
||||
{
|
||||
value = JsonSerializer.Deserialize<TConfiguration>(currentNode[segments[lastIndex]], serializerOptions ?? defaultSerializerOptions());
|
||||
value = JsonSerializer.Deserialize<TConfiguration>(currentNode[segments[lastIndex]],
|
||||
serializerOptions ?? defaultSerializerOptions());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,13 @@ public interface IComponentBuilder
|
||||
IComponentBuilder AddConfiguration<TConfiguration>(Action<TConfiguration> configurationDelegate)
|
||||
where TConfiguration : ComponentConfiguration, new();
|
||||
|
||||
IComponentBuilder AddConfiguration<TConfiguration>(string section,
|
||||
Action<TConfiguration>? configurationDelegate = null)
|
||||
where TConfiguration : ComponentConfiguration, new();
|
||||
|
||||
IComponentBuilder AddConfiguration<TConfiguration>(string section)
|
||||
where TConfiguration : ComponentConfiguration, new();
|
||||
|
||||
IComponentHost Build();
|
||||
|
||||
IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate);
|
||||
|
||||
@@ -5,4 +5,6 @@ public interface IConfiguration<out TConfiguration>
|
||||
class
|
||||
{
|
||||
TConfiguration Value { get; }
|
||||
|
||||
string Section { get; }
|
||||
}
|
||||
|
||||
@@ -51,11 +51,11 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
string section)
|
||||
where TConfiguration : class =>
|
||||
where TConfiguration : class, new() =>
|
||||
services.AddConfiguration<TConfiguration>(section, "Settings.json", null);
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services)
|
||||
where TConfiguration : class =>
|
||||
where TConfiguration : class, new() =>
|
||||
services.AddConfiguration<TConfiguration>(typeof(TConfiguration).Name, "Settings.json", null);
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
@@ -81,21 +81,21 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
TConfiguration configuration)
|
||||
where TConfiguration : class =>
|
||||
where TConfiguration : class, new() =>
|
||||
services.AddConfiguration(configuration.GetType().Name, "Settings.json", configuration);
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(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<TConfiguration>(this IServiceCollection services,
|
||||
string section,
|
||||
string path = "Settings.json",
|
||||
TConfiguration? configuration = null,
|
||||
TConfiguration? defaultConfiguration = null,
|
||||
Action<JsonSerializerOptions>? serializerDelegate = null)
|
||||
where TConfiguration : class
|
||||
where TConfiguration : class, new()
|
||||
{
|
||||
services.TryAddSingleton<IConfigurationFile<TConfiguration>>(provider =>
|
||||
{
|
||||
@@ -132,11 +132,10 @@ public static class IServiceCollectionExtensions
|
||||
new ConfigurationWriter<TConfiguration>(provider.GetRequiredKeyedService<IConfigurationSource<TConfiguration>>(key)));
|
||||
|
||||
services.TryAddKeyedTransient<IConfigurationFactory<TConfiguration>>(section, (provider, key) =>
|
||||
new ConfigurationFactory<TConfiguration>(() => configuration ?? provider.GetRequiredService<TConfiguration>()));
|
||||
new ConfigurationFactory<TConfiguration>(() => defaultConfiguration ?? new TConfiguration()));
|
||||
|
||||
services.AddTransient<IInitializer, ConfigurationInitializer<TConfiguration>>(provider =>
|
||||
new ConfigurationInitializer<TConfiguration>(section,
|
||||
provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(section),
|
||||
new ConfigurationInitializer<TConfiguration>(provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(section),
|
||||
provider.GetRequiredKeyedService<IConfigurationWriter<TConfiguration>>(section),
|
||||
provider.GetRequiredKeyedService<IConfigurationFactory<TConfiguration>>(section),
|
||||
provider.GetRequiredService<IPublisher>()));
|
||||
@@ -146,8 +145,11 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
|
||||
|
||||
services.AddTransient<IConfiguration<TConfiguration>, Configuration<TConfiguration>>();
|
||||
services.AddTransient(provider => provider.GetRequiredService<IConfiguration<TConfiguration>>().Value);
|
||||
services.TryAddKeyedTransient<IConfiguration<TConfiguration>>(section, (provider, key) =>
|
||||
new Configuration<TConfiguration>(section, provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(key)));
|
||||
|
||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfiguration<TConfiguration>>(section));
|
||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfiguration<TConfiguration>>(section).Value);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user