From 55756b7093b4fe5ec9108c829c93306eb07d9466 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Tue, 8 Oct 2024 09:43:09 +0100 Subject: [PATCH] Added ConfigurationBuilder --- Toolkit.Foundation/Component.cs | 39 ++++++++++----------- Toolkit.Foundation/ComponentFactory.cs | 18 ++++++---- Toolkit.Foundation/ConfigurationBuilder.cs | 10 ++++++ Toolkit.Foundation/IComponent.cs | 10 ++---- Toolkit.Foundation/IComponentFactory.cs | 6 ++-- Toolkit.Foundation/IConfigurationBuilder.cs | 8 +++++ 6 files changed, 55 insertions(+), 36 deletions(-) create mode 100644 Toolkit.Foundation/ConfigurationBuilder.cs create mode 100644 Toolkit.Foundation/IConfigurationBuilder.cs diff --git a/Toolkit.Foundation/Component.cs b/Toolkit.Foundation/Component.cs index 8e8ba85..d5d3128 100644 --- a/Toolkit.Foundation/Component.cs +++ b/Toolkit.Foundation/Component.cs @@ -22,38 +22,37 @@ public class Component : return factory.Create(builder); } - public IComponentBuilder Configure(string name, - Action? builderDelegate = null) - { - if (builderDelegate is not null) - { - builderDelegate(builder); - } - - return Configuring(builder); - } - - public IComponentBuilder Configure(Action? builderDelegate = null) + public IComponentBuilder Configure(Action? componentDelegate = null) { - if (builderDelegate is not null) + if (componentDelegate is not null) { - builderDelegate(builder); + componentDelegate(builder); } return Configuring(builder); } - public IComponentBuilder Configure(string name, - TConfiguration? configuration = null, - Action? builderDelegate = null) + public IComponentBuilder Configure(Action>? configurationDelegate = null, + Action? 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 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); } diff --git a/Toolkit.Foundation/ComponentFactory.cs b/Toolkit.Foundation/ComponentFactory.cs index 9402fb2..77221a2 100644 --- a/Toolkit.Foundation/ComponentFactory.cs +++ b/Toolkit.Foundation/ComponentFactory.cs @@ -7,16 +7,17 @@ public class ComponentFactory(IServiceProvider provider, IComponentScopeCollection scopes) : IComponentFactory { - public IComponentHost? Create(string name, - TConfiguration? configuration = null, - Action? builderDelegate = null, + public IComponentHost? Create(string key, + Action>? configurationDelegate = null, + Action? componentDelegate = null, Action? servicesDelegate = null) where TComponent : IComponent where TConfiguration : class, new() { if (provider.GetRequiredService() 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()); 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())); return host; diff --git a/Toolkit.Foundation/ConfigurationBuilder.cs b/Toolkit.Foundation/ConfigurationBuilder.cs new file mode 100644 index 0000000..33caad6 --- /dev/null +++ b/Toolkit.Foundation/ConfigurationBuilder.cs @@ -0,0 +1,10 @@ +namespace Toolkit.Foundation; + +public class ConfigurationBuilder : + IConfigurationBuilder + where TConfiguration : class +{ + public string? Section { get; set; } + + public TConfiguration? Configuration { get; set; } +} diff --git a/Toolkit.Foundation/IComponent.cs b/Toolkit.Foundation/IComponent.cs index 06016f4..47b2932 100644 --- a/Toolkit.Foundation/IComponent.cs +++ b/Toolkit.Foundation/IComponent.cs @@ -2,13 +2,9 @@ public interface IComponent { - IComponentBuilder Configure(string name, - Action? builderDelegate = null); + IComponentBuilder Configure(Action? configurationDelegate = null); - IComponentBuilder Configure(Action? builderDelegate = null); - - IComponentBuilder Configure(string name, - TConfiguration? configuration = null, - Action? builderDelegate = null) + IComponentBuilder Configure(Action>? configurationDelegate = null, + Action? componentDelegate = null) where TConfiguration : class, new(); } \ No newline at end of file diff --git a/Toolkit.Foundation/IComponentFactory.cs b/Toolkit.Foundation/IComponentFactory.cs index aa50763..1686f95 100644 --- a/Toolkit.Foundation/IComponentFactory.cs +++ b/Toolkit.Foundation/IComponentFactory.cs @@ -4,9 +4,9 @@ namespace Toolkit.Foundation; public interface IComponentFactory { - IComponentHost? Create(string name, - TConfiguration? configuration = null, - Action? builderDelegate = null, + IComponentHost? Create(string key, + Action>? configurationDelegate = null, + Action? componentDelegate = null, Action? servicesDelegate = null) where TComponent : IComponent where TConfiguration : class, new(); diff --git a/Toolkit.Foundation/IConfigurationBuilder.cs b/Toolkit.Foundation/IConfigurationBuilder.cs new file mode 100644 index 0000000..5f795f7 --- /dev/null +++ b/Toolkit.Foundation/IConfigurationBuilder.cs @@ -0,0 +1,8 @@ +namespace Toolkit.Foundation; + +public interface IConfigurationBuilder +{ + string? Section { get; set; } + + TConfiguration? Configuration { get; set; } +}