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 and easy to get up and running. However I’ve heard from fellow developers that they think there is a high 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 MVC application.
All you need installed is MVC 3 (steps are the same for MVC 4) and NuGet.
- Create new MVC 3 project empty template
- Install Ninject NuGet package
- Install Ninject MVC3 NuGet package
- Install RavenDB Embedded NuGet package
- Add Ninject Module for RavenDB Embedded
- Add your Ninject Module to NinjectWebCommon.cs
- Add App_Data folder
- RavenDB Management Studio
- Use RavenDB in your MVC application
Create new MVC 3 project empty template
File -> New Project -> select ASP.NET MVC 3 Web Application template -> Empty
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.FSharp unless you have F# runtime installed.
Add Ninject Module for RavenDB Embedded
Create a new class called RavenDBNinjectModule.cs and add the following code:
The first binding to IDocumentStore binds using a simple method. Using NonAdminHttp.EnsureCanListenToWhenInNonAdminContext(8080) means if you are running Visual Studio as an non admin the first time you run the site you will get a 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 a 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 and 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.
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:
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.
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.
To the root of your application
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 a IDocumentSession parameter.
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 the EmbeddableDocumentStore to save documents in memory.
I also videoed the 20 minutes or so I spent creating the example application.