7

I have never come across this pattern of code right here. Would anyone care to explain it to me? (Or is there even a pattern here? Is there a reason why this was done like this? What benefits is this giving?) I'm new at general programming, and this is a very interesting one to me:

Global.asax.cs

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        //...
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        //...
    }

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
JCCV
  • 75
  • 1
  • 7

2 Answers2

5

This isn't a pattern as much an example of the Single Responsibility Principle (SRP). In Global.asax, we know of the high-level tasks that are required to set things up but we leave the implementation separated.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
  • That's not really a pattern, but a principle to adhere to when designing your classes. – millimoose Sep 14 '12 at 01:14
  • Thanks, but Single Responsibility is a *Principle* of Object-Oriented Programming. It's not a [design] pattern. http://en.wikipedia.org/wiki/Single_responsibility_principle – JCCV Sep 14 '12 at 01:16
  • 1
    @JCCV Nonetheless it applies. If you adhere to that principle, `Global.asax.cs` shouldn't have the three responsibilities of "register areas, set up Web API, and set up routing". Factoring the code in the way that's in your question means `Global.asax.cs` only has to know *what* needs to be configured, not *how* to configure it. – millimoose Sep 14 '12 at 01:20
5

You could easily write the code in your sample like this:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional });

    RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    RouteTable.Routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional });
}

However, as the amount of necessary configuration grows, it makes sense to split it up into several logically related chunks. ASP.NET MVC supports this fairly well, and the default project template is just trying to guide you towards doing so.

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • hmmm... I see why they did that. Great answer! Can you explain to me (if possible), how and why they did it more in-depth and in basic terms? Maybe provide a very simple parallel example? (I'm new at programming). Thank you. – JCCV Sep 14 '12 at 01:21
  • 2
    @JCCV I'm not sure I'm an authority on exactly why they did things this way. My best guess would be to avoid the problems with XML configuration which was common in, for instance, Javaland. For instance, if you want to allow to split XML configuration into multiple files, you need to support this explicitly in the configuration file format. This gets more complicated if you want to be able to include configuration files from a library. Even worse if you wanted to dynamically generate configuration at run-time. – millimoose Sep 14 '12 at 01:29
  • @millimoose That's an argument against XML-configuration in general, not an argument for/against one or multiple methods updating the configuration .. –  Sep 14 '12 at 01:33
  • @pst At the end of the day, you should use however many methods / classes you feel is necessary given how complex your application is. Splitting it up even if it's as simple as in the project template is arguably overkill, but I interpret it being the default as a friendly reminder from Microsoft that you have the option of doing so more than anything. The split being a default is also a weak argument in favour of keeping things that way (as opposed to inlining everything into `Global.asax.cs`) if only because it's what other developers will expect. – millimoose Sep 14 '12 at 01:37
  • @millimoose I do not disagree with that. However, it is orthogonal to XML or common issues with XML configurations :) –  Sep 14 '12 at 01:45
  • @pst To be honest I just wasn't sure what exactly the OP's "how and why" was referring to. "Why configure with code?" is just much easier to answer factually than "Why split up configuration?" so I covered that base first. – millimoose Sep 14 '12 at 01:54