From 5a5d3f80a9fd6b363dd5d0ccd59f5542d673c875 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Mon, 30 Sep 2024 21:54:28 +0100 Subject: [PATCH] Fixed issue where the builder delegate was called too late --- Toolkit.Foundation/Component.cs | 15 ++++++++++---- Toolkit.Foundation/ComponentFactory.cs | 23 ++++++---------------- Toolkit.Foundation/ComponentInitializer.cs | 2 +- Toolkit.Foundation/IComponent.cs | 3 ++- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Toolkit.Foundation/Component.cs b/Toolkit.Foundation/Component.cs index 68df7e3..ef0fa11 100644 --- a/Toolkit.Foundation/Component.cs +++ b/Toolkit.Foundation/Component.cs @@ -22,9 +22,16 @@ public class Component : return factory.Create(builder); } - public IComponentBuilder Configure(string name) => - Configuring(name, builder); + public IComponentBuilder Configure(string? name = null, + Action? builderDelegate = null) + { + if (builderDelegate is not null) + { + builderDelegate(builder); + } - public virtual IComponentBuilder Configuring(string name, - IComponentBuilder builder) => builder; + return Configuring(builder); + } + + public virtual IComponentBuilder Configuring(IComponentBuilder builder) => builder; } \ No newline at end of file diff --git a/Toolkit.Foundation/ComponentFactory.cs b/Toolkit.Foundation/ComponentFactory.cs index 9008a73..cd43e45 100644 --- a/Toolkit.Foundation/ComponentFactory.cs +++ b/Toolkit.Foundation/ComponentFactory.cs @@ -7,22 +7,16 @@ public class ComponentFactory(IServiceProvider provider, IComponentScopeCollection scopes) : IComponentFactory { - public IComponentHost? Create(string key, + public IComponentHost? Create(string name, TConfiguration? configuration = null, Action? builderDelegate = null, Action? servicesDelegate = null) where TComponent : IComponent - where TConfiguration : - class, new() + where TConfiguration : class, new() { if (provider.GetRequiredService() is TComponent component) { - IComponentBuilder builder = component.Configure(key); - - if (builderDelegate is not null) - { - builderDelegate(builder); - } + IComponentBuilder builder = component.Configure(name, builderDelegate); builder.AddServices(services => { @@ -45,18 +39,13 @@ public class ComponentFactory(IServiceProvider provider, provider.GetRequiredService()); services.AddRange(proxy.Services); - services.AddSingleton(new NamedComponent(key)); - - if (servicesDelegate is not null) - { - servicesDelegate(services); - } + services.AddSingleton(new NamedComponent(name)); }); - builder.AddConfiguration(key, configuration); + builder.AddConfiguration(name, configuration); IComponentHost host = builder.Build(); - scopes.Add(new ComponentScopeDescriptor(key, + scopes.Add(new ComponentScopeDescriptor(name, host.Services.GetRequiredService())); return host; diff --git a/Toolkit.Foundation/ComponentInitializer.cs b/Toolkit.Foundation/ComponentInitializer.cs index 50acd8c..2ee2b9c 100644 --- a/Toolkit.Foundation/ComponentInitializer.cs +++ b/Toolkit.Foundation/ComponentInitializer.cs @@ -14,7 +14,7 @@ public class ComponentInitializer(IEnumerable components, { foreach (IComponent component in components) { - IComponentBuilder builder = component.Configure(""); + IComponentBuilder builder = component.Configure(); builder.AddServices(services => { services.AddTransient(_ => diff --git a/Toolkit.Foundation/IComponent.cs b/Toolkit.Foundation/IComponent.cs index 06accd6..904e2bf 100644 --- a/Toolkit.Foundation/IComponent.cs +++ b/Toolkit.Foundation/IComponent.cs @@ -2,5 +2,6 @@ public interface IComponent { - IComponentBuilder Configure(string key); + IComponentBuilder Configure(string? name = null, + Action? builderDelegate = null); } \ No newline at end of file