3
votes

Reading up on Lucene, it seems it's recommeneded to use the same instance of IndexSearcher across all requests.

If I have a search class which is injected using ninject

public interface IPatientSearch
{
    void DoSearch(ref SearchDTO _search);

    //...
}

Would there be any issues binding it using InSingletonScope, which would ensure the same instance is shared across all requests?

        Bind<IPatientSearch>().To<PatientSearch>().InSingletonScope();

Am I missing any obvious pitfalls of using such an approach?

1

1 Answers

5
votes

There's not an issue here from the Lucene.NET point of view; assuming your implementation of IPatientSearch creates an IndexWriter and uses that, there shouldn't be any problem. The IndexWriter class is thread-safe, and you won't have any troubles accessing your Lucene.NET index.

However, you have to make sure that all other aspects of the IPatientSearch implementation are thread-safe; if this singleton is accessed from multiple threads, then any other state that you have in the implementation has to be thread-safe. If your class is just a pass-through for calls to Lucene.NET, then you'll be fine, but if you have other state, then you need to make sure access to that state is synchronized.

You might want to create a thin abstraction around Lucene.NET and make that a singleton for the purposes of dependency injection, and have your other classes be instantiated normally (unless you need only one instance of that class).