How to create a document store?
To create an instance of the DocumentStore
you need to specify the URL address that points to the RavenDB server. The URL must contain the TCP port on which the server is listening (the default port number is 8080).
using (IDocumentStore store = new DocumentStore()
{
Url = "http://localhost:8080"
}.Initialize())
{
}
This will instantiate a communication channel between your application and the local RavenDB server instance. Alternatively instead of providing the server URL during initialization you can specify a name of the connection string.
Initialization and disposal
The above code contains two things you should pay special attention. The first one is that you have to call Initialize
method on the DocumentStore
to get the fully initialized instance of IDocumentStore
that you will be able to work with.
The second thing you should note is that the whole DocumentStore
initialization code is placed inside the using
statement to ensure the object disposal. This is needed to guarantee a proper cleanup. In a real case scenario it should be done when an application shuts down.
Singleton
Because the document store is a heavyweight object, there should only be one instance created per application (singleton). The document store is a thread safe object and its typical initialization looks like the following:
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 = "Northwind"
}.Initialize();
return store;
}
}