Using a document database (RavenDb) from within Umbraco

I wanted to experiment with a document database and hopefully make use of it in a project of mine. Living in a .Net context and reading many good things about RavenDb the choice was pretty easy to make.

The thing that made me want to try a document database in the first place was the very comfortable way of storing common objects right in the DB without defining tables and fields. Just like this:

public class Product
{
    public string Id { get; set; }
    public string Name { get; set; }
    public decimal Cost { get; set; }
    public DateTime CreateDate { get; set; }
}
public void StoreNewProduct()
{
    var product = new Product
    {
        Name = "Car",
        Cost = 100000,
        CreateDate = DateTime.Now
    };
    using (var DbSession = MyDb.Store.OpenSession())
    {
        DbSession.Store(product);
        DbSession.SaveChanges();
    }
}

And that is indeed all that is necessary to store an object into a RavenDb database. The Id field is mandatory and is filled automatically by Raven, it will contain something like “products/2041”.

Basic querying of the database is as easy as Linq (here in a Razor script):

var products = from product in session.Query<Product>()
                     where product.Cost>10000 select product;
<table>
@foreach (var p in products)
{
 <tr>
   <td>@p.Id</td>
   <td>@p.Name</td>
   <td>@p.Cost</td>
   <td>@p.CreateDate.ToShortDateString()</td>
  </tr>
}
</table>

For more advanced queries, and with a lot of documents in the DB its adviced to query using Lucene which comes with RavenDb. See for example http://ravendb.net/tutorials/hello-world . (Note that there’s a typo in that sample, the Lucene query should be made with “session.Advanced.LuceneQuery”).

If we later decide to add another field to the Product class, that’s no problem, objects will be retrieved with the data available.

Now how do we setup this in Umbraco?

Download and start RavenDb server

I did it the quick way and run RavenDb as a service in my own server. It’s as easy as downloading the RavenDb package and running one single command (/Server/Raven.Server.exe /install) and it serves the Db on http://localhost:8080.

There’s also a web ui at that url from which one can administer the database and documents.

Initialize the database within the Umbraco application

For RavenDb to run In Umbraco it’s best to initialize a global instance of DocumentStore on ApplicationStart. For this test I made that happen with a simple class file in App_Code:

using Raven.Client.Document;

namespace joeriks.raven
{
    public class MyApplicationBase : umbraco.BusinessLogic.ApplicationBase
    {
        public MyApplicationBase()
        {
            MyDb.Store = new DocumentStore { Url = "http://localhost:8080/" };
            MyDb.Store.Initialize();
            MyDb.IsInitialized = true;
        }
    }
    public class MyDb
    {
        public static DocumentStore Store;
        public static bool IsInitialized = false;
    }

Binaries

The final thing that’s necessary is to add the RavenDb client dll’s to the Umbraco /bin directory. And then voila – all good to start using raven for example from within razor macros.

References

http://ravendb.net/tutorials/hello-world
https://github.com/ravendb/ravendb

Advertisements

7 thoughts on “Using a document database (RavenDb) from within Umbraco

  1. Just a idea… Ever thought of publishing umbraco documents to ravendb? To make umbraco published content available outside umbraco?

  2. Interesting thought – I guess that would be possible with the use of some api events. Ie create / update a raven document on publish and remove it on delete.

  3. One typical mistake:, Gibline is a?On by wrong, reading this right.And ultraviolet B, loss pills are.Duck and goose florists in fort collins, successful recoveries of because a method.Amount of confusion, many illnesses and.,

    • Ismail,
      Thanks for asking – was a long time ago I wrote the article , I’d probably use route hijacking today – or even create a custom default controller for all.

Leave a Reply

Please log in using one of these methods to post your comment:

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