Added ConfigurationBuilder

This commit is contained in:
TheXamlGuy
2024-10-08 09:43:09 +01:00
parent f47e3deee9
commit 55756b7093
6 changed files with 55 additions and 36 deletions
+19 -20
View File
@@ -22,38 +22,37 @@ public class Component :
return factory.Create<TComponent>(builder); return factory.Create<TComponent>(builder);
} }
public IComponentBuilder Configure(string name, public IComponentBuilder Configure(Action<IComponentBuilder>? componentDelegate = null)
Action<IComponentBuilder>? builderDelegate = null)
{
if (builderDelegate is not null)
{
builderDelegate(builder);
}
return Configuring(builder);
}
public IComponentBuilder Configure(Action<IComponentBuilder>? builderDelegate = null)
{ {
if (builderDelegate is not null) if (componentDelegate is not null)
{ {
builderDelegate(builder); componentDelegate(builder);
} }
return Configuring(builder); return Configuring(builder);
} }
public IComponentBuilder Configure<TConfiguration>(string name, public IComponentBuilder Configure<TConfiguration>(Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
TConfiguration? configuration = null, Action<IComponentBuilder>? componentDelegate = null)
Action<IComponentBuilder>? builderDelegate = null)
where TConfiguration : class, new() where TConfiguration : class, new()
{ {
if (builderDelegate is not null) if (componentDelegate is not null)
{ {
builderDelegate(builder); componentDelegate(builder);
}
if (configurationDelegate is not null)
{
ConfigurationBuilder<TConfiguration> configurationBuilder = new();
configurationDelegate(configurationBuilder);
if (configurationBuilder.Section is { Length: > 0 } section)
{
builder.AddConfiguration(section, configurationBuilder.Configuration ?? new TConfiguration());
}
} }
builder.AddConfiguration(name, configuration);
return Configuring(builder); return Configuring(builder);
} }
+12 -6
View File
@@ -7,16 +7,17 @@ public class ComponentFactory(IServiceProvider provider,
IComponentScopeCollection scopes) : IComponentScopeCollection scopes) :
IComponentFactory IComponentFactory
{ {
public IComponentHost? Create<TComponent, TConfiguration>(string name, public IComponentHost? Create<TComponent, TConfiguration>(string key,
TConfiguration? configuration = null, Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
Action<IComponentBuilder>? builderDelegate = null, Action<IComponentBuilder>? componentDelegate = null,
Action<IServiceCollection>? servicesDelegate = null) Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent where TComponent : IComponent
where TConfiguration : class, new() where TConfiguration : class, new()
{ {
if (provider.GetRequiredService<TComponent>() is TComponent component) if (provider.GetRequiredService<TComponent>() is TComponent component)
{ {
IComponentBuilder builder = component.Configure(name, configuration, builderDelegate); IComponentBuilder builder = component.Configure(configurationDelegate,
componentDelegate);
builder.AddServices(services => builder.AddServices(services =>
{ {
@@ -45,12 +46,17 @@ public class ComponentFactory(IServiceProvider provider,
provider.GetRequiredService<IComponentScopeProvider>()); provider.GetRequiredService<IComponentScopeProvider>());
services.AddRange(proxy.Services); services.AddRange(proxy.Services);
services.AddSingleton(new NamedComponent(name)); services.AddSingleton(new NamedComponent(key));
if (servicesDelegate is not null)
{
servicesDelegate(services);
}
}); });
IComponentHost host = builder.Build(); IComponentHost host = builder.Build();
scopes.Add(new ComponentScopeDescriptor(name, scopes.Add(new ComponentScopeDescriptor(key,
host.Services.GetRequiredService<IServiceProvider>())); host.Services.GetRequiredService<IServiceProvider>()));
return host; return host;
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public class ConfigurationBuilder<TConfiguration> :
IConfigurationBuilder<TConfiguration>
where TConfiguration : class
{
public string? Section { get; set; }
public TConfiguration? Configuration { get; set; }
}
+3 -7
View File
@@ -2,13 +2,9 @@
public interface IComponent public interface IComponent
{ {
IComponentBuilder Configure(string name, IComponentBuilder Configure(Action<IComponentBuilder>? configurationDelegate = null);
Action<IComponentBuilder>? builderDelegate = null);
IComponentBuilder Configure(Action<IComponentBuilder>? builderDelegate = null); IComponentBuilder Configure<TConfiguration>(Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
Action<IComponentBuilder>? componentDelegate = null)
IComponentBuilder Configure<TConfiguration>(string name,
TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null)
where TConfiguration : class, new(); where TConfiguration : class, new();
} }
+3 -3
View File
@@ -4,9 +4,9 @@ namespace Toolkit.Foundation;
public interface IComponentFactory public interface IComponentFactory
{ {
IComponentHost? Create<TComponent, TConfiguration>(string name, IComponentHost? Create<TComponent, TConfiguration>(string key,
TConfiguration? configuration = null, Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
Action<IComponentBuilder>? builderDelegate = null, Action<IComponentBuilder>? componentDelegate = null,
Action<IServiceCollection>? servicesDelegate = null) Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent where TComponent : IComponent
where TConfiguration : class, new(); where TConfiguration : class, new();
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IConfigurationBuilder<TConfiguration>
{
string? Section { get; set; }
TConfiguration? Configuration { get; set; }
}