The Rain and The Shade

July 27, 2011

Why I like RavenDb as a .Net developer

Filed under: NoSQL Databases,RavenDb — ovaisakhter @ 1:00 pm

We often have discussion about amazing new NoSQL databases like CouchDB, Casendra and Redis.

Usually when I am asked about my choice for a NoSQL database I always say “I will use RavenDb”. I have used RavenDb in one of my projects and I am quite happy from the results, and in my experience if you do it right RavenDb performs amazingly well under extreme load scenarios (If not done right you can seriously screw up things but that’s a talk of another time).

As I mentioned I am generally very happy with the performance provided by RavenDb but I have an other reason for reason to use RavenDb,  that I am a .Net developer, and RavenDb is a Database build on top of .Net. So the learning curve for .Net developer to start using it is negligibly small.

Here I will describe the process of starting a new RavenDb endeavor

“Installing” RavenDb

To start using RavenDb download the binaries from http://ravendb.net/download unzip the file go to the server directory and hit the Raven.Server.exe and here you go you are up and running with a brand new Instance of RavenDb.

Try to navigate to http://localhost:8080/ in your browser and you will be able to see the All new Management Studio for RavenDb built in Silverlight. (The interface provides you can option to create sample data if you don’t want to get your hands dirty in code right now, neat idea I say?)

Using RavenDb

RavenDb server interacts with the client using RESTful web services and the response is in the form of JSON. For .Net developers a very extensive .Net client API is also available, which makes your life a lot easier. Using the client API it is extremely simple to get started.

Now you have you server up and running all you need to do is to write the following code in your application

    var documentStore1 = new DocumentStore { Url = “http://localhost:8080″ }.Initialize();

            using (var session1 = documentStore1.OpenSession())
{
session1.Store(new User { Id = “users/ayende”, Name = “Ayende” });
session1.SaveChanges();
}

and here you go you just saved your first record or document as it is called in this neck of wood into the database, and here is what you need to do to get it back.

using (var session1 = documentStore1.OpenSession())
{
var myUserBackFromRaven = session1.Load<User>(“users/ayende”);

                Console.WriteLine(myUserBackFromRaven.Name);
}

Simple ?

and if you need to query your documents all you need to know if how to write a LINQ statement.

var users = session1.Query<User>().Where(x => x.Name.StartsWith(“A”));

             foreach (var user in users)
{
Console.WriteLine(user.Name);
}

Ravendb provides you a possibility to be able to index your documents for faster retrieval of information. The best part is that all the RavenDb index definitions are written using LINQ which makes the learning curve extremely small.

Here is how you define an index on the User Name in side “Raven Management Studio” web interface.

image

or alternatively you can create an index from inside the code.

public class UsersNameIndex : AbstractIndexCreationTask<User>
{
public  UsersNameIndex()
{
Map = users => from user in users
select new { user.Name };
}

    }

and then query this index from the code like

var users = session1.Query<User>(“UsersNameIndex”).Where(x => x.Name.StartsWith(“A”));

                foreach (var user in users)
{
Console.WriteLine(user.Name);
}

Conclusion

The purpose of this blog is not to give you a tutorial of RavenDb rather it is to show you that how terribly simple it is for a .Net developer to start using RavenDb. I believe if you are a .Net developer you will be up and running with RavenDb in the matter of hours and that is why I like RavenDb as a .Net developer.

Links

www.Ravendb.net

Advertisements

Blog at WordPress.com.