StructureMap - 检查某个类/接口的实例是否已创建
我需要检查结构图是否已创建某个接口的实例。我尝试过 ObjectFactory.GetInstance() 但这会在调用时创建 T 的新实例,并且 T 的具体实例不存在。我只想检查 T 的实例是否已创建(而不是创建新的)。我需要它来强制通过某个类创建实例)。
I have the need to check if an instance of some interface has already been created by structuremap. I have tried ObjectFactory.GetInstance() but this creates a new instance of T when called and a concrete instance of T does not exist. I just want to check if an instance of T has already been created (not to create a new). I need this to force the creation of instances through a certain class).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够使用以下方法来执行此操作:
注意,这仅适用于具有 Singleton、HttpContext 或 Hybrid 生命周期的实例。
You should be able to do this using:
Note, this only works for instances with Singleton, HttpContext or Hybrid lifecycle.
您不能询问 StructureMap 是否已经创建了接口的实例。
但是,它确实具有许多生命周期管理功能,允许它控制是创建新实例还是返回之前创建的缓存实例。
您可以告诉 StructureMap 创建一个 HttpContextScoped 实例,以便它为给定的 HTTP 请求返回相同的实例。
听起来您真正想知道的是如何使用 StructureMap 在 Web 应用程序中处理 NHibernate 会话。尝试:
http://trason.net/journal/2009/10 /7/bootstrapping-nhibernate-with-structuremap.html
You cannot ask StructureMap if it has already created an instance of an interface.
However, it does have many lifecycle management features that allow it to control whether it creates a new instance, or returns a cached instance that it previously created.
You can tell StructureMap to make an instance HttpContextScoped, so that it returns the same instance for a given HTTP request.
It sounds like what you really want to know is how to handle NHibernate sessions in a web application with StructureMap. Try:
http://trason.net/journal/2009/10/7/bootstrapping-nhibernate-with-structuremap.html