NANCY Sinatra – An instance of Ruby's influence in .NET – Runs just about anywhere



NANCY Sinatra – An instance of Ruby's influence in .NET – Runs just about anywhere

0 0


NancySinatraLightningTalk

Nancy Sinatra Lighting talk for OKC Ruby

On Github davidroberts63 / NancySinatraLightningTalk

 

NANCY Sinatra

An instance of Ruby's influence in .NET

Presented by David Roberts / @davidroberts63

Sinatra

A DSL for quickly creating web applications in Ruby.

Nancy

Lightweight low ceremony web application framework
get "/sayhello/{name}" do
  "Hello " + {name}
end
public class HomeModule : NancyModule
{
  public HomeModule()
  {
      Get[″/sayhello/{name}″] = 
            input => return ″Hello ″ + input.Name;
  }
}
I believe many of us here are somewhat familiar with Sinatra ruby. What you see here is a simple Sinatra app. Run this in ruby and you have a working website. Nancy is a simple web application framework for .NET. Here you see a simple Nancy app. There is additional structure because of the static typing of C# but overall similar to Sinatra. Nancy supports most of the rout matching that Sinatra does. Such as regular expressions, optional, default values, constraints such as integer, date time, min/max, and customized such as email.

Tweet App

Using ScriptCS

scriptcs -install ScriptCs.Nancy;
'Require<NancyPack>().Get("/",_=>"Hi").Host();'|Out-File s.x;
scriptcs s.x #dropsmike @NancyFx @scriptcsnet
Most of the time writing a Nancy app would look like what you just saw. A Visual Studio project with that NancyModule in place. However with the creation of ScriptCS it can get smaller in some uses. Briefly, ScriptCS is a scripting environment for C# and it also has a REPL environment. What you see is an entire web application in Nancy. Installing the dependencies, creating the application file, and running it. All fitting within a tweet with enough room for shout outs to Nancy and ScriptCS.

Runs just about anywhere

Uses its own Request/Response objects.Not dependent on ASP.NET

  • OWIN
  • Self Host
  • ASP.NET
  • Nginx, RaspberryPi ...
Nancy is self contained in that it doesn't depend on ASP.NET like MVC does. It can be run in many different types of hosts such as OWIN, ASP.NET and others. You can even self host it in an executable or on Linux in Mono. There is also documentation on the Nancy site to run it on RaspberryPi or Nginx.

WCF

Ewwww.

Note, WCF NOT inspired by Ruby. Because Ruby wouldn't do that.

S. D. H. P.

  • It Just Works
  • Easily Customizable
  • Low Ceremony
  • Low Friction

Super Duper Happy Path

The SDHP is an idea created in the Nancy framework that makes the development process much simpler from a .NET perspective. Most of what the SDHP does is similar to things done in Ruby. So what is the SDHP? It's the Super Duper Happy Path. This is a happy path that most of us would give up coffee for. The rest of us…drink tea. If your class needs a dependency just define it and use it. Nancy uses dependency injection (TinyIOC by default) for all your modules and other classes. Don't want TinyIoC put in your own. Most of the well known .NET IoC projects already have Nugets ready for you to use.

Content Negotiation

Done automatically or controll it yourself

Get["/"] = p => return "Hello Jupiter!";
Get["/"] = parameters => {
  return Negotiate
    .WithModel(new RatPack {FirstName = "Nancy "})
    .WithMediaRangeModel("text/html", 
        new RatPack {FirstName = "Nancy for html"})
    .WithView("negotiatedview")
    .WithHeader("X-Custom", "SomeValue");
};
Like in Sinatra, Nancy does content negotiation out of the box. Just pass back the data and Nancy will process it into an html page, json, or xml response depending on the requests Accept header. You can also add custom ones easily. Control the negotiation yourself if you want with the Negotiate property.

Easily Customizable

Custom Model Binding

public class CustomModelBinder 
  : Nancy.ModelBinding.IModelBinder 
{ . . . }
					
Because of the SDHP customizing is very easy. Just define the class and Nancy will pick up most customizations automatically. Custom model binding for instance.

Easily Customizable

Custom View Engine

public class CustomViewEngine 
  : Nancy.ViewEngines.IViewEngine
{ . . . }
  • Super Simple
  • Razor
  • NDjango
  • Nustache
  • Spark
  • DotLiquid
  • Veil
  • Parrot
  • NHaml
  • HandlebarsJs
  • Markdown
Or custom view engines. Nancy comes with the Super Simple view engine by default. There are several NuGet packages with other view engines available for use.

Testability

Test the whole Nancy pipeline quickly with the Browser object.

public void Should_return_status_ok_when_route_exists()
{
 var bootstrapper = new DefaultNancyBootstrapper();
 var browser = new Browser(bootstrapper, 
       defaults: to => to.Accept(″application/json″));

 var result = browser.Get(″/″, 
       with => { with.HttpRequest(); } );

 Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}
Its testability is easily one of the best features of Nancy. Because it's not dependent on ASP.NET, exercising the entire nancy pipeline including any custom model binders, request/response pipeline customizations etc. is a breeze. The Browser object allows you to easily test your modules individually or verify the Nancy setup entirely. The Browser object also allows you to verify your response output be it JSON, XML, or HTML without having to go through an actual browser.

Diagnostics

  • Information (== PHPINFO)
  • Static Configuration
  • Request Tracing
  • Interactive Diagnostics (customizable)
Nancy comes with a diagnostics module built in. I'll just touh on one interesting part of it the Interactive Diagnostics. You can create custom diagnostic objects with functionality to operate upon your app. You could have the ability to view a list of files in a data directory or remove some of the files for example. Nancy handles the input form generation for you to make it very easy to add runtime diagnostic operations to your site.

!the_beginning

teapotcoder.com

@davidroberts63

twitter, github, bitbucket, jabbr, IRC

NancyFx.org That's all I have to talk about Nancy right now. There is a lot more too it and plenty of documentation to go with it. You can see that Ruby is having a very good influence on .NET with Nancy, OWIN (the Rack for .NET) and C# scripting with ScriptCS. So take a look to see that .NET is headed in a great direction.