Openrasta:在依赖解析器中交换实例
假设我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你必须在某个时候交换实例,我不会使用 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