Search
Categories
Navigation
Twitter Updates

Twitter Updates

    follow me on Twitter
    « Mocking HttpContext with Moq | Main | So, You Want to Learn ASP.NET MVC? »
    Friday
    11Sep2009

    Are Your Controllers Playing Music?

    Chicago Symphony Orchestra - Photo by: Jordan Fischer (Flickr) Controllers are where application logic lives within ASP.NET MVC (and other MVC-based frameworks). This typically means that the controller is in charge of accepting user input, be it form-data or URL parameters, using that data to direct the model objects to perform certain actions and then ushering the user to the appropriate view to see the results of all that effort.

    This separation can be a big change for developers new to MVC, however. The metaphor that I like to use when describing MVC to them is that of an orchestra. In it, the individual musicians are the model, the conductor is the controller and the audience is the view.

    What Conductors Do

    According to Wikipedia, conducting is “the act of directing a musical performance by way of visible gestures”.  A conductor is “the boss” of the orchestra, in that the musicians take queues and direction from the conductor so they play together and produce a cohesive performance. The conductor coordinates the performance and makes any necessary adjustments to the beat or tempo of specific sections of the orchestra. The conductor does not, however, play an instrument. Conductor leading an orchestra - Photo by: chrisbb@prodigy.net (Flickr)

    It’s critical to understand that most conductors know how to play several musical instruments. Conductors typically have a deep understanding of music theory, history, and often write their own pieces and arrangements. In short, they are experts in the field of music, yet they do not play during an orchestral performance, instead focusing on directing their efforts toward the performance as a whole.

    Conductors are also typically the only member of the orchestra who will talk directly to the audience, informing them about a piece of music, prompting them for applause and generally forming the relationship between the audience and the musicians. While the audience experiences output from the various musicians in the orchestra during a performance, the relationship is between it and the conductor.

    What Controllers Do

    Violin player - Photo by: Alden Chadwick (Flickr)Controllers within an MVC application are similar to the conductor in that they likely possess deep and expert knowledge in how things work within the system. Sure, a controller probably knows how to query a table in the database to determine what it should do, but it shouldn’t do such work because that’s the job of the model. Much like a conductor, a well-designed controller does not directly perform, instead choosing to direct model classes to perform actions on its behalf and focusing on the overall flow and direction of the performance at hand. By focusing on directing model classes to perform actions for it, the controller is made more flexible. To return to our analogy, this allows the controller to be able to conduct the same performance regardless of the composition of the orchestra.

    Additionally, controllers own the relationship between the model and the view, providing the right data to the right view as needed. While a view may interact directly with a model that was provided to it by the controller, it doesn’t know how to get the model on its own any more than the typical audience member at an orchestra knows how to play all the parts in a symphony. In fact, the view’s connection to the rest of the system should be through the controller, in much the same way that an audience experiences a symphony through the conductor’s baton.

    Fighting Controller Bloat

    While all analogies break down at some point, the controller as conductor metaphor is useful as it makes for a pretty clear set of design questions when looking at a given part of a system:

    • Is your conductor playing in the orchestra? When a controller starts to interact directly with persistence layers or takes on too much of the doing aspect of an action, rather than directing, it’s probably trying to play an instrument. You should re-design it to put down the instrument and pick up the baton.
    • Is your audience telling the musicians how to play? When a view is directly accessing model objects that the controller didn’t provide to it, it’s probably trying to form a direct relationship with specific members of the orchestra. You should re-design it to put down the baton and pick up a concert program.
    • Are your musicians playing to their own beat? When a model class starts interacting directly with HTTP request elements (HttpContextBase) or providing data directly to the view (ViewData) it’s probably trying to subvert the conductor’s authority. You should re-design it to put down the baton and pick up an instrument.

    Are your controllers playing music, sitting in the audience, or directing the performance?

    PrintView Printer Friendly Version

    EmailEmail Article to Friend

    Reader Comments

    There are no comments for this journal entry. To create a new comment, use the form below.

    PostPost a New Comment

    Enter your information below to add a new comment.

    My response is on my own website »
    Author Email (optional):
    Author URL (optional):
    Post:
     
    Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>