Updated 01 Oct 2012 for version 1.0.960

About a month ago now I used RavenDB for the first time in a 'real' application. I think it's awesome, frictionless to get up and running. However, I've heard from fellow developers that they think there is a higher barrier to entry when compared to a traditional RDBMS approach such as SQL server.
Yes, when learning anything new, there is a learning curve, but this post is to dispel the myth that setting up RavenDB is hard and to show you what you get for pretty much zero effort.

There are two flavours of RavenDB server and embedded. I'm going to show you in this post how to set up RavenDB embedded inside your ASP.NET MVC application.

Prerequisites

All you need installed is MVC 3 (steps are the same for MVC 4) and NuGet.

Create new MVC 3 project empty template

File -> New Project -> select ASP.NET MVC 3 Web Application template -> Empty

MVC3 Template

Empty Template

Install Ninject NuGet package

Tools -> Library Package Manager -> Package Manager Console PM> Install-Package Ninject

Install Ninject MVC3 NuGet package

PM> Install-Package Ninject.MVC3

Install RavenDB Embedded NuGet package

PM> Install-Package RavenDB.Embedded Remove the reference to Raven.Client.Lightweight.FSharpunless you have F# runtime installed.

Add Ninject Module for RavenDB Embedded

Create a new class called RavenDBNinjectModule.cs and add the following code:

using Ninject; using Ninject.Modules; using Ninject.Web.Common; using Raven.Client; using Raven.Client.Embedded; using Raven.Database.Server; namespace RavenDBInFiveMinutes { public class RavenDBNinjectModule : NinjectModule { public override void Load() { Bind<IDocumentStore>() .ToMethod(context => { NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080); var documentStore = new EmbeddableDocumentStore { DataDirectory = "App_Data", UseEmbeddedHttpServer = true, }; return documentStore.Initialize(); }) .InSingletonScope(); Bind<IDocumentSession>().ToMethod(context => context.Kernel.Get<IDocumentStore>().OpenSession()).InRequestScope(); } } }

The first binding to IDocumentStore binds using a simple method. Using NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080) means if you are running Visual Studio as a non admin the first time you run the site, you will get an UAC prompt to allow RavenDB to listen on port 8080 (default port) rather than a failure.
UseEmbeddedHttpServer = true enables the RestAPI and RavenDB Management Studio. The rest of the code should be self-explanatory.

You can think of a DocumentStore as an Initialized Raven database and therefore should be created once per application, hence .InSingletonScope() You can change the configuration of the EmbeddableDocumentStore by setting properties for example DataDirectory = "new path here" check out RavenDB's documentation for available configuration options.

The second binding to IDocumentSession again binds using a simple method that just calls OpenSession() from the initialized EmbeddableDocumentStore, this is your session to RavenDB. Generally you want to work with one session per request, hence .InRequestScope()

Add your Ninject Module to NinjectWebCommon.cs

When you installed the Ninject MVC3 package, you might have noticed it created a folder called App_Start. App_Start is for WebActivator and the code in this folder will run before the code in Global.asax.cs. If you don't know about WebActivator, it's well worth reading David Ebbo's post.

NinjectWebCommon.cs

In the App_Start folder, the Ninject MVC3 package created a file called NinjectWebCommon.cs. We just need to change it to load our RavenDBNinjectModule. At the bottom of the file there is a method called RegisterServices change it to the following:

private static void RegisterServices(IKernel kernel) { kernel.Load(new RavenDBNinjectModule()); }

Add App_Data folder

This is optional as you can store RavenDB in any location but a good place to store RavenDB is in the App_Data folder.

Add App_Data folder

RavenDB Management Studio

The last and optional task is to enable RavenDB Management Studio. RavenDB Management Studio is great and simplifies tasks such as maintaining documents, managing indexes and testing queries.

All you need to do to enable RavenDB Management Studio is copy the Silverlight Raven.Studio.xap to the root of your application. Raven.Studio.xap can be found in the packages' folder. The packages folder isn't part of your Visual Studio project, so you will need to find the folder in Windows Explorer.

Copy packages\RavenDB-Embedded.1.0.888\lib\net40\Raven.Studio.xap To the root of your application

Windows Explorer Raven Studio Visual Studio Explorer Raven Studio

Now you can use RavenDB Management Studio by pointing a browser to http://localhost:8080

Don't forget to use UseEmbeddedHttpServer = true when initializing the DocumentStore.

Use RavenDB in your MVC application

Now we have the RavenDB bindings in Ninject MVC3, RavenDB will be available to all our controllers. So using RavenDB in our application is as trivial as creating a controller with a constructor and an IDocumentSession parameter.

For example

public class HomeController : Controller { private readonly IDocumentSession _documentSession; public HomeController(IDocumentSession documentSession) { _documentSession = documentSession; } }

Conclusion

I hope this blog post shows that it is easy to get up and running with RavenDB in no time at all. It should also demonstrate how far you can get with just installing a couple of NuGet packages.

Based on this post, I’ve spent a further 20 minutes creating an example MVC application that saves basic information about Movies. The example also shows how in your tests you can configure EmbeddableDocumentStore to save documents in memory.

My example application is on GitHub https://github.com/DalSoft/RavenDBInFiveMinutes and can also be downloaded as a zip file.

I also recorded the 20 minutes or, so I spent creating the example application.