NoSQL: First steps with RavenDB and .NET

RavenDB is categorized as a NoSQL database.NoSQL means, that it is no longer necessary to use a relational schema to save information or data. RavenDB is open source, written in .NET and document orientated, so this means all data is saved in JSON. Furthermore RavenDB is transactional and has full ACID support (ACID stands for Atomicity,Consistency, Isolation and Durability, so in summary everythings works in a very safe way). For me these are enough facts to take a deeper look.

First, I created a Visual Studio solution with a C# Console application project. In this example the project is named RavenDBFirstSteps.Client. I recommend to use NuGet for the installation, so I searched for RavenDB in the NuGet Packages Manager and installed the RavenDB Client and RavenDB Server as shown in the following screenshots.

RavenDBFirstSteps1

RavenDBFirstSteps2

The binaries for the server can be found in the solution-folder\packages\RavenDB.Server.\Tools\Raven.Server.exe
and the only step to start the server is to double click the exe file. After a few seconds (and probably a firewall and administrator question) the RavenDB server is up and running.

RavenDBFirstSteps3

The database can be accessed with a browser (Silverlight plugin is needed). The default address is http://localhost:8080 (To change the port, edit the file local.config in the server directory). If the server is accessed for the first time, it asks to create a database, so I created the database RavenDBFirstSteps and selected none of the available options.

RavenDBFirstSteps4

To save data a model class is needed. It this example it will be tea as a class and a belonging enum.

public class Tea
    {
        public String Id { get; set; }
 
        public String Name { get; set; }
 
        public TeaType TeaType { get; set; }
 
        public Double WaterTemp { get; set; }
 
        public Int32 SleepTime { get; set; }
    }
public enum TeaType
    {
        Black,
        Green,
        Yellow,
        Oolong
    }

The class Tea has an Id property, which is used from the RavenDB to save the identifier of the object.

The saving of new data is very simple, only a DocumentStore and Session object is necessary. To create a DocumentStore the Url of the server is needed and the Database name for the Session.


using (var ds = new DocumentStore { Url = "http://localhost:8080/" }.Initialize())
{
    using (var session = ds.OpenSession("RavenDBFirstSteps"))
    {
        Tea tea = new Tea() { Name = "Earl Grey", TeaType = TeaType.Black, WaterTemp = 99, SleepTime = 3 };

        session.Store(tea);
        session.SaveChanges();
        Console.WriteLine(tea.Id);
    }
}

Right after the execution of the code, the RavenDB website shows the new document.

RavenDBFirstSteps5

As simple as saving new data is also loading and editing. Every document has an id, which can be used to load a specified object. The changes of the object can be saved with the Store() method as well.


using (var ds = new DocumentStore { Url = "http://localhost:8080/" }.Initialize())
{
    using (var session = ds.OpenSession("RavenDBFirstSteps"))
    {
        Tea tea = session.Load<Tea>("teas/1");
        tea.Name = "Earl Grey II";
        session.Store(tea);
        session.SaveChanges();
    }
}

The deletion of a product is similar, simply the Delete() method must be called.


using (var ds = new DocumentStore { Url = "http://localhost:8080/"}.Initialize())
    {
        using (var session = ds.OpenSession("RavenDBFirstSteps"))
        {
            Tea tea = session.Load<Tea>("teas/1");
            session.Delete(tea);
            session.SaveChanges();
        }
    }

The ability to use Linq to query the data is one the most powerful pros of RavenDB. In this case the search queries all black teas.


using (var ds = new DocumentStore { Url = "http://localhost:8080/" }.Initialize())
    {
        using (var session = ds.OpenSession("RavenDBFirstSteps"))
        {
            List<Tea> dbTeas = (from teas in session.Query<Tea>()
                                where teas.TeaType == TeaType.Black
                                select teas).ToList<Tea>();
 
            return dbTeas;
        }
    }

One of the major concerns about NoSQL databases in comparison to SQL databases is, that a model (in this example the model Tea) cannot be changed without having a loss of data. For Raven is this not a problem at all. I added a new property to the model and the code is working without errors or loss, but – of course – the new property is always null until a new entity is saved. It had really surprised me if this wouldn’t be possible.

So that’s it, a very first step. In another blog post I will take a deeper look at handling complex data structures and performance in comparison to sql server compact or SQLite.

The example project can be found on GitHub.

Advertisements
This entry was posted in C#, Development, NoSQL and tagged , . Bookmark the permalink.

One Response to NoSQL: First steps with RavenDB and .NET

  1. alois says:

    The best easy simple to understand RavenDB project for starters so far

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s