Amend compoments to be keyed based

This commit is contained in:
TheXamlGuy
2024-06-29 11:23:45 +01:00
parent e19a963f8e
commit f921860fbe
9 changed files with 37 additions and 40 deletions
+9 -5
View File
@@ -2,17 +2,16 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public class Component : public class Component :
IComponent IComponent
{ {
private readonly IComponentBuilder builder; private readonly IComponentBuilder builder;
protected Component(IComponentBuilder builder) protected Component(IComponentBuilder builder) =>
{
this.builder = builder; this.builder = builder;
}
public static TComponent? Register<TComponent>(IServiceProvider provider, public static TComponent? Create<TComponent>(IServiceProvider provider,
Action<IComponentBuilder> builderDelegate) Action<IComponentBuilder> builderDelegate)
where TComponent : class, IComponent where TComponent : class, IComponent
{ {
@@ -23,8 +22,13 @@ public class Component :
return factory.Create<TComponent>(builder); return factory.Create<TComponent>(builder);
} }
return default; return default;
} }
public virtual IComponentBuilder Create() => builder; public IComponentBuilder Configure(string name) =>
Configuring(name, builder);
public virtual IComponentBuilder Configuring(string name,
IComponentBuilder builder) => builder;
} }
+12 -21
View File
@@ -9,23 +9,7 @@ public class ComponentBuilder :
{ {
private readonly IHostBuilder hostBuilder; private readonly IHostBuilder hostBuilder;
private ContentRootConfiguration rootConfiguration = new(); private ComponentContentConfiguration configuration = new();
public void SetRootConfiguration(Action<ContentRootConfiguration> configurationDelegate)
{
ContentRootConfiguration rootConfiguration = new();
configurationDelegate.Invoke(rootConfiguration);
this.rootConfiguration = rootConfiguration;
}
public void SetAppConfiguration(Action<ContentRootConfiguration> rootConfigurationDelegate)
{
ContentRootConfiguration rootConfiguration = new();
rootConfigurationDelegate.Invoke(rootConfiguration);
this.rootConfiguration = rootConfiguration;
}
private ComponentBuilder() private ComponentBuilder()
{ {
@@ -61,8 +45,7 @@ public class ComponentBuilder :
}); });
} }
public static IComponentBuilder Create() => public static IComponentBuilder Create() => new ComponentBuilder();
new ComponentBuilder();
public IComponentBuilder AddConfiguration<TConfiguration>(Action<TConfiguration> configurationDelegate) public IComponentBuilder AddConfiguration<TConfiguration>(Action<TConfiguration> configurationDelegate)
where TConfiguration : ComponentConfiguration, new() where TConfiguration : ComponentConfiguration, new()
@@ -104,13 +87,21 @@ public class ComponentBuilder :
public IComponentHost Build() public IComponentHost Build()
{ {
hostBuilder.UseContentRoot(rootConfiguration.ContentRoot, true) hostBuilder.UseContentRoot(configuration.ContentRoot, true)
.ConfigureAppConfiguration(config => .ConfigureAppConfiguration(config =>
{ {
config.AddJsonFile(rootConfiguration.JsonFileName, true, true); config.AddJsonFile(configuration.JsonFileName, true, true);
}); });
IHost host = hostBuilder.Build(); IHost host = hostBuilder.Build();
return host.Services.GetRequiredService<IComponentHost>(); return host.Services.GetRequiredService<IComponentHost>();
} }
public void SetComponentConfiguration(Action<ComponentContentConfiguration> configurationDelegate)
{
ComponentContentConfiguration configuration = new();
configurationDelegate.Invoke(configuration);
this.configuration = configuration;
}
} }
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record ContentRootConfiguration public record ComponentContentConfiguration
{ {
public string ContentRoot { get; set; } = "Local"; public string ContentRoot { get; set; } = "Local";
+7 -7
View File
@@ -7,16 +7,16 @@ 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, TConfiguration? configuration = null,
Action<IServiceCollection>? servicesDelegate = null) Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent where TComponent : IComponent
where TConfiguration : ComponentConfiguration, new() where TConfiguration :
ComponentConfiguration, new()
{ {
if (provider.GetRequiredService<TComponent>() is TComponent component) if (provider.GetRequiredService<TComponent>() is TComponent component)
{ {
IComponentBuilder builder = component.Create(); IComponentBuilder builder = component.Configure(key);
builder.AddServices(services => builder.AddServices(services =>
{ {
services.AddTransient(_ => services.AddTransient(_ =>
@@ -38,7 +38,7 @@ 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) 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(); 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;
+1 -1
View File
@@ -13,7 +13,7 @@ public class ComponentInitializer(IEnumerable<IComponent> components,
{ {
foreach (IComponent component in components) foreach (IComponent component in components)
{ {
IComponentBuilder builder = component.Create(); IComponentBuilder builder = component.Configure("");
builder.AddServices(services => builder.AddServices(services =>
{ {
services.AddTransient(_ => services.AddTransient(_ =>
+1 -1
View File
@@ -2,5 +2,5 @@
public interface IComponent public interface IComponent
{ {
IComponentBuilder Create(); IComponentBuilder Configure(string key);
} }
+3 -1
View File
@@ -14,7 +14,9 @@ public interface IComponentBuilder
IComponentBuilder AddConfiguration<TConfiguration>(string section) IComponentBuilder AddConfiguration<TConfiguration>(string section)
where TConfiguration : ComponentConfiguration, new(); where TConfiguration : ComponentConfiguration, new();
IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate);
IComponentHost Build(); IComponentHost Build();
IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate); void SetComponentConfiguration(Action<ComponentContentConfiguration> configurationDelegate);
} }
+2 -2
View File
@@ -1,6 +1,6 @@
namespace Toolkit.Foundation; namespace Toolkit.Foundation;
public record NamedComponent(string Name) public record NamedComponent(string Key)
{ {
public override string ToString() => Name; public override string ToString() => Key;
} }
+1 -1
View File
@@ -28,7 +28,7 @@ public class NavigateHandler(NamedComponent scope,
if (navigation is null) if (navigation is null)
{ {
ComponentScopeDescriptor? descriptor = componentScopeProvider.Get(args.Scope ?? scope.Name); ComponentScopeDescriptor? descriptor = componentScopeProvider.Get(args.Scope ?? scope.Key);
navigation = descriptor?.Services?.GetRequiredService<INavigation>(); navigation = descriptor?.Services?.GetRequiredService<INavigation>();
} }