Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructureMap 'conditional singleton' for Lucene.Net IndexReader

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?

like image 925
Gareth D Avatar asked Feb 09 '11 17:02

Gareth D


1 Answers

I would probably use a scope of PerRequest and not return the IndexReader directly. Instead, I'd return an abstraction of the IndexReader 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.

like image 170
casperOne Avatar answered Sep 30 '22 12:09

casperOne