views:

1833

answers:

1

The example I see everywhere for MVC-style routing is something like this:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new Route
    (
         "Category/{action}/{categoryName}"
         , new CategoryRouteHandler()
    ));
}

What's the reason to pass the RouteTable.Routes collection to RegisterRoutes()? Why not just:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes();
}

public static void RegisterRoutes()
{
    RouteTable.Routes.Add(new Route
    (
         "Category/{action}/{categoryName}"
         , new CategoryRouteHandler()
    ));
}

What RouteCollection besides RouteTable.Routes would a route be added to? Isn't RouteTable.Routes the RouteCollection for the web application?

I have a particular IRouteHandler with has a Map() method:

public class ChatRouteHandler : IRouteHandler
{
    private static bool mapped;

    public void Map()
    {
     if (!ChatRouteHandler.mapped)
     {
      RouteTable.Routes.Add
      (
       new Route("chat/{room}/{date}", 
       new ChatRouteHandler())
      );
     }
    }

Is there a reason that Map() should accept a RouteCollection and not add to the RouteTable.Routes collection? Again, what other RouteCollection would this IRouteHandler be added to?

+4  A: 

Unit testing.

By decoupling (through argument passing) registration form RouteTable.Routes you can choose which RouteCollection is used for registration in unit testing.

IMHO, it's also nicer to write routes.Add() instead of RouteTable.Routes.Add() everywhere.

"Is there a reason that Map() should accept a RouteCollection and not add to the RouteTable.Routes collection?"

I'm interpreting this question as "Why doesn't RouteCollection have the MapRoute method, instead of an extension method?". Yeah, RouteCollection is in System.WEb.Routing, whilst MapRoute extension method is in System.Web.Mvc. MapRoute() depends on MvcHandler, which System.Web.Routing has no idea about.

CVertex
I can definitely see how this could affect class designs. Can you recommend an example or illustration of unit testing for a RouteCollection or IRouteHandler? After all, couldn't a unit test look in RouteTable.Routes?
Chris
Haacked has a good unit testing demo for routes. http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspxAt my work, a colleague derived RouteCollection to do something similar.
CVertex
Thanks for the link. Appreciate it. Regarding Map(), I'm a little confused. Is there a Map() extension method on MvcHandler or something? I'm completely new to MVC.
Chris
There is a MapRoute extension method for RouteCollection that resides within the System.Web.Mvc assembly. It's a nice shortcut method for adding routes. All the examples/tutorials use MapRoute.
CVertex
Also, if you're a non-n00b to C#, download the source from aspnet.codeplex.com and browse around. It's surprisingly easy to understand to entire framework by looking at the code
CVertex
Ah, very helpful suggestion. Learning by example is a great way to pick up a new tech.
Chris