Reputation: 435
I am a newbie in using RavenDB. What I understand is we must first create a DocumentStore, then opening a session in order to save data to the database. From the documentation, I understand that we should not create instance every time and should only use singleton for creating DocumentStore. But I realized most of the documentation or tutorials only demo creating instance every time.
FYI, I'm using ASP.NET MVC framework.
So here comes my question,
Below is the original code of my AdminController before using Singleton. And I don't know how to change it in order to use the singleton class - CreatingDocumentStore.cs
I would greatly appreciate if someone can demo in using Singleton by showing the code. Thanks in advance!
AdminController.cs in Controller folder
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();
}
}
CreatingDocumentStore.cs in root folder
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
}
Upvotes: 1
Views: 801
Reputation: 1872
As Ayende posted on his blog sometime ago Managing RavenDB Document Store startup:
The RavenDB’s document store is your main access point to the database. It is strongly recommended that you’ll have just a single instance of the document store per each server you are accessing. That usually means that you have to implement a singleton, with all the double checked locking nonsense that is involved in that.
He show us an example:
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; }
}
}
Where you are going to place it, depends on your architecure. Usually we place database
connections, etc, in an Infrastructure
. If you have a single project, you can place in the root of the project, or create a folder that contains database
stuff.
You can check these posts from stackoverflow:
Upvotes: 1