1

我是使用 RavenDB 的新手。我的理解是我们必须首先创建一个 DocumentStore,然后打开一个会话才能将数据保存到数据库中。从文档中,我了解到我们不应该每次都创建实例,而应该只使用单例来创建 DocumentStore。但我意识到大多数文档或教程每次都只是演示创建实例。

仅供参考,我正在使用 ASP.NET MVC 框架。

所以我的问题来了,

  1. 应将 CreatingDocumentStore.cs(单例类)放在哪个文件夹中?在应用程序文件夹的根目录中?
  2. 创建了这个单例类之后,如何使用呢?

下面是我的 AdminController 在使用 Singleton 之前的原始代码。而且我不知道如何更改它以使用单例类-CreatingDocumentStore.cs

如果有人可以通过显示代码演示使用 Singleton,我将不胜感激。提前致谢!

Controller 文件夹中的AdminController.cs

public class AdminController : Controller
{

    public ActionResult Index()
    {

        using (var store = new DocumentStore
        {
            Url = "http://localhost:8080/",
            DefaultDatabase = "foodfurydb"
        })
        {
            store.Initialize();

            using (var session = store.OpenSession())
            {
                session.Store(new Restaurant
                {
                    RestaurantName = "Boxer Republic",
                    ResCuisine = "Western",
                    ResAddress = "Test Address",
                    ResCity = "TestCity",
                    ResState = "TestState",
                    ResPostcode = 82910,
                    ResPhone = "02-28937481"
                });

                session.SaveChanges();

            }
        }

        return View();
    }

    public ActionResult AddRestaurant()
    {
        return View();
    }
}

在根文件夹中创建DocumentStore.cs

 public class CreatingDocumentStore
{
    public CreatingDocumentStore()
    {
        #region document_store_creation
        using (IDocumentStore store = new DocumentStore()
        {
            Url = "http://localhost:8080"
        }.Initialize())
        {

        }
        #endregion
    }

    #region document_store_holder
    public class DocumentStoreHolder
    {
        private static Lazy<IDocumentStore> store = new Lazy<IDocumentStore>(CreateStore);

        public static IDocumentStore Store
        {
            get { return store.Value; }
        }

        private static IDocumentStore CreateStore()
        {
            IDocumentStore store = new DocumentStore()
            {
                Url = "http://localhost:8080",
                DefaultDatabase = "foodfurydb"
            }.Initialize();

            return store;
        }
    }
    #endregion
}
4

1 回答 1

1

正如Ayende前段时间在他的博客上发布的管理 RavenDB 文档存储启动

RavenDB 的文档存储是您访问数据库的主要访问点。强烈建议您访问的每台服务器都只有一个文档存储实例。这通常意味着您必须实现一个单例,其中涉及所有双重检查锁定废话。

他向我们展示了一个例子:

public static class Global
{
    private static readonly Lazy<IDocumentStore> theDocStore = new Lazy<IDocumentStore>(()=>
        {
            var docStore = new DocumentStore
                {
                    ConnectionStringName = "RavenDB"
                };
            docStore.Initialize();

            //OPTIONAL:
            //IndexCreation.CreateIndexes(typeof(Global).Assembly, docStore);

            return docStore;
        });

    public static IDocumentStore DocumentStore
    {
        get { return theDocStore.Value; }
    }
}

你要把它放在哪里,取决于你的架构。通常我们将database连接等放在一个Infrastructure. 如果你有一个项目,你可以放在项目的根目录下,或者创建一个包含database东西的文件夹。

您可以从 stackoverflow 查看这些帖子:

于 2016-11-01T11:28:45.860 回答