Storing and retrieving records using RavenDB

After installing RavenDB 2 the next step is to start saving data and load it again. Unlike SQL server we don’t need to go into the RavenDB Management Studio and create a database and tables with a schema. The database, optional but highly recommended, will be created when we first access it. And when it comes down to tables and schema RavenDB is much simpler, all it does is store documents. A document doesn’t have a schema enforced so we don’t need to define one up front. Instead we just save some data and load it again.

The document to store

Even though RavenDB doesn’t require us to define the document structure we are using C# at the front end and in C# we usually define our data structure using classes. In this example we are going to store people using the following C# class:

   1: class Person


   2: {


   3:     public string Id { get; set; }


   4:     public string FirstName { get; set; }


   5:     public string LastName { get; set; }


   6: }

We are going to provide the first and last name and the Id property is for use with RavenDB. In fact we could leave the Id out, RavenDB would not care, but it would be harder to load a single document if we did.

 

The RavenDB DocumentStore

Before we can start working with the RavenDB server we first need to create an instance of the RavenDB DocumentStore. This is a relatively expensive object to create and normally in an application you only create a single and keep a reference to it. It is thread safe so a singleton will work just fine even in an ASP.NET application. The minimum information the DocumentStore needs is the url of the RavenDB server, in this case the default location of http://localhost:8080/. Additionally we can specify a default database to use, in this case people. This database will be created automatically when needed.

   1: var store = new DocumentStore


   2: {


   3:     Url = "http://localhost:8080/",


   4:     DefaultDatabase = "people"


   5: };


   6: store.Initialize();


The RavenDB DocumentSession

When we start doing any actual work with the RavenDB database we need to do so in the scope of a session. To create a new session we can call the DocumentStore.OpenSession() function. This session needs to short lived and only be used on a single thread. With this session we can persist data to the database and load it again.

   1: using (var session = store.OpenSession())


   2: {


   3:     var person = new Person()


   4:     {


   5:         FirstName = "Maurice",


   6:         LastName = "de Beijer"


   7:     };


   8:     session.Store(person);


   9:     Console.WriteLine("Storing person with ID: {0}", person.Id);


  10:     session.SaveChanges();


  11:     id = person.Id;


  12: }

Note that we need to call SaveChanges for the data to be persisted. If we forget to do this the session object will be disposed and the new person will not be saved.

 

Loading the document again is simple. Just create a session and use the Load() with the document id.

   1: using (var session = store.OpenSession())


   2: {


   3:     var person = session.Load<Person>(id);


   4:     Console.WriteLine("Loaded: {0} {1} {2}",


   5:         person.Id, person.FirstName, person.LastName);


   6: }


If we don’t know the document id, or we just want to load multiple document we can also query the document store using the Query() function.

   1: using (var session = store.OpenSession())


   2: {


   3:     var people = session.Query<Person>();


   4:     foreach (var person in people)


   5:     {


   6:         Console.WriteLine("Loaded: {0} {1} {2}",


   7:             person.Id, person.FirstName, person.LastName);


   8:     }


   9: }


Running the application gives us the following output:

image

 

What does RavenDB store for us?

If we open the RavenDB Management Studio and navigate to the person stored we can see what data is actually stored. As you can see below the data is actually saved in a JSON format. The C# type isn’t used at all.

image

In fact we can load this document without the .NET client library with a simple HTTP get request:

 

http://localhost:8080/databases/people/docs/people/1



This will simply return the data as a JSON response in the browser.



image



 



It turns out RavenDB stores more than just the data we send, it also stores some metadata. The most important parts of metadata are the RavenDB entity name, the C# class name we used without the namespace, and the CLR type, the full type name for the C# class we created.



image



















 



Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>