Adding a RavenDB persistence layer to an existing application

Posted : Monday, 19 March 2012 19:48:12

I recently read an article about RavenDB and it sounds awesome! If you don’t know too much about NoSQL or document based databases then have a bit of a Google of it or maybe just head straight to the RavenDB project page. It’s got a fairly comprehensive documentation with examples and updates being added all the time. I wanted to see how easy it was to add RavenDB based persistence to an existing application and this blog post is a summary of the experience.

A while ago I did a series of posts around persistence ignorance using an online auction application. For this post I used the same application and added a RavenDB based persistence layer to it. The first thing to do is install RavenDB – according to the documentation the recommended way is to use Nuget however I didn’t use Nuget for the other persistence technologies so for this post I downloaded the binaries, installed them manually then copied the required client assemblies into my solution. I added the relevant references and stub classes (for details of the required classes and application structure please consult the link at the start of this paragraph) to my new class library project to create the project structure shown below:

project structure_thumb[1]

The compiler raised a design-time error stating I had to add a reference to System.Component.Model, I am guessing that this is required by RavenDB so added it and the project built successfully. Next I implemented RavenDBModule which is responsible for wiring up the persistence layer. RavenDB has been designed to operate in a similar fashion as NHibernate in terms of sessions – in the case of RavenDB the recommendation is to create a single instance of IDocumentStore per application and use that to create Sessions as necessary. In the RavenDBModule code I wire up Ninject to use a single instance of IDocumentStore and inject it into the constructor of each repository:

   13         public override void Load()

   14         {

   15             //check there is a connection string

   16             var connectionStringSection = ConfigurationManager.ConnectionStrings["LightsCameraAuctionRavenDB"];

   17             if (connectionStringSection == null || string.IsNullOrEmpty(connectionStringSection.ConnectionString))

   18                 throw new ApplicationException("no LightsCameraAuctionRavenDB connection string defined");


   20             Bind<AuctionService>()

   21                 .ToSelf()

   22                 .WithMetadata(MetadataBindingParamName, MetadataBindingParamValue);


   24             Bind<IDocumentStore>()

   25                 .ToMethod(ctx =>

   26                               {

   27                                 var store = new DocumentStore { ConnectionStringName = "LightsCameraAuctionRavenDB" };

   28                                   store.Initialize();

   29                                 return store;

   30                               })

   31                 .InSingletonScope();


   33             //..code omitted for brevity

   34         }

Below is the implementation of RavenDBBidderRepository:

Post a comment Sharespacer Tagged: POCO, PersistenceIgnorance
  • (This will not appear on the site)