私は RavenDB を使用する初心者です。私が理解しているのは、最初に DocumentStore を作成し、次にデータをデータベースに保存するためにセッションを開く必要があるということです。ドキュメントから、毎回インスタンスを作成するべきではなく、DocumentStore の作成にはシングルトンのみを使用する必要があることを理解しています。しかし、ドキュメントやチュートリアルのほとんどは、毎回インスタンスを作成するデモのみであることに気付きました。
参考までに、私は ASP.NET MVC フレームワークを使用しています。
ここで質問です。
- CreationDocumentStore.cs (Singleton クラス) はどのフォルダーに配置する必要がありますか? アプリケーションフォルダのルートに?
- このシングルトン クラスを作成したら、どのように使用しますか?
以下は、シングルトンを使用する前の私の AdminController の元のコードです。そして、シングルトンクラスを使用するためにそれを変更する方法がわかりません-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
}