Magellan Route Registration

This week I have been working on a routing system for Magellan. The goal was to make it very similar to ASP.NET's style of routing:

Routes.Register("Default", 
    "{controller}/{action}/{id}",                            // Path specification
    new { controller = "Home", action = "Index", id = "" },  // Defaults
    new { id = "^[0-9]+$" }                                  // Constraints
);

Unfortunately, I hadn't counted on Silverlight's security implementation. Since anonymous types are internal, I can't use reflection to pull apart the defaults and constraints the way ASP.NET MVC does.

Which of these syntaxes do you prefer?

Routes.Register("Default", "{controller}/{action}/{id}")
    .Defaults(controller => "Home", action => "Index", id => "")
    .Constraints(id => "^[0-9]+$");

Routes.Register("Default", 
    "{controller}/{action}/{id}",
    new Defaults(controller => "Home", action => "Index", id => ""),
    new Constraints(id => "^[0-9]+$")
);

Alternatively, please suggest a better alternative :)

A picture of me

Welcome, my name is Paul Stovell. I live in Brisbane and work on Octopus Deploy, an automated deployment tool for .NET applications.

Prior to founding Octopus Deploy, I worked for an investment bank in London building WPF applications, and before that I worked for Readify, an Australian .NET consulting firm. I also worked on a number of open source projects and was an active user group presenter. I was a Microsoft MVP for WPF from 2006 to 2013.

02 May 2010

I prefer the second, it is semantically clearer.

Rob
Rob
02 May 2010

I tend to favor fluent interfaces, so my preference would be for the first option. Though, it seams that you could build the fluent interface on top of the the second option using some extension methods. Admittedly, it's actually hard to answer this question without having built a real application with the route system, and having some experience registering lots of routes and maintaining them....but my intuition says the first options would be better in that sense.

As a side note...I'm really impressed with the way you have mapped ASP.NET MVC on top of WPF. In the past I've been inclined to say that the MVC pattern makes little sense in a modern stateful client. But, your implementation is so well done, I'm changing my thinking. Thanks for your contributions!

Carl Scarlett
Carl Scarlett
03 May 2010

I prefer the first one as it's less (keyboard) typing and I love composition.

03 May 2010

I can think of two ways to stick with the ASP.NET MVC style:

  • Use the ToString() method of the anonymous object and parse the values on your own.
  • Slightly change the syntax like this:

    Routes.Register("Default", 
    "{controller}/{action}/{id}",                                  // Path specification
    () => new { controller = "Home", action = "Index", id = "" },  // Defaults (this should be LINQ expression)
    () => new { id = "^[0-9]+$" }                                  // Constraints (LINQ expression again)
    

    );

This way you will get an Expression Tree for the anonymous objects and extract the values.

Regarding your suggestions I prefer the fluency of the first approach.

08 May 2010

I prefer the fluent interface, but I think Stefan's solution is the best alternative. I didn't think about the ToString().