Amend compoments to be keyed based
This commit is contained in:
@@ -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<TComponent>(IServiceProvider provider,
|
||||
public static TComponent? Create<TComponent>(IServiceProvider provider,
|
||||
Action<IComponentBuilder> builderDelegate)
|
||||
where TComponent : class, IComponent
|
||||
{
|
||||
@@ -23,8 +22,13 @@ public class Component :
|
||||
|
||||
return factory.Create<TComponent>(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;
|
||||
}
|
||||
@@ -9,23 +9,7 @@ public class ComponentBuilder :
|
||||
{
|
||||
private readonly IHostBuilder hostBuilder;
|
||||
|
||||
private ContentRootConfiguration rootConfiguration = 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 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<TConfiguration>(Action<TConfiguration> 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<IComponentHost>();
|
||||
}
|
||||
|
||||
public void SetComponentConfiguration(Action<ComponentContentConfiguration> configurationDelegate)
|
||||
{
|
||||
ComponentContentConfiguration configuration = new();
|
||||
configurationDelegate.Invoke(configuration);
|
||||
|
||||
this.configuration = configuration;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record ContentRootConfiguration
|
||||
public record ComponentContentConfiguration
|
||||
{
|
||||
public string ContentRoot { get; set; } = "Local";
|
||||
|
||||
@@ -7,16 +7,16 @@ public class ComponentFactory(IServiceProvider provider,
|
||||
IComponentScopeCollection scopes) :
|
||||
IComponentFactory
|
||||
{
|
||||
public IComponentHost? Create<TComponent, TConfiguration>(string name,
|
||||
public IComponentHost? Create<TComponent, TConfiguration>(string key,
|
||||
TConfiguration? configuration = null,
|
||||
Action<IServiceCollection>? servicesDelegate = null)
|
||||
where TComponent : IComponent
|
||||
where TConfiguration : ComponentConfiguration, new()
|
||||
where TConfiguration :
|
||||
ComponentConfiguration, new()
|
||||
{
|
||||
if (provider.GetRequiredService<TComponent>() 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<IComponentScopeProvider>());
|
||||
|
||||
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<IServiceProvider>()));
|
||||
|
||||
return host;
|
||||
|
||||
@@ -13,7 +13,7 @@ public class ComponentInitializer(IEnumerable<IComponent> components,
|
||||
{
|
||||
foreach (IComponent component in components)
|
||||
{
|
||||
IComponentBuilder builder = component.Create();
|
||||
IComponentBuilder builder = component.Configure("");
|
||||
builder.AddServices(services =>
|
||||
{
|
||||
services.AddTransient(_ =>
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
public interface IComponent
|
||||
{
|
||||
IComponentBuilder Create();
|
||||
IComponentBuilder Configure(string key);
|
||||
}
|
||||
@@ -14,7 +14,9 @@ public interface IComponentBuilder
|
||||
IComponentBuilder AddConfiguration<TConfiguration>(string section)
|
||||
where TConfiguration : ComponentConfiguration, new();
|
||||
|
||||
IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate);
|
||||
|
||||
IComponentHost Build();
|
||||
|
||||
IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate);
|
||||
void SetComponentConfiguration(Action<ComponentContentConfiguration> configurationDelegate);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<INavigation>();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user