Fixed issue where the builder delegate was called too late

This commit is contained in:
TheXamlGuy
2024-09-30 21:54:28 +01:00
parent a9ae9b1d8f
commit 5a5d3f80a9
4 changed files with 20 additions and 23 deletions
+12 -5
View File
@@ -22,9 +22,16 @@ public class Component :
return factory.Create<TComponent>(builder); return factory.Create<TComponent>(builder);
} }
public IComponentBuilder Configure(string name) => public IComponentBuilder Configure(string? name = null,
Configuring(name, builder); Action<IComponentBuilder>? builderDelegate = null)
{
public virtual IComponentBuilder Configuring(string name, if (builderDelegate is not null)
IComponentBuilder builder) => builder; {
builderDelegate(builder);
}
return Configuring(builder);
}
public virtual IComponentBuilder Configuring(IComponentBuilder builder) => builder;
} }
+6 -17
View File
@@ -7,22 +7,16 @@ public class ComponentFactory(IServiceProvider provider,
IComponentScopeCollection scopes) : IComponentScopeCollection scopes) :
IComponentFactory IComponentFactory
{ {
public IComponentHost? Create<TComponent, TConfiguration>(string key, public IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration? configuration = null, TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null, Action<IComponentBuilder>? builderDelegate = null,
Action<IServiceCollection>? servicesDelegate = null) Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent where TComponent : IComponent
where TConfiguration : where TConfiguration : class, new()
class, new()
{ {
if (provider.GetRequiredService<TComponent>() is TComponent component) if (provider.GetRequiredService<TComponent>() is TComponent component)
{ {
IComponentBuilder builder = component.Configure(key); IComponentBuilder builder = component.Configure(name, builderDelegate);
if (builderDelegate is not null)
{
builderDelegate(builder);
}
builder.AddServices(services => builder.AddServices(services =>
{ {
@@ -45,18 +39,13 @@ public class ComponentFactory(IServiceProvider provider,
provider.GetRequiredService<IComponentScopeProvider>()); provider.GetRequiredService<IComponentScopeProvider>());
services.AddRange(proxy.Services); services.AddRange(proxy.Services);
services.AddSingleton(new NamedComponent(key)); services.AddSingleton(new NamedComponent(name));
if (servicesDelegate is not null)
{
servicesDelegate(services);
}
}); });
builder.AddConfiguration(key, configuration); builder.AddConfiguration(name, configuration);
IComponentHost host = builder.Build(); IComponentHost host = builder.Build();
scopes.Add(new ComponentScopeDescriptor(key, scopes.Add(new ComponentScopeDescriptor(name,
host.Services.GetRequiredService<IServiceProvider>())); host.Services.GetRequiredService<IServiceProvider>()));
return host; return host;
+1 -1
View File
@@ -14,7 +14,7 @@ public class ComponentInitializer(IEnumerable<IComponent> components,
{ {
foreach (IComponent component in components) foreach (IComponent component in components)
{ {
IComponentBuilder builder = component.Configure(""); IComponentBuilder builder = component.Configure();
builder.AddServices(services => builder.AddServices(services =>
{ {
services.AddTransient(_ => services.AddTransient(_ =>
+2 -1
View File
@@ -2,5 +2,6 @@
public interface IComponent public interface IComponent
{ {
IComponentBuilder Configure(string key); IComponentBuilder Configure(string? name = null,
Action<IComponentBuilder>? builderDelegate = null);
} }