gpt4 book ai didi

autofac - 注册委托(delegate)时假定的 Autofac LifetimeScope 是什么?

转载 作者:行者123 更新时间:2023-12-05 04:11:00 25 4
gpt4 key购买 nike

我无法从文档中确定这一点。

给定一个注册:

builder.RegisterType<ExampleComponent>().As<IComponent>().InstancePerLifetimeScope();

以下注册将承担什么LifetimeScope?

builder.Register(ctx =>
{
var component = ctx.Resolve<IComponent>();
return new SomeService(component);
}).As<ISomeService>();

注意:这只是一个例子。显然,在这种情况下,您只需解析 ISomeService 并允许 Autofac 实例化具有 IComponent 依赖项的 SomeService 实例。我有一个更复杂的注册需求,这种方法是必要的,但其详细信息与问题并不相关。

鉴于 IComponent 被注册为 InstancePerLifetimeScope(我理解这意味着它继承了用于直接解析它的范围或用于解析它是其依赖项的组件的范围),并且为 ISomeService 注册的委托(delegate)将是InstancePerDependency 的默认生命周期范围,我希望委托(delegate)中 IComponent 的解析具有生命周期范围 InstancePerDependency。

这是正确的吗?

最佳答案

当您解析 ISomeService 时,这将按预期按依赖项完成,这意味着您将在每次解析时获得 SomeService 的一个新实例(通过调用委托(delegate))。

然而,获取组件的调用例如:

var component = ctx.Resolve<IComponent>();

将返回每个生命周期共享组件的一个实例(因此,如果您不创建任何子/嵌套生命周期范围,它将或多或少是一个单例)。

这里有一个小例子来演示它。

首先这里是 IComponent 的简单实现:

public interface IComponent
{
int GetId();
}

public class ExampleComponent : IComponent
{
private static int id = 0;
private int instanceId;

public ExampleComponent()
{
this.instanceId = id;
id++;
}

public int GetId()
{
return this.instanceId;
}
}

然后对于 ISomeService :

public interface ISomeService
{
int GetServiceID();
int GetComponentId();
}

public class SomeService : ISomeService
{
private static int id = 0;
private int instanceId;


private readonly IComponent component;

public SomeService(IComponent component)
{
if (component == null)
throw new ArgumentNullException("component");

this.component = component;
this.instanceId = id;
id++;
}

public int GetComponentId()
{
return this.component.GetId();
}

public int GetServiceID()
{
return this.instanceId;
}
}

我按照你写的那样保留了注册:

    ContainerBuilder builder = new ContainerBuilder();

builder.RegisterType<ExampleComponent>().As<IComponent>().InstancePerLifetimeScope();
builder.Register(ctx =>
{
var component = ctx.Resolve<IComponent>();
return new SomeService(component);
}).As<ISomeService>();

IContainer rootContainer = builder.Build();

现在让我们做一些解决:

ISomeService service1 = rootContainer.Resolve<ISomeService>();
ISomeService service2 = rootContainer.Resolve<ISomeService>();

Console.WriteLine(string.Format("Service 1: {0} , Component : {1} ", service1.GetServiceID(), service1.GetComponentId()));
Console.WriteLine(string.Format("Service 2: {0} , Component : {1} ", service2.GetServiceID(), service2.GetComponentId()));

您将获得:

服务 1:0,组件:0服务 2:1,组件:0

因为 Component 由主要生命周期范围共享。

当然,如果您创建子作用域:

IContainer rootContainer = builder.Build();

ILifetimeScope scope1 = rootContainer.BeginLifetimeScope();
ILifetimeScope scope2 = rootContainer.BeginLifetimeScope();

ISomeService service1 = scope1.Resolve<ISomeService>();
ISomeService service2 = scope2.Resolve<ISomeService>();

Console.WriteLine(string.Format("Service 1: {0} , Component : {1} ", service1.GetServiceID(), service1.GetComponentId()));
Console.WriteLine(string.Format("Service 2: {0} , Component : {1} ", service2.GetServiceID(), service2.GetComponentId()));

服务 1:0,组件:0服务 2:1,组件:1

在这种情况下,您有 2 个不同的作用域,因此对组件的每个解析都会提供一个不同的作用域。

关于autofac - 注册委托(delegate)时假定的 Autofac LifetimeScope 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43504588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com