Fixed issue where the builder delegate was called too late
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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,5 +2,6 @@
|
||||
|
||||
public interface IComponent
|
||||
{
|
||||
IComponentBuilder Configure(string key);
|
||||
IComponentBuilder Configure(string? name = null,
|
||||
Action<IComponentBuilder>? builderDelegate = null);
|
||||
}
|
||||
Reference in New Issue
Block a user