0

我是 RavenDb 的新手。我已经像下面的代码一样构建了 RavenDB 会话工厂。这个想法很大程度上来自我们构建 NHibernateSessionHelpers 的方式。我希望这应该在生产中很好地工作。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;
    }
}
4

1 回答 1

2

我认为你不需要_docStore单独保存。请参阅Jon Skeet 的单例模式(#6)。

除此之外,我看不出有什么特别的问题。

我会小心不要在单元测试时使用它。在那里,您实际上确实需要为每个测试创建一个新的 docstore 实例——并且应该正确处理它们。

于 2013-02-21T21:16:36.157 回答