First Steps with RavenDB
Ok, I’m starting another blog post series about a database. This time it is RavenDB, a document database. RavenDB is one of my favorite databases. In my opinion RavenDB has a very compelling combination of features. It stores JSON documents, supports transactions, it has very interesting query/indexing strategy (more in a later post), a well designed client-site API and good tooling. The only big drawback and yet a strength is that RavenDB was specifically designed for the .NET platform.
Downloading / Installing Raven DB
You can download RavenDB here. It’s just a zip-file which you can unzip at any location. After that you can just run the ‘Start.bat’-file. It will start up RavenDB and open the administration console in the browser. That’s it, you have a RavenDB server running.
Adding Raven to you Solution
The next step is to add the client API to your solution. You either can reference the assemblies which are shipped with the RavenDB download. The ‘Client’-folder contains the assemblies for .NET 4.0, the ‘Client 3.5’-folder for .NET 3.5 and the ‘Silverlight’ folder Silverlight 4.0. Even better you can use NuGet and grab the ‘RavenDB’ package.
Storing Documents
First let’s create a class which we want to store. Like this person class:
Now let’s use RavenDB to persist data. First we create a document-store instance. This document-store allows us to create sessions for doing work. Each session is basically a unit of work on the database side.
So let’s store our first document. Call the Store()-method and pass it the person object. After that we call .SaveChanges() to commit our changes.
Query Documents
Queries (and Indexes) are written in LINQ in RavenDB. Queries and Indexes work quite different in RavenDB than in most databases. However this difference will be the topic for another blog post. For now we just query for our stored Person document.
Update Documents
Updating documents is also easy. Query for the document, change the data you want and call .SaveChanges() to commit the changes.
Delete Documents
Of course we also can delete documents. First call the .Delete() method and pass it the document to delete and then commit the changes with .SaveChanges().
Documents? Tell Me More.
So far we stored person-objects / documents, ran a query and updated a person. At this stage is nearly looks like RavenDB is an object-database (like db4o). That’s why next time I will go into the concepts of RavenDB. Stay tuned.
- RSS-Feed Changes
- RavenDB: Documents, Nothing But Documents
I had to add session.SaveChanges(); to the storing documents code
Really interesting! I just found out about RavenDB 5 minutes and I already love it! Looks really simple. Could certainly make use of it in a couple of places!
Oups, yes…missed that in the code-snippet. Fixed.
Pingback: RavenDB: Documents, Nothing But Documents | Gamlor
Pingback: RavenDB: Preventing Database Slaughter | Gamlor