NancyFX

Introduction to NancyFx

The Nancy project began in 2009 and is primarily maintained by Andreas Håkansson and Steven Robbins.  It has become a very popular framework, known for being light-weight, simplistic and fast.  This Code Review will take a close look at Nancy and see where it fits in the marketplace.

What is Nancy?

Nancy is a Sinatra-inspired lightweight, low-ceremony, framework for building HTTP based services on .NET and Mono.  The goal of the framework is to stay out of the way as much as possible and provide a “Super-duper-happy-path” to all interactions.  Everything in Nancy is set up to be as simple and effective as possible, utilising sensible defaults and conventions instead of making developers jump through hoops just to get up and running.

Nancy isn’t a Silver bullet. That said, neither is ASP.NET MVC / WebAPI. Every framework has it’s strengths and weaknesses, the import thing is to evaluate the options and apply each when suited. Nancy doesn’t want to be the “everything” framework, because then it would end up with several of the design decisions that helped forge the ASP.NET stack. There are major disadvantages when trying solve everyone’s problem with a single solution, which is a big part of why Nancy exists in the first place.

What is the Super-Duper-Happy-Path?

The “Super-Duper-Happy-Path” (SDHP) is a phrase coined to describe the Ethos of Nancy.  The basic ideas behind it are:

  • Things should Just work – and should be ready to use out of the box.  Adding new modules, dependencies, view engine should all be configured and set up for you.
  • Customisability – Even though things should work out of the box they should also be flexible so that they can work with other components (IoC containers and so on).
  • Low Ceremony – Nancy should be behind the scenes and near invisible to developers.  Nancy believes that the developer should only need to write minimal “Nancy code” in order to build an application.
  • Low Friction – Relating to the above point, Nancy should “Help” the developer build his application and should not show any resistance to the developer.  Naming should be obvious, configuration should be minimal, but power and extensibility should be available.

The Nancy Stack

https://app.pluralsight.com/player?course=nancy&author=richard-cirerol&name=nancy-04-bootstrapper&clip=0&mode=live

https://github.com/NancyFx/Nancy/wiki/Bootstrapper

What are Nancy Modules?

Interactions with a Nancy application are typical HTTP session scenarios.  That is, a Request message is sent to the server, the server receives the Request, processes the Request, creates a Response, then returns that Response.  Within a Nancy application, this process is controlled by the Nancy Engine with the use of Modules.

Modules are the linchpin of any Nancy application in that they define how Nancy should behave at runtime.  However, Modules do more than just allow you to define behaviour , they also give access to the Request context, Response helpers and View renderings.  There is no limit as to how many modules can be defined in a Nancy application as long as there is at least 1 with at least 1 route defined.  To keep it simple, Nancy Modules define Routes (Method + Pattern + Action + Optional Condition) and Pre/Post Request hooks.

Nancy Modules are easily registered, in that they are globally discovered for you on start-up, which means the required configuration is kept minimal (if not non-existent for the developers point of view).

The Nancy Engine

The Nancy Engine is the brains that the Host delegates requests to.  As mentioned in the “What are Nancy Modules” section, Nancy deals with typical HTTP session scenarios which means it need to deal with Requests, processing Requests and  return Responses – the Nancy Engine manages this life-cycle.  When the Engine receives a Request from the Host, the engine will:

  • Create the Request context
  • Invoke the Pre-Request hook
  • Look for a valid Route to handle the Request (Modules)
  • Invoke the Post-Request hook

The Bootsrapper

Internally, Nancy uses the bootstrapper to initialise all required modules and the DI container that will be used by other core modules.  Nancy comes with it’s own custom-built TinyIoC container “Out of the box” , but of course Nancy provides alternatives with support for Ninject, StrucutreMap, Unity and Windsor.

Testing in Nancy

Nancy provides a testing library that allows testing of the full request/response cycle in a very easy and readable way.  Not only can you test that your request returns a model that you expect, you can test that when you pass in accept headers the response is in the format you expect.  E.g.

[Fact]
public void GetData_WhenRequested_ShouldReturnOKStatusCode()
{
    var browser = new Browser();
    var response = browser.Get("/GetData", (with) =>
    {
        with.Header("Authorization", "Bearer johnsmith");
        with.Header("Accept", "application/json");
        with.HttpRequest();
    });

    Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
}

This kind of thing is not very easy to test in MVC without it being a full integration test whereas Nancy has no dependencies on System.Web or MVC so it can provide a Response without hitting a server.

Customizability

One of Nancy’s core features is its extensibility – it is designed to be loosely coupled for greater customizability.  For example, if you would like to implement your own INancyEngine and completely change how Nancy Handles requests, you can do that.  There are also a set of pre-defined conventions that you can swap in/out if you want Nancy to do something different than what comes as standard.  Everything is very easy to modify which offers people looking to create a 3rd party library a great many options.

Performance

 

End-To-End Benchmarks

WebServiceBenchmarksFrameworks

End-to-End Results 
Framework Client Number of Requests Time (ms)
 ServiceStack  JsonServiceClient (Recommended)  10000  5973 ms
 WebAPI  HttpClient (Recommended)  10000 8099 ms
 NancyFx  HttpClient (Recommended)  10000  7988 ms
 ServiceStack  RestSharp  10000  1791 ms
 WebAPI  RestSharp  10000  2274 ms
 NancyFx  RestSharp  10000  2068 ms

Pricing

All of Nancy is built, by the community, as an open source framework, meaning you get full access to the source code, and it is licensed under the MIT license.  It is completely free with no restrictions.

Conclusions

By no means should Microsoft’s MVC be the default for developers looking to build web applications, and neither should Nancy.  Nancy makes no claims to be a “Silver bullet” and quite successfully fills a gap not fully filled by MVC.  Nancy is not only free to use and actively developed, it is a Cross Platform framework that offers a lot in terms of performance, ease of development, and customizability.  Nancy offers a lot with its community and support.  The community is very active on StackOverflow and Jabbr and Nancy currently has over 243 contributers on Github.  This says a lot about a framework that is effectively free to use and primarily maintained by two people.

Long story short, Nancy is a fantastic framework to work with when building a Web Service.  It’s easy to build upon, easy to extend, and provides a great deal of community-driven support.

You may also like...

Leave a Reply