Raven DB – Introduction

Few days’ back I got introduced to some new database, called RavenDB. Unlike relational databases, its document based database. Document-based databases do not store data in tables with uniform sized fields for each record. Instead, each record is stored as a document that has certain characteristics. Any number of fields of any length can be added to a document. Fields can also contain multiple pieces of data. You can find more details about Raven DB here: http://ravendb.net/

In this post I am talking about connecting to RavenDB database; and adding items to a document (not table). You need to start the RavenDB server (RavenDb.exe) from the “RavenDB-Build-98\Server” path. (Or you can install the RavenDb server as a Service.) This will opens up a command window.

RavenDB Server running

RavenDB Server running

Now your RavenDB server is ready to accept connections. You can check it by browsing http://localhost:8080 in the browser. It will display the WebUI of RavenDB. To connect and access the database via code, you need to add reference of Raven.Client.Lightweight.dll from this path “RavenDB-Build-98\Client”. If you are developing applications in .Net Framework 3.5 you need to add reference of the same assembly from this path “RavenDB-Build-98\Client-3.5″ instead of Client folder.

In this post I am creating a console application which helps to add addresses to RavenDB. I have created a class with few properties like Name, StreetAddress etc.

public class Address
{
    public override string ToString()
    {
        return this.Name + ":" + this.StreetAddress + ":" + this.Phone + ":" + this.Email;
    }
    public string Name
    {
        get;
        set;
    }

    public string StreetAddress
    {
        get;
        set;
    }

    public string Phone
    {
        get;
        set;
    }

    public string Email
    {
        get;
        set;
    }
}

To connect to with RavenDB; you need to create a DocumentStore and then need to initialize the DocumentStore object. Then need to create a Document Session object from the DocumentStore.OpenSession() method. You need to set the URL property of the documentstore object otherwise you may get an exception- Invalid URI: The format of the URI could not be determined. To save an object to the DB you need to call the documentSession.Store() method, and to commit it to the database you need to call the SaveChanges method of the documentSession. The documentSession object also exposes a Stored event, which triggers when we call the SaveChanges() method, helps to get the information about the last saved object.

using (DocumentStore documentStore = new DocumentStore())
{
    documentStore.Url = "http://localhost:8080";
    documentStore.Initialize();
    using (IDocumentSession documentSession = documentStore.OpenSession())
    {
        Console.Write("Enter name :");
        string name = Console.ReadLine();
        Console.Write("Enter Street Address :");
        string streetAddress = Console.ReadLine();
        Console.Write("Enter Phone :");
        string phone = Console.ReadLine();
        Console.Write("Enter Email :");
        string email = Console.ReadLine();

        documentSession.Store(new Address()
        {
            Email = email,
            Name = name,
            Phone = phone,
            StreetAddress = streetAddress
        });
        documentSession.Stored += new EntityStored(delegate(object o)
        {
            Console.WriteLine("Last saved data :");
            Console.WriteLine(o.ToString());
            Console.Read();
        });
        documentSession.SaveChanges();
    }
}

You can get more information and documentation from http://ravendb.net/