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);
}
public IComponentBuilder Configure(string name) =>
Configuring(name, builder);
public virtual IComponentBuilder Configuring(string name,
IComponentBuilder builder) => builder;
public IComponentBuilder Configure(string? name = null,
Action<IComponentBuilder>? builderDelegate = null)
{
if (builderDelegate is not null)
{
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) :
IComponentFactory
{
public IComponentHost? Create<TComponent, TConfiguration>(string key,
public IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration? configuration = null,
Action<IComponentBuilder>? builderDelegate = null,
Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent
where TConfiguration :
class, new()
where TConfiguration : class, new()
{
if (provider.GetRequiredService<TComponent>() is TComponent component)
{
IComponentBuilder builder = component.Configure(key);
if (builderDelegate is not null)
{
builderDelegate(builder);
}
IComponentBuilder builder = component.Configure(name, builderDelegate);
builder.AddServices(services =>
{
@@ -45,18 +39,13 @@ public class ComponentFactory(IServiceProvider provider,
provider.GetRequiredService<IComponentScopeProvider>());
services.AddRange(proxy.Services);
services.AddSingleton(new NamedComponent(key));
if (servicesDelegate is not null)
{
servicesDelegate(services);
}
services.AddSingleton(new NamedComponent(name));
});
builder.AddConfiguration(key, configuration);
builder.AddConfiguration(name, configuration);
IComponentHost host = builder.Build();
scopes.Add(new ComponentScopeDescriptor(key,
scopes.Add(new ComponentScopeDescriptor(name,
host.Services.GetRequiredService<IServiceProvider>()));
return host;
+1 -1
View File
@@ -14,7 +14,7 @@ public class ComponentInitializer(IEnumerable<IComponent> components,
{
foreach (IComponent component in components)
{
IComponentBuilder builder = component.Configure("");
IComponentBuilder builder = component.Configure();
builder.AddServices(services =>
{
services.AddTransient(_ =>
+2 -1
View File
@@ -2,5 +2,6 @@
public interface IComponent
{
IComponentBuilder Configure(string key);
IComponentBuilder Configure(string? name = null,
Action<IComponentBuilder>? builderDelegate = null);
}