adam-gligor monologue about stuff, brain dump

Web apps with NancyFx and Sqlite

There are frameworks that are suited for simple apps and I got the opportunity to build one. Here are a few words about a less known web framework NancyFx and Ratchet library for the ui.

Context

A collaboration between my employer and a local organization lead to me becoming the teacher for a group of high school students during the summer. We’re going building a small functioning web app backed by a database. Developing web apps on .NET means ASP.NET and MSSQL but this time I wanted something more lightweight. Enter NancyFx …

NancyFx

NancyFx is a web framework inspired by the Sinatra, the web framework for Ruby.

NancyFx the spec sheet

  • Lightweight & low ceremony
  • Convention over configuration
  • Built in dependency management
  • Runs on .NET and Mono
  • Runs (or will run) on .NET Core
  • Supports Razor views

NancyFx vs ASP.NET MVC at first glance.

NancyFx as everything these days is distributed through Nuget. One of the first striking differences is what you end up with after setting up a new empty project. In case of Nancy that’s three code files, that’s all you need for a hello world app. Now granted that Nancy won’t solve the same number of problems as ASP.NET without significant plumbing, extension libraries etc but if you are after low entry barrier that’s an A+.

Sqlite

Every app needs persistence eventually. I wanted an embedded engine that did not require separate installation and worked with entity framework. LocalDb and SqlCompact are the offerings from Microsoft but Sqlite was better suited. It’s very popular in the mobile devices but not that uncommon in low traffic websites. Best part: install a nuget package, job done you have a sql engine. The less good is that the database file and schema has to be created manually as the entity framework provider does not do data migrations yet, perhaps a future version.

Here’s a good podcast on Sqlite where it’s author talks about the history and interesting facts.

Sqlite the spec sheet

  • No server process
  • Installed though Nuget
  • Has entity framework provider
  • Hugely popular
  • Official support in entity framework 7

Ratchet

Bootstrap is what you’d normally use for web app these days. Ratchet is advertised as a prototyping tool for quickly building up mobile apps. What made me choose it, good documentation and visual examples for every component in the framework. Just copy paste the html fragments and arrange them to suit your needs.

Ratchet the spec sheet

  • Ajax links via push.js
  • Mobile(ish) look and feel
  • Image gallery

The app

Since this combination of frameworks was new to me too, I decided to do my homework before teach this to someone else, so I ended up building a prototype.

The theme: flash-cards … it’s a learning tool to help you memorize bits of information that can be written onto cards.

I won’t go into details of the implementation, check out the code on github.

Tips & things to watch out for

Razor views

This doesn’t work without a bit of sorcery, luckily the Nancy docs to the rescue.

I had to do two things. First is specifying the assemblies and namespaces of the views in a special section in the web.config, second install two packages: Microsoft.AspNet.WebPages, Microsoft.Web.Infrastructure. Without these changes you’ll get false compile errors that are annoying. Both issues are mentioned in the docs.

Sqlite connection string

Setting up the sqlite connection proved to be trickier than I anticipated. What happens is that Nancy has its own root path provider. To be able to find the database file and connect to it you’ll have to hack the connection string from the program and use that path provider to create an absolute path, I haven’t been able to make it work with relative paths.

Azure deployment

A web app is a web app only when it’s deployed and running on a server, this case azure. Small web apps can be hosted freely in azure. The Sqlite database is deployed alongside the web app, so nothing special has to be set up for that.