Openrasta:在依赖解析器中交换实例

发布于 2024-12-22 13:23:50 字数 339 浏览 4 评论 0原文

假设我使用 Now 在 OpenRasta 的依赖解析器中注册了某个实例,

resolver.AddDependencyInstance(IInterface, instance, DependencyLifetime.Singleton)

如果我想稍后交换该实例,比如从数据库中重新读取新数据,再次调用resolver.AddDependencyInstance 是正确的做法吗?

检查InternalDependencyResolver的实现,似乎没问题。然而我这么问是因为行为没有定义(在 openrasta 的源代码中,我检查过),并且方法前缀“Add”暗示了不同的行为。

Suppose I register some instance in OpenRasta's dependency resolver using

resolver.AddDependencyInstance(IInterface, instance, DependencyLifetime.Singleton)

Now if I want to swap that instance later, say to reread fresh data from the db, is another call to resolver.AddDependencyInstance the right thing to do?

Checking the InternalDependencyResolver implementation, it seems to be fine. However I'm asking because the behavior is not defined (in openrasta's sources, where I checked), and the method prefix "Add" is suggestive of different behavior.

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

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

发布评论

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

评论(1

凉城已无爱 2024-12-29 13:23:50

如果你必须在某个时候交换实例,我不会使用 Singleton。
使用 DependencyLifetime.Transient 并在需要新实例的类中进行构造函数注入

I wouldn't use Singleton if you have to swap the instance at some point.
Use DependencyLifetime.Transient and have a constructor injection in the class where you need the new instance

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