StructureMap“条件单例”用于 Lucene.Net IndexReader
我有一个线程安全对象,它的创建成本很高,并且需要通过我的应用程序(Lucene.Net IndexReader)来使用。
该对象可能会变得无效,此时我需要重新创建它(IndexReader.IsCurrent 为 false,需要使用 IndexReader.Reopen 的新实例)。
我希望能够使用 IoC 容器 (StructureMap) 来管理对象的创建,但我无法确定这种情况是否可行。感觉就像某种“条件单例”生命周期。
StructureMap提供这样的功能吗? 还有其他建议吗?
I have a threadsafe object that is expensive to create and needs to be available through my application (a Lucene.Net IndexReader).
The object can become invalid, at which point I need to recreate it (IndexReader.IsCurrent is false, need a new instance using IndexReader.Reopen).
I'd like to able to use an IoC container (StructureMap) to manage the creation of the object, but I can't work out if this scenario is possible. It feels like some kind of "conditional singleton" lifecycle.
Does StructureMap provide such a feature?
Any alternative suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会使用
PerRequest
范围,而不直接返回IndexReader
。相反,我会返回 IndexReader 的抽象,它将对类级别上保存的静态引用执行检查。然后,当访问 shim/proxy/abstraction 上的属性时,它将检查静态引用(当然,您将使其成为线程安全的)并在交付之前根据需要重新获取
IndexReader
它返回给用户。I would probably use a scope of
PerRequest
and not return theIndexReader
directly. Instead, I'd return an abstraction of theIndexReader
which would perform a check on a static reference held on the class level.Then, when your property on the shim/proxy/abstraction is accessed, it would check the static reference (you would make it thread-safe, of course) and re-get the
IndexReader
if needed before delivering it back to the user.最后,我选择了一个简单的代理对象,它包装了实际的 IndexReader 并管理重新打开。由于我需要在请求之间使用相同的实例,因此我使用 StructureMap 来提供它的单例实例。代码如下。
我已经研究过创建自定义 StructureMap ILifecycle 来处理这种情况,但没有走得太远,请参阅此问题。
以及 StructureMap 注册:
In the end I have gone for a simple proxy object that wraps the actual IndexReader and manages the Reopening. As I need to use the same instance of this across requests I am using StructureMap to provide a singleton instance of it. Code below.
I've investigated creating a custom StructureMap ILifecycle to handle this situation, but didn't get to far, see this question.
And the StructureMap registration: