This commit is contained in:
TheXamlGuy
2024-09-27 21:25:36 +01:00
parent bd577975b2
commit 928969f39f
5 changed files with 16 additions and 11 deletions
+5 -8
View File
@@ -10,19 +10,16 @@ public class Component :
protected Component(IComponentBuilder builder) => protected Component(IComponentBuilder builder) =>
this.builder = builder; this.builder = builder;
public static TComponent? Create<TComponent>(IServiceProvider provider, public static TComponent Create<TComponent>(IServiceProvider provider,
Action<IComponentBuilder> builderDelegate) Action<IComponentBuilder> builderDelegate)
where TComponent : class, IComponent where TComponent : class, IComponent
{ {
if (provider.GetRequiredService<IServiceFactory>() is IServiceFactory factory) IServiceFactory factory = provider.GetRequiredService<IServiceFactory>();
{
IComponentBuilder builder = ComponentBuilder.Create();
builderDelegate(builder);
return factory.Create<TComponent>(builder); IComponentBuilder builder = ComponentBuilder.Create();
} builderDelegate(builder);
return default; return factory.Create<TComponent>(builder);
} }
public IComponentBuilder Configure(string name) => public IComponentBuilder Configure(string name) =>
+2 -2
View File
@@ -96,10 +96,10 @@ public class ComponentBuilder :
return host.Services.GetRequiredService<IComponentHost>(); return host.Services.GetRequiredService<IComponentHost>();
} }
public void SetComponentConfiguration(Action<ComponentContentConfiguration> configurationDelegate) public void SetContentConfiguration(Action<ComponentContentConfiguration> configurationDelegate)
{ {
ComponentContentConfiguration configuration = new(); ComponentContentConfiguration configuration = new();
configurationDelegate.Invoke(configuration); configurationDelegate(configuration);
this.configuration = configuration; this.configuration = configuration;
} }
+7
View File
@@ -9,6 +9,7 @@ public class ComponentFactory(IServiceProvider provider,
{ {
public IComponentHost? Create<TComponent, TConfiguration>(string key, public IComponentHost? Create<TComponent, TConfiguration>(string key,
TConfiguration? configuration = null, TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null,
Action<IServiceCollection>? servicesDelegate = null) Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent where TComponent : IComponent
where TConfiguration : where TConfiguration :
@@ -17,6 +18,12 @@ public class ComponentFactory(IServiceProvider provider,
if (provider.GetRequiredService<TComponent>() is TComponent component) if (provider.GetRequiredService<TComponent>() is TComponent component)
{ {
IComponentBuilder builder = component.Configure(key); IComponentBuilder builder = component.Configure(key);
if (builderDelegate is not null)
{
builderDelegate(builder);
}
builder.AddServices(services => builder.AddServices(services =>
{ {
services.AddTransient(_ => services.AddTransient(_ =>
+1 -1
View File
@@ -18,5 +18,5 @@ public interface IComponentBuilder
IComponentHost Build(); IComponentHost Build();
void SetComponentConfiguration(Action<ComponentContentConfiguration> configurationDelegate); void SetContentConfiguration(Action<ComponentContentConfiguration> configurationDelegate);
} }
+1
View File
@@ -6,6 +6,7 @@ public interface IComponentFactory
{ {
IComponentHost? Create<TComponent, TConfiguration>(string name, IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration? configuration = null, TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null,
Action<IServiceCollection>? servicesDelegate = null) Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent where TComponent : IComponent
where TConfiguration : ComponentConfiguration, new(); where TConfiguration : ComponentConfiguration, new();