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);
}
public IComponentBuilder Configure(string name,
Action<IComponentBuilder>? builderDelegate = null)
{
if (builderDelegate is not null)
{
builderDelegate(builder);
}
return Configuring(builder);
}
public IComponentBuilder Configure(Action<IComponentBuilder>? builderDelegate = null)
public IComponentBuilder Configure(Action<IComponentBuilder>? componentDelegate = null)
{
if (builderDelegate is not null)
if (componentDelegate is not null)
{
builderDelegate(builder);
componentDelegate(builder);
}
return Configuring(builder);
}
public IComponentBuilder Configure<TConfiguration>(string name,
TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null)
public IComponentBuilder Configure<TConfiguration>(Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
Action<IComponentBuilder>? componentDelegate = null)
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);
}
+12 -6
View File
@@ -7,16 +7,17 @@ public class ComponentFactory(IServiceProvider provider,
IComponentScopeCollection scopes) :
IComponentFactory
{
public IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null,
public IComponentHost? Create<TComponent, TConfiguration>(string key,
Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
Action<IComponentBuilder>? componentDelegate = null,
Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent
where TConfiguration : class, new()
{
if (provider.GetRequiredService<TComponent>() is TComponent component)
{
IComponentBuilder builder = component.Configure(name, configuration, builderDelegate);
IComponentBuilder builder = component.Configure(configurationDelegate,
componentDelegate);
builder.AddServices(services =>
{
@@ -45,12 +46,17 @@ public class ComponentFactory(IServiceProvider provider,
provider.GetRequiredService<IComponentScopeProvider>());
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();
scopes.Add(new ComponentScopeDescriptor(name,
scopes.Add(new ComponentScopeDescriptor(key,
host.Services.GetRequiredService<IServiceProvider>()));
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
{
IComponentBuilder Configure(string name,
Action<IComponentBuilder>? builderDelegate = null);
IComponentBuilder Configure(Action<IComponentBuilder>? configurationDelegate = null);
IComponentBuilder Configure(Action<IComponentBuilder>? builderDelegate = null);
IComponentBuilder Configure<TConfiguration>(string name,
TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null)
IComponentBuilder Configure<TConfiguration>(Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
Action<IComponentBuilder>? componentDelegate = null)
where TConfiguration : class, new();
}
+3 -3
View File
@@ -4,9 +4,9 @@ namespace Toolkit.Foundation;
public interface IComponentFactory
{
IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null,
IComponentHost? Create<TComponent, TConfiguration>(string key,
Action<IConfigurationBuilder<TConfiguration>>? configurationDelegate = null,
Action<IComponentBuilder>? componentDelegate = null,
Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent
where TConfiguration : class, new();
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IConfigurationBuilder<TConfiguration>
{
string? Section { get; set; }
TConfiguration? Configuration { get; set; }
}