Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

When building a website with NancyFX by
default views are loaded from the file system - pretty much like with
all MVC-based websites. While NancyFX also supports loading views
embedded in assemblies as resources , both options require re-deploying
of actual files when something in the view needs updating. Even with
fully CI environments, that is still sort of a PITA.

Here is how to use RavenDB to override views
in a live website without re-deploying anything. Basically what this
does, thanks to NancyFX's modular and flexible design, is take the
default ViewLocationProvider and encapsulate it, reading all the views
from the original location (file system or assembly resources), and give
precedence to views loaded from RavenDB.

The code featured here makes 2 assumptions:

1. A document name convention for view
documents is preserved - basically some prefix (for example
"MyWebsite/") used to prevent polluting the document store and then the
full view name (location + name + extension). When loading all available
views, we use a filter to make sure we load only views with a supported
extension (determined by the installee view-engines). A view-template
document in RavenDB will then have an ID similar to
"WebsiteViews/Views/Home/Read.cshtml".

2. There are less than 1024 views stored to RavenDB. This is probably safe to assume, or you have some monstrous website.

There's one bit missing here - view-cache
invalidation. By default NancyFx will cache all views it
loaded indefinitely, as far as I can tell. That's bad for us, because
when you update a template in your RavenDB store you do want to
invalidate all caches, or at least one specific template you updated.
This is something I'll keep for another post.

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.