diff --git a/Toolkit.Foundation/Component.cs b/Toolkit.Foundation/Component.cs index efa64ef..47934f9 100644 --- a/Toolkit.Foundation/Component.cs +++ b/Toolkit.Foundation/Component.cs @@ -2,17 +2,16 @@ namespace Toolkit.Foundation; + public class Component : IComponent { private readonly IComponentBuilder builder; - protected Component(IComponentBuilder builder) - { + protected Component(IComponentBuilder builder) => this.builder = builder; - } - public static TComponent? Register(IServiceProvider provider, + public static TComponent? Create(IServiceProvider provider, Action builderDelegate) where TComponent : class, IComponent { @@ -23,8 +22,13 @@ public class Component : return factory.Create(builder); } + return default; } - public virtual IComponentBuilder Create() => builder; + public IComponentBuilder Configure(string name) => + Configuring(name, builder); + + public virtual IComponentBuilder Configuring(string name, + IComponentBuilder builder) => builder; } \ No newline at end of file diff --git a/Toolkit.Foundation/ComponentBuilder.cs b/Toolkit.Foundation/ComponentBuilder.cs index cf12543..135eddd 100644 --- a/Toolkit.Foundation/ComponentBuilder.cs +++ b/Toolkit.Foundation/ComponentBuilder.cs @@ -9,23 +9,7 @@ public class ComponentBuilder : { private readonly IHostBuilder hostBuilder; - private ContentRootConfiguration rootConfiguration = new(); - - public void SetRootConfiguration(Action configurationDelegate) - { - ContentRootConfiguration rootConfiguration = new(); - configurationDelegate.Invoke(rootConfiguration); - - this.rootConfiguration = rootConfiguration; - } - - public void SetAppConfiguration(Action rootConfigurationDelegate) - { - ContentRootConfiguration rootConfiguration = new(); - rootConfigurationDelegate.Invoke(rootConfiguration); - - this.rootConfiguration = rootConfiguration; - } + private ComponentContentConfiguration configuration = new(); private ComponentBuilder() { @@ -61,8 +45,7 @@ public class ComponentBuilder : }); } - public static IComponentBuilder Create() => - new ComponentBuilder(); + public static IComponentBuilder Create() => new ComponentBuilder(); public IComponentBuilder AddConfiguration(Action configurationDelegate) where TConfiguration : ComponentConfiguration, new() @@ -104,13 +87,21 @@ public class ComponentBuilder : public IComponentHost Build() { - hostBuilder.UseContentRoot(rootConfiguration.ContentRoot, true) + hostBuilder.UseContentRoot(configuration.ContentRoot, true) .ConfigureAppConfiguration(config => { - config.AddJsonFile(rootConfiguration.JsonFileName, true, true); + config.AddJsonFile(configuration.JsonFileName, true, true); }); IHost host = hostBuilder.Build(); return host.Services.GetRequiredService(); } + + public void SetComponentConfiguration(Action configurationDelegate) + { + ComponentContentConfiguration configuration = new(); + configurationDelegate.Invoke(configuration); + + this.configuration = configuration; + } } \ No newline at end of file diff --git a/Toolkit.Foundation/ContentRootConfiguration.cs b/Toolkit.Foundation/ComponentContentConfiguration.cs similarity index 78% rename from Toolkit.Foundation/ContentRootConfiguration.cs rename to Toolkit.Foundation/ComponentContentConfiguration.cs index 54c3242..8f3f91b 100644 --- a/Toolkit.Foundation/ContentRootConfiguration.cs +++ b/Toolkit.Foundation/ComponentContentConfiguration.cs @@ -1,6 +1,6 @@ namespace Toolkit.Foundation; -public record ContentRootConfiguration +public record ComponentContentConfiguration { public string ContentRoot { get; set; } = "Local"; diff --git a/Toolkit.Foundation/ComponentFactory.cs b/Toolkit.Foundation/ComponentFactory.cs index 37e59c5..872e55e 100644 --- a/Toolkit.Foundation/ComponentFactory.cs +++ b/Toolkit.Foundation/ComponentFactory.cs @@ -7,16 +7,16 @@ public class ComponentFactory(IServiceProvider provider, IComponentScopeCollection scopes) : IComponentFactory { - public IComponentHost? Create(string name, + public IComponentHost? Create(string key, TConfiguration? configuration = null, Action? servicesDelegate = null) where TComponent : IComponent - where TConfiguration : ComponentConfiguration, new() + where TConfiguration : + ComponentConfiguration, new() { if (provider.GetRequiredService() is TComponent component) { - IComponentBuilder builder = component.Create(); - + IComponentBuilder builder = component.Configure(key); builder.AddServices(services => { services.AddTransient(_ => @@ -38,7 +38,7 @@ 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) { @@ -46,10 +46,10 @@ public class ComponentFactory(IServiceProvider provider, } }); - builder.AddConfiguration(name, configuration); + builder.AddConfiguration(key, configuration); IComponentHost host = builder.Build(); - scopes.Add(new ComponentScopeDescriptor(name, + scopes.Add(new ComponentScopeDescriptor(key, host.Services.GetRequiredService())); return host; diff --git a/Toolkit.Foundation/ComponentInitializer.cs b/Toolkit.Foundation/ComponentInitializer.cs index f5c183e..856cdd0 100644 --- a/Toolkit.Foundation/ComponentInitializer.cs +++ b/Toolkit.Foundation/ComponentInitializer.cs @@ -13,7 +13,7 @@ public class ComponentInitializer(IEnumerable components, { foreach (IComponent component in components) { - IComponentBuilder builder = component.Create(); + IComponentBuilder builder = component.Configure(""); builder.AddServices(services => { services.AddTransient(_ => diff --git a/Toolkit.Foundation/IComponent.cs b/Toolkit.Foundation/IComponent.cs index 9165c0f..06accd6 100644 --- a/Toolkit.Foundation/IComponent.cs +++ b/Toolkit.Foundation/IComponent.cs @@ -2,5 +2,5 @@ public interface IComponent { - IComponentBuilder Create(); + IComponentBuilder Configure(string key); } \ No newline at end of file diff --git a/Toolkit.Foundation/IComponentBuilder.cs b/Toolkit.Foundation/IComponentBuilder.cs index a383377..fc8fafa 100644 --- a/Toolkit.Foundation/IComponentBuilder.cs +++ b/Toolkit.Foundation/IComponentBuilder.cs @@ -14,7 +14,9 @@ public interface IComponentBuilder IComponentBuilder AddConfiguration(string section) where TConfiguration : ComponentConfiguration, new(); + IComponentBuilder AddServices(Action configureDelegate); + IComponentHost Build(); - IComponentBuilder AddServices(Action configureDelegate); + void SetComponentConfiguration(Action configurationDelegate); } \ No newline at end of file diff --git a/Toolkit.Foundation/NamedComponent.cs b/Toolkit.Foundation/NamedComponent.cs index 3c7b4d7..82c401c 100644 --- a/Toolkit.Foundation/NamedComponent.cs +++ b/Toolkit.Foundation/NamedComponent.cs @@ -1,6 +1,6 @@ namespace Toolkit.Foundation; -public record NamedComponent(string Name) +public record NamedComponent(string Key) { - public override string ToString() => Name; + public override string ToString() => Key; } \ No newline at end of file diff --git a/Toolkit.Foundation/NavigateHandler.cs b/Toolkit.Foundation/NavigateHandler.cs index 5b82db9..b1eec14 100644 --- a/Toolkit.Foundation/NavigateHandler.cs +++ b/Toolkit.Foundation/NavigateHandler.cs @@ -28,7 +28,7 @@ public class NavigateHandler(NamedComponent scope, if (navigation is null) { - ComponentScopeDescriptor? descriptor = componentScopeProvider.Get(args.Scope ?? scope.Name); + ComponentScopeDescriptor? descriptor = componentScopeProvider.Get(args.Scope ?? scope.Key); navigation = descriptor?.Services?.GetRequiredService(); }