3 notes &
Updates to the routing system in Manos
I spent my memorial day updating the routing system in Manos to allow for parameterized actions. Originally actions in Manos were simply a delegate that returned void and took an IManosContext param. This made some things simple, but also made the API a little clunky to work with. Here’s an example of what things sometimes looked like:
[Get ("/Article/(?<slug>.*?)/(?<index>\\d+?)")]
public void Foo (IManosContext ctx)
{
MyManosApp app = (MyManosApp) ctx.App;
if (app.SomePropOnMyApp) {
// ....
}
string slug = ctx.Request.Data ["slug"];
int index = Int32.Parse (ctx.Request.Data ["index"];
}
Having to cast the IManosContext::ManosApp to the applications App type is rather awkward and is something that has bothered me for awhile. Unfortunately because Actions are a static delegate and the context is a static interface, there wasn’t much I could do about it.
On top of that, the Int32.Parse is rather scary looking.
Today I came up with a system that allows me to pass an application specific ManosApp type to the actions and as a side benefit I can also pass in request data as parameters. So the updated example looks like this:
[Get ("/Article/(?<slug>.*?)/(?<index>\\d+?)")]
public void Foo (MyManosApp app, IManosContext ctx, string slug, int index)
{
if (app.SomePropOnMyApp) {
// ....
}
}
Implicit Routes
Implicit routes are still available, anything that has the correct signature will have an implicit route based on method name added for it:
public void Bar (IManosContext ctx)
{
}
public void Baz (MyManosApp app, IManosContext ctx, string foo)
{
}
I also added explicit routing for ManosModule properties:
[Route ("/Articles")]
public ArticlesModule Articles {
get {
// ...
}
}
Routing Methods
Finally, all of the routing methods are still available, you can still do stuff like this if you want:
public MyApp ()
{
Get ("/Foobar", ctx => ctx.Response.Write ("Hello, Foobar"));
Route ("/Admin", new AdminModule ());
}
OH GOD MY EYES!
Writing this blog entry has made me realize the need for something simpler than regex for pattern matching. Luckily Manos was designed to have a swappable matching system, so users can easily add their own matching system and I can take advantage of that system to make my new system. Expect to see something like this in Manos really soon:
[Get ("/Article/{slug}/{index}")]
public static void Foo (IManosContext ctx)
{
}
More info
Its rough, but here is my first draft on the routing docs:
http://github.com/jacksonh/manos/blob/master/docs/routing.md