I found some time yesterday to finally play around a bit with some tech that I have been tracking recently; AppHarbor,NancyFx, Highcharts & this spiffy CSV feed on Yahoo Finance.

It took a while to figure out how to set up Nancy. I think the documentation is a bit out of date and more internal developer focused due to the high rate of change for the prerelease framework, but I was able to look at NerdBeers for guide posts.

In the end, I was able to put together a functioning demo of all the features I was interested in in about two to two and a half hours. And that is from scratch, meaning never having seen any of the frameworks I was working with. That is pretty damn simple. The vast majority of time was spent figuring how how to get Highcharts to display the data the way I wanted.

You can see the functioning demo up on AppHarbor and the source can be found on my github account. Try entering MSFT or AAPL and hitting enter. If you are watching with FireBug you will see an Ajax call go out to the path /ticker/MSFT or /ticker/AAPL which will return a year or so worth of price data for that symbol as JSON. Which is then rendered by Highcharts.

The bit of code that handles the /ticker/MSFT response is called a Module in Nancy parlance and it looks like this.

using System.Collections.Generic;
using Nancy;
using TickerPerformanceViewer.Web.Models;
using TickerPerformanceViewer.Web.Services;

namespace TickerPerformanceViewer.Web.Modules
{
    public class TickerModule : AppModule
    {
        public TickerModule(ILookUpPrices lookUp) : base("/ticker")
        {
            Get["/{symbol}"] = parameters =>
                                   {
                                       var prices = lookUp.GetFor(parameters.symbol);
                                       return Response.AsJson(prices);
                                   };
        }
    }
}

Notice how both routing and handling concerns are handled by the module. In the constructor for the module I pass a the string “/ticker” to the base constructor. This tell Nancy that this modules handles all requests to /ticker. Next up I add a delegate to the Get dictionary with the key “/{symbol}”. This basically means that any requests to /ticker/anystringhere should be handled by this delegate. I then simply look up my prices with a service class using the parameters collection that is of type dynamic and has my symbol picked out of the URL as a property. Finally I return the collection as Json.

A quick “git push appharbor master” in Powershell and I get to watch as AppHarbor builds and deploys my solution. All .NET deployments should be this easy.

And finally a shot of the working application, man what’s going on with Apple, eh?

So this was pretty much a “LOOK WHAT I DID!” kind of post. I am still figuring out the usage of these tools. I intend to talk about each of them individually in the future. But it is kind of nice to see how all of them can be plugged in together to rapidly build a slick application with a full stack of tools from source control to deployment. And not a bit of it using the stock Microsoft solution. Open source is indeed alive in .NET and getting better every day.



blog comments powered by Disqus

Published

22 April 2011

Tags