RavenDB-构建会话工厂,singleton DocumentStore

RavenDB-构建会话工厂,singleton DocumentStore,ravendb,Ravendb,我是RavenDb的新手。我构建了RavenDB会话工厂,如下代码所示。这个想法在很大程度上源于我们构建NHibernateSessionHelper的方式。我希望这在生产中能很好地发挥作用。RavenDB的专家们有什么建议来改进这一点吗 public class MXRavenDbSessionHelper { //---All new lazy singleton that's thread safe.--- private static La

我是RavenDb的新手。我构建了RavenDB会话工厂,如下代码所示。这个想法在很大程度上源于我们构建NHibernateSessionHelper的方式。我希望这在生产中能很好地发挥作用。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;
    }
}
公共类MXRavenDbSessionHelper
{        
//---所有新的线程安全的懒惰单例
private static Lazy _lazyDocStore=new Lazy(()=>InitializeSessionFactory());
私有MXRavenDbSessionHelper(){}
私有静态IDocumentStore会话工厂
{
得到
{
返回_lazyDocStore.Value;
}
}
公共静态IDocumentSession OpenSession()
{
返回SessionFactory.OpenSession();
}
私有静态IDocumentStore InitializeSessionFactory()
{
var\u docStore=new DocumentStore{ConnectionStringName=“RavenDBConnString”,DefaultDatabase=“MXMunky”};//还有一种方法是:\u store=new DocumentStore{Url=”http://localhost:7000" };
_初始化();
_docStore.Conventions.IdentityPartsSeparator=“-”;
CreateIndexes(typeof(Location).Assembly,_docStore);
退货(docStore);;
}
}

我认为您不需要单独保存
\u docStore
。见(#6)

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


在单元测试时,我会小心不要使用这个。在这里,您确实希望每个测试都有一个新的docstore实例,并且应该正确地处理它们。

oh ya_docStore在这里过时了,我错了。我已经更新了代码。乔恩·斯基特(Jon Skeet)关于单身汉的文章绝对很棒。谢谢你,马特