Creating a REST API with ASP.Net MVC and RavenDB

Mariusz Plaskowicki

With the second day of our dev camp coming to an end it’s time to summarise our server side work.  I’ve been working with Neil on setting up the server side part of the application while Tomasz was working on the client side. We decided that we are going to use standard MVC controllers to implement a RESTful service and RavenDB for data storage. RavenDB is a nosql database server, which stores information as JSON “documents”. These documents can have any form and there is no schema to adhere to so the storage is extremely flexible, ideal for our questionnaire application where the questions and answers we are storing will vary a great deal between instances. RavenDB is a .Net nosql implementation so getting it up and running is nice and quick.

Getting the database working with ASP.Net MVC was also pretty easy and fast, on the other hand using the MVC controllers as REST services wasn’t that straightforward. Initially, we had the choice of using MVC controllers or the WCF Web API. There was a third option of using signalR but because we decided to use Backbone.js for client side and it prefers the REST approach for client-server communication we had to stick with REST. After examining both controllers and the Web API we decided to use the controllers because it was more straightforward and we only needed a simple solution.

As Luke mentioned in previous blog notes we decided to give the Mercurial a try for our source control. We haven’t used it before and having heard that it’s really nice we decided to give it a go and see how it plays with small team over a 5-day project. We decided to try and use BitBucket as a hosting place. Setting it up was quite easy so in no time we had working repo with some initial code inside. Unfortunately because we had no experience with Mercurial we hit the wall quite fast and got a bit lost with what do you really need to do in order to use it properly. First day was hard but now I do believe we are starting to catch up with the idea and it’s going to be much better.

We ended day one with basic MVC + REST + RavenDB working and talking to the backbone.js test environment that I’ve hacked together in order to test if all works. For day two we planned to finish the services implementation and class definition. Easy. Or so we thought.

Getting client talking to the REST was one thing, getting it to talk to REST and send the right data was a completely different story. And not so easy. We needed to send to the service a response object along with the answers to the questions that has been presented to the user. Each question could have a different kind of answer – text for text boxes, bool for checkboxes, int for sliders and more complicated data for more advanced rating controls. So we decided to subclass the question types and put them all in questionnaire’s collection.

This is where the problems started to appear because MVC serialiser didn’t wanted to serialise our JSON into proper classes. We tried some different approaches but every one of them had some drawbacks and did not work in the way we wanted. In the end we got back to the initial idea of subclassing but we decided to write our own binder instead. To our great surprise we ended up with a binder that created the correct object, returned it back to the MVC internals and broken. Frankly I don’t really know what happens between the binder creating the object and the MVC action receiving it but certainly something happens. So in the end we decided that we don’t want to spend more time struggling with some weird MVC issues and we just reconstructed the object from the post data inside the action method. It’s obviously wrong but it will have to do for now, and I’m going to look for a solution to this problem when I have more time.

By the end of the second day we had pretty much everything on the server side up and running. For tomorrow we are going to try and help Tomasz with some backbone.js development and general client side development. I’m eager to put my hands on some nice canvas related controls, but first we need to finish setting up the entire backbone stuff and figure out how to render the views for our controls.