Asp.net mvc RavenDB';s单例文档商店

Asp.net mvc RavenDB';s单例文档商店,asp.net-mvc,singleton,ravendb,document-store,Asp.net Mvc,Singleton,Ravendb,Document Store,我是使用RavenDB的新手。 我的理解是,我们必须首先创建一个DocumentStore,然后打开一个会话,以便将数据保存到数据库中。 从文档中,我了解到我们不应该每次都创建实例,而应该只使用singleton来创建DocumentStore。 但我意识到大多数文档或教程每次都只是演示创建实例 仅供参考,我正在使用ASP.NETMVC框架 所以我的问题来了 CreatingDocumentStore.cs(Singleton类)应位于哪个文件夹中 安置?在应用程序文件夹的根目录中 创建这个单例

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

仅供参考,我正在使用ASP.NETMVC框架

所以我的问题来了

  • CreatingDocumentStore.cs(Singleton类)应位于哪个文件夹中 安置?在应用程序文件夹的根目录中
  • 创建这个单例类之后,如何使用它 下面是我的AdminController在使用Singleton之前的原始代码。我不知道如何更改它以使用singleton类CreatingDocumentStore.cs

    如果有人能通过展示代码来演示使用Singleton,我将不胜感激。提前谢谢

    控制器文件夹中的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
    }
    
    公共类CreatingDocumentStore
    {
    公共创建文档库()
    {
    #区域文档\存储\创建
    使用(IDocumentStore=新文档库()
    {
    Url=”http://localhost:8080"
    }.Initialize())
    {
    }
    #端区
    }
    #区域文件\存储\持有者
    公共类文档存储者
    {
    私有静态惰性存储=新惰性(CreateStore);
    公共静态IDocumentStore
    {
    获取{return store.Value;}
    }
    私有静态IDocumentStore CreateStore()
    {
    IDocumentStore=新文档库()
    {
    Url=”http://localhost:8080",
    DefaultDatabase=“foodfurydb”
    }.Initialize();
    退货店;
    }
    }
    #端区
    }
    
    不久前在他的博客上发布:

    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; }
        }
    }
    
    公共静态类全局
    {
    private static readonly Lazy theDocStore=new Lazy(()=>
    {
    var docStore=新文档存储
    {
    ConnectionStringName=“RavenDB”
    };
    初始化();
    //可选:
    //CreateIndexes(typeof(Global).Assembly,docStore);
    退货仓库;
    });
    公共静态IDocumentStore文档库
    {
    获取{返回docstore.Value;}
    }
    }
    
    你要把它放在哪里,这取决于你的建筑风格。通常,我们将
    数据库
    连接等放置在
    基础设施
    中。如果只有一个项目,可以将其放在项目的根目录中,或者创建一个包含
    数据库
    内容的文件夹

    您可以从stackoverflow中检查这些帖子:


    通过在
    CreatingDocumentStore
    中使用
    块添加
    您到底想要实现什么?当控件离开构造函数时,它将立即处理您的存储。