Skip to content

Instantly share code, notes, and snippets.

@grumpydev
Created February 14, 2011 14:20
Show Gist options
  • Save grumpydev/825932 to your computer and use it in GitHub Desktop.
Save grumpydev/825932 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace IndexerTest
{
class Program
{
static void Main(string[] args)
{
var test = new MyModule();
foreach (var route in test.Routes)
{
route.Action("moo!");
}
}
}
public class MyModule : NancyModule
{
public MyModule()
{
Get["Test"] = s => Console.WriteLine("Get: " + s);
Put["Test"] = s => Console.WriteLine("Put: " + s);
}
}
public abstract class NancyModule
{
private List<Route> routes;
public IEnumerable<Route> Routes
{
get
{
return this.routes;
}
}
public NancyModule()
{
this.routes = new List<Route>();
}
public RouteIndexer Get
{
get
{
return new RouteIndexer("Get", this);
}
}
public RouteIndexer Put
{
get
{
return new RouteIndexer("Put", this);
}
}
public class RouteIndexer
{
private string method;
private NancyModule parentModule;
public RouteIndexer(string method, NancyModule parentModule)
{
this.method = method;
this.parentModule = parentModule;
}
public Action<string> this[string data]
{
set
{
this.parentModule.routes.Add(new Route() { Method = this.method, Data = data, Action = value });
}
}
}
}
public class Route
{
public string Method { get; set; }
public string Data { get; set; }
public Action<string> Action { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment