Snowing Code

Personal notes on software development


FubuMVC Routing - No Strings Attached

(Publish date: 06/02/2010)

Recently I’ve stumbled upon FubuMVC, and thought I should have a go at it, see what can you do with it, and more importantly see how other implementations of the MVC pattern work. After following the HelloWorld in Fubu steps, I felt I really like what I see, and thought I should try and pick it up, blogging on it on my way… If everything would work out nicely, I’m hoping to write my own site with Fubu, and share with others my experience with it :)

So the first thing I wanted to have a look at was the way they map (or register) their routes in Fubu, the way we in MVC do

routes.MapRoute(
           "Default", // Route name
           "{controller}/{action}/{id}", // URL with parameters
           new {controller = "Home", action = "Index", id = ""} // Parameter defaults
          );

And lo and behold, Jeremy Miller has posted about the subject. Now, I really like the fact that you can simply have a [RouteInput] attribute on your InputModel property, and that’s all it takes to tell the URL to end with “/{Id}”; that way, if you have the IputModel and the controller action

public class ProductsInputModel
           {
           [RouteInput]
           public int Page { get; set; }
          
           public ProductsInputModel(int page)
           {
           Page = page;
          }
          }
          
          public class ProductsController
          {
          
          public ProductsViewModel List(ProductsInputModel model)
           {
          //use model.Page to get your products list of page number X
          }
          
          }

then the url /products/List/4 will get you to the 4th page of your products list.

This, by the way, could be also achieved with the QueryString attribute, which as its name suggests, would put this data in your url as a query string like so: /products/List?Page=4

However, using attributes should come only after the convention (that is for exceptional occasions), and that’s something I still need to figure out. Doh’!

Now, what Jeremy was saying in his post (in a nutshell), is that the Fubu equivalent to ASP.NET MVC

Html.ActionLink("ActionName", "ControllerName", routeObjects)

uses no strings whatsoever, but simply uses the conventions. If we use an input model per action, we simply call

UrlRegistry.UrlFor(new ProductsInputModel(4)); 

How cool is that?? Working with ASP.NET MVC I can definitely say that this approach could have made a HUGE difference. One of the things I dread the most is renaming an action name or a controller.

Tags: fubu fubumvc mvc
blog comments powered by Disqus