在 Autofac 中,如何更改调用 Build 后注册的实例?

发布于 2024-09-28 12:59:30 字数 234 浏览 0 评论 0原文

假设我有这段代码

var builder = new ContainerBuilder();
builder.RegisterInstance(new MyType());
var container = builder.Build();

,然后一段时间后,我想为 container 上调用的所有未来解析更改 MyType 的实例。

So lets say i have this code

var builder = new ContainerBuilder();
builder.RegisterInstance(new MyType());
var container = builder.Build();

Then some time later I want to change the instance of MyType for all future resolves that are called on container.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫雪独思 2024-10-05 12:59:30

当您想要更改注册时,创建一个新的 ContainerBuilder,注册新实例,并调用传入容器的 Update

// at some later point...
builder = new ContainerBuilder();
builder.RegisterInstance(myType2);
builder.Update(container);

At the time you want to change the registration, create a new ContainerBuilder, register the new instance, and call Update passing in the container:

// at some later point...
builder = new ContainerBuilder();
builder.RegisterInstance(myType2);
builder.Update(container);
红衣飘飘貌似仙 2024-10-05 12:59:30

另一种方法是注册一个能够更改容器提供的底层实例的委托。考虑以下代码:

 var theInstance = new MyType();
 var builder = new ContainerBuilder();
 builder.Register(context => theInstance);
 builder.Register<Action<MyType>>(context => newInstance => theInstance = newInstance);
 var container = builder.Build();

您现在可以解析操作以获取可以更改注册的委托:

 var updateInstance = c.Resolve<Action<MyType>>();
 updateInstance(new MyType());

注意:如果您可以详细说明何时为什么 您需要更改实例,也许我们甚至可以找到更好的解决方案。

An alternative could be to register a delegate that is able to change the underlying instance provided by the container. Consider the following code:

 var theInstance = new MyType();
 var builder = new ContainerBuilder();
 builder.Register(context => theInstance);
 builder.Register<Action<MyType>>(context => newInstance => theInstance = newInstance);
 var container = builder.Build();

You can now resolve the action to get a delegate that can change the registration:

 var updateInstance = c.Resolve<Action<MyType>>();
 updateInstance(new MyType());

Note: if you could elaborate on when and why you need to change the instance, perhaps we could even find a better solution.

×纯※雪 2024-10-05 12:59:30

您还可以利用 Autofac Lifetime 事件“OnActivating”,并在内存中拥有自己的控制器对象,该对象会替换已解析的实例,如下所示

builder.Register<TInterface>(c => c.Resolve<TConcrete>())
       .OnActivating(e => e.ReplaceInstance(new TInterfaceSubclass()));

https://autofaccn.readthedocs.io/en/latest/lifetime/events.html#onactivating

You can also make use of the Autofac Lifetime event "OnActivating" and have your own controller object in memory which replaces the resolved instance like so

builder.Register<TInterface>(c => c.Resolve<TConcrete>())
       .OnActivating(e => e.ReplaceInstance(new TInterfaceSubclass()));

https://autofaccn.readthedocs.io/en/latest/lifetime/events.html#onactivating

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文