Source Code - http://ravenmvc.codeplex.com
A while back, I have blogged NoSQL with MongoDB, NoRM and ASP.NET MVC Part 1 and Part 2 on how to use MongoDB with an ASP.NET MVC application. The NoSQL movement is getting big attention and RavenDB is the latest addition to the NoSQL and document database world. RavenDB is an Open Source (with a commercial option) document database for the .NET/Windows platform developed by Ayende Rahien. Raven stores schema-less JSON documents, allow you to define indexes using Linq queries and focus on low latency and high performance. RavenDB is .NET focused document database which comes with a fully functional .NET client API and supports LINQ. RavenDB comes with two components, a server and a client API. RavenDB is a REST based system, so you can write your own HTTP cleint API. As a .NET developer, RavenDB is becoming my favorite document database. Unlike other document databases, RavenDB is supports transactions using System.Transactions. Also it's supports both embedded and server mode of database. You can access RavenDB site at http://ravendb.net
A demo App with ASP.NET MVC
Let's create a simple demo app with RavenDB and ASP.NET MVC. To work with RavenDB, do the following steps.
- Go to http://ravendb.net/download and download the latest build.
- Unzip the downloaded file.
- Go to the /Server directory and run the RavenDB.exe. This will start the RavenDB server listening on localhost:8080
You can change the port of RavenDB by modifying the "Raven/Port" appSetting value in the RavenDB.exe.config file.
When running the RavenDB, it will automatically create a database in the /Data directory. You can change the directory name data by modifying "Raven/DataDirt" appSetting value in the RavenDB.exe.config file.
RavenDB provides a browser based admin tool. When the Raven server is running, You can be access the browser based admin tool and view and edit documents and index using your browser admin tool. The web admin tool available at http://localhost:8080
The below is the some screen shots of web admin tool
Working with ASP.NET MVC
To working with RavenDB in our demo ASP.NET MVC application, do the following steps
Step 1 - Add reference to Raven Cleint API
In our ASP.NET MVC application, Add a reference to the Raven.Client.Lightweight.dll from the Client directory.
Step 2 - Create DocumentStore
The document store would be created once per application. Let's create a DocumentStore on application start-up in the Global.asax.cs.
The above code will create a Raven DB document store and will be listening the server locahost at port 8080
Step 3 - Create DocumentSession on BeginRequest
Let's create a DocumentSession on BeginRequest event in the Global.asax.cs. We are using the document session for every unit of work. In our demo app, every HTTP request would be a single Unit of Work (UoW).
Step 4 - Destroy the DocumentSession on EndRequest
At the end of HTTP request, we are destroying the DocumentSession object.
The below code block shown all the code in the Global.asax.cs
We have setup all necessary code in the Global.asax.cs for working with RavenDB. For our demo app,
Let’s write a domain class
We have created simple domain entity Category. Let's create repository class for performing CRUD operations against our domain entity Category.
For every CRUD operations, we are taking the current document session object from HttpContext object.
We are calling the static method CurrentSession from the Global.asax.cs
The Load method get the single Category object based on the Id. RavenDB is working based on the REST principles and the Id would be like categories/1. The Id would be created by automatically when a new object is inserted to the document store. The REST uri categories/1 represents a single category object with Id representation of 1.
The GetCategories method returns all the categories calling the session.LuceneQuery method. RavenDB is using a lucen query syntax for querying. I will explain more details about querying and indexing in my future posts.
For insert/Update a Category entity, we have created Save method in repository class. The Save mothod used for both insert and update category. We just need to store category object on the document session. The session.SaveChanges() will save the changes to document store. if the category is a new one, it will insert a new record and update the category object, if it is an existing category object.
In the Delete method, we call the document session's delete method and call the SaveChanges method to reflect changes in the document store.
Let’s create ASP.NET MVC controller and controller actions for handling CRUD operations for the domain class Category
RavenDB is an awesome document database and I hope that it will be the winner in .NET space of document database world. The source code of demo application available at http://ravenmvc.codeplex.com/