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