Amit Kumar
Amit Kumar

Reputation: 853

RavenDB- Building Session Factory, singleton DocumentStore

I'm a newbie to RavenDb. I've built the RavenDB session factory like the below code. The idea is very much driven from the way we build NHibernateSessionHelpers. I hope this should work really well in production. Are there any suggestions to improve this from people who are experts in RavenDB?

public class MXRavenDbSessionHelper
{        
    //---All new lazy singleton that's thread safe.---        
    private static Lazy<IDocumentStore> _lazyDocStore = new Lazy<IDocumentStore>(() => InitializeSessionFactory());

    private MXRavenDbSessionHelper() { }

    private static IDocumentStore SessionFactory
    {
        get
        {
            return _lazyDocStore.Value;
        }
    }

    public static IDocumentSession OpenSession()
    {
        return SessionFactory.OpenSession();
    }

    private static IDocumentStore InitializeSessionFactory()
    {
        var _docStore = new DocumentStore { ConnectionStringName = "RavenDBConnString", DefaultDatabase = "MXMunky" }; //One more way is this : _store = new DocumentStore { Url = "http://localhost:7000" };
        _docStore.Initialize();            
        _docStore.Conventions.IdentityPartsSeparator = "-";
        IndexCreation.CreateIndexes(typeof(Location).Assembly, _docStore);

        return _docStore;
    }
}

Upvotes: 0

Views: 588

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241563

I don't think you need to keep _docStore separately. See Jon Skeet's singleton patterns (#6).

Other than that, I don't see anything particularly wrong with it.

I'd be careful not to use this when unit testing. There, you actually do want a new docstore instance for each test - and they should be disposed properly.

Upvotes: 2

Related Questions