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);
|
return factory.Create<TComponent>(builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IComponentBuilder Configure(string name) =>
|
public IComponentBuilder Configure(string? name = null,
|
||||||
Configuring(name, builder);
|
Action<IComponentBuilder>? builderDelegate = null)
|
||||||
|
{
|
||||||
|
if (builderDelegate is not null)
|
||||||
|
{
|
||||||
|
builderDelegate(builder);
|
||||||
|
}
|
||||||
|
|
||||||
public virtual IComponentBuilder Configuring(string name,
|
return Configuring(builder);
|
||||||
IComponentBuilder builder) => builder;
|
}
|
||||||
|
|
||||||
|
public virtual IComponentBuilder Configuring(IComponentBuilder builder) => builder;
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -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,5 +2,6 @@
|
|||||||
|
|
||||||
public interface IComponent
|
public interface IComponent
|
||||||
{
|
{
|
||||||
IComponentBuilder Configure(string key);
|
IComponentBuilder Configure(string? name = null,
|
||||||
|
Action<IComponentBuilder>? builderDelegate = null);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user