A one file Asp Net web application with NancyFx and SisoDb

It’s hard to see how an Asp Net webapp with database functionality can be easier/less ceremony than with the help of NancyFx + SisoDb. The sample I’m going to show here is very far from a full real web application, it’s rather trying to show one way of creating the most basic building blocks of such an app.

Followup article : one file full CRUD sample.

To join my sample here start by creating an empty Asp Net web application within Visual Studio. And then add the two packages Nancy.Hosting.Aspnet and SisoDb. Just as easy as opening Tools > Library Package Manager > Package Manager Console. And then run the two install commands:

install-package Nancy.Hosting.Aspnet
install-package SisoDb

Nancy

A basic Nancy app can be setup with just one cs-file (a NancyModule class):

public class NancyStart : Nancy.NancyModule
public NancyStart()
{
    // Todo: Initialize db
    Get["/products"] = x =>
    {
       return "<h1>my products</h1>";
    };
}

The Nancy Nuget gave you a ootb basic routing configured in your web.config. So with the code above you should be able to browse to /products and get the h1.

With a small change we get the idea of how we’re going to get the actual products:

Get["/products"] = x =>
{
    return htmlForm + htmlListProductNames();
};

And a post:

Post["/products"] = x =>
{
    var htmlResult = InsertProduct(Request.Form["name"]);
    return htmlResult + htmlForm + htmlListProductNames();
};

The htmlForm and htmlListProductNames are just strings with html tags:

string htmlForm = @"
    <form method=""Post"">
        <input type=""text"" name=""name"" id=""name""/>
        <input type=""submit""/>
    </form><br/>
";

SisoDb

To handle the db as easy as possible SisoDb is a neat alternative. It’s storing regular poco’s as json objects in a regular sql database, which means there’s no need for setting up sql schemas for your pocos.

Here’s my Product class:

public class Product
{
    public Guid SisoId { get; set; }
    public string Name { get; set; }
    public Product(string name)
    {
        this.Name = name;
    }
}

And here’s how to store a product into the DB:

public static void InsertProduct(string name)
{
    using (var uow = db.CreateUnitOfWork())
    {
        var p = new Product(name);
        uow.Insert(p);
        uow.Commit();
    }
}

The first time you’re saving a product SisoDb will create necessary db schemas automatically.

To get a list with all products, and return a basic html list, we can do:

string htmlListProductNames()
{
    IEnumerable<Product> products;
    var retval = "";
    using (var uow = db.CreateUnitOfWork())
    {
        products = uow.GetAll<Product>();
        retval = "<ul>";
        foreach (var p in products)
        {
            retval += "<li>" + p.Name + "</li>\n";
        }
        retval += "</ul>";
    }
    return retval;
}

The full source for my noob app can be found at gisthub. Comments are more than welcome. I’m a 100% Nancy and SisoDb noob and there’s probably many things that could be made better.

View Engines

There’s quite a few ViewEngines avaliable for NancyFx, there’s for example Spark, NDjango and Razor. All requires the install of an additional dll (via a Nuget of course – I’m starting to get very lazy about dll’s, if they aren’t nugetted I probably wont use them…). When I was browsing for the Razor dll I found out there’s indeed an included view engine in Nancy – the Super Simple View Engine. For the purpose of this sample that view engine is good enough:

string htmlListProductNames()
{
IEnumerable<Product> products;
var retval = "";

using (var uow = db.CreateUnitOfWork())
{
    products = uow.GetAll<Product>();
    dynamic model = new System.Dynamic.ExpandoObject();
    model.Products = products;

    var template = @"
<ul>
@Each.Products
<li>@Current.Name</li>
@EndEach
</ul>
";
    // declared in the function for brievity
    var viewEngine = new Nancy.ViewEngines.SuperSimpleViewEngine viewEngine()
    retval = viewEngine.Render(template, model);
}

return retval;
}

WebMatrix?

Using WebMatrix its also very easy to get started with NancyFx. You just need to add the Nuget and start coding. Have a look:

Two notes here : I’m creating a temporary dummy foo.cshtml to be able to start my site in a browser. To be able to go to the /_Admin/ page from where the Nugets can be installed. Also I’m storing the NancyStart.cs file in App_Code, which means I can edit it more on the fly than if I had it in a precompiled .cs.

Ref :
Introducting Nancy (Elegant Code)
Nancy Fx @ GitHub
SisoDb – a Simple-Structure-Oriented-Db

A very nice NancyFx introduction : Building a photoblog with NancyFX and Simple.Data Part 1: Setting up the project

Hope you liked the post. Happy coding!

About these ads

One thought on “A one file Asp Net web application with NancyFx and SisoDb

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s