Tags

, ,

RavenDBIn this blog post I will walk through how to host an MVC application which uses RavenDB as the back-end document database hosted up on Appharbor, Appharbor now allows us to host RavenDB in the cloud.

In the blog post I am going to make several assumptions, the post itself is more to do with getting an application deployed and running up on AppHarbor using RavenDB as the document database.

I am assuming you have you have a github account and know how to get your code up onto github, an Appharbor account and know how to deploy your code to github to Appharbor.

If you need some help with doing this then check here.

Ok lets create a brand new MVC Web application project, I used MVC 3 application

Raven2

And then I chose the internet template.

Raven3

You can either download RavenDB from RavenDB.net or use Nuget to get RavenDB. For the demo I just installed it using Nuget
as follows, within Visual Studio goto Tools, Library Package Manager and then select Package Manager Console and then type:-

Raven

RavenDB is now downloaded using NuGet and you will find it within the packages folder in the same location on disk as your project solution files, to start RavenDB locat the packages folder and then go to the RavenDB.1.0.616 folder and then the Server folder and double click on Raven.Server.exe as below:-

Raven4

By default RavenDb runs on http://localhost:8080, once started you can view the Raven Studio from within a browser, and you should see something like this:- (if it says Create Sample Data then click on the button to create sample data)

Raven10

Moving back to our MVC Application we now need to wire a couple of things up to use RavenDB as below:-

In the global.asax add in the following in the Application_Start event:-

 
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    var parser = ConnectionStringParser<RavenConnectionStringOptions>.FromConnectionStringName("RavenDB");
    parser.Parse();

    Store = new DocumentStore
    {
        ApiKey = parser.ConnectionStringOptions.ApiKey,
        Url = parser.ConnectionStringOptions.Url,
    }.Initialize();
}

Add in a connection string in your web.config as follows:-

 
<connectionStrings>
    <add name="RavenDB" connectionString="Url=http://localhost:8080" />
  </connectionStrings>

All new Controllers will inherit from RavenController which is below:-

 
public class RavenController : Controller
{
    public IDocumentSession RavenSession { get; private set; }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.IsChildAction)
            return;
        RavenSession = MvcApplication.Store.OpenSession();
        base.OnActionExecuting(filterContext);
    }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.IsChildAction)
            return;
         using (RavenSession)
         {
             if (filterContext.Exception != null)
             return;

             if (RavenSession != null)
             RavenSession.SaveChanges();
         }
     }
}

Below is some example code for some basic CRUD.

 
public class AlbumController : RavenController
    {
        public ActionResult Index()
        {
            var model = this.RavenSession.Query<Album>().ToList();
            return View(model);
        }

        public ActionResult Create()
        {
            var model = new Category();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Album album)
        {
            this.RavenSession.Store(album);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(string id)
        {
            var model = this.RavenSession.Load<Album>(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(Album album)
        {
            this.RavenSession.Store(album);
            return RedirectToAction("Index");
        }

        public ViewResult Details(string id)
        {
            var model = this.RavenSession.Load<Album>(id);
            return View(model);
        }

        public ActionResult Delete(string id)
        {
            var model = this.RavenSession.Load<Album>(id);
            return View(model);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            this.RavenSession.Advanced.DatabaseCommands.Delete(id, null);
            return RedirectToAction("Index");
        }
    }

Within Appharbor choose RavenDB as an add-on, appharbor will provision you with a connection string if you choose
RavenDB as an add-on, wait a couple of seconds and you will be supplied with a connection string for your RavenDB instance in the cloud.

Step 1

Step1

Step 2

Step 2

Step 3

Step 3

Step 4
Step 4

Step 5
Step 5

This setting is the one you will change to within your web.config, for testing locally use http://localhost:8080 but for deploying to the cloud use the connection string setting supplied to us by Appharbor.

Getting RavenDB from NuGet means quite a large download, we dont want to have to upload all of this content to github so we can do a nice little thign within our solution, roght click on the entire solution within Visual Studio and choose:-

Package Restore

This means our github repository doesnt have to contain all of the Nuget packages and will pull them down for us when building.

Once your code is building and running locally send it up to github and use the service hook to deploy to Appharbor.

If you come across any build errors then you may have to turn customerrors=”Off” in your web.config as well as checking the build log on Appharbor to see what went wrong.

At this point we should have a working MVC application which is using RavenDB as the back-end document database – sweet.

If I missed anything or anyone has any questions or comments please tweet me @gsuttie or leave a comment.