<< Foundation Series Is a Hit! | Home | Tech Dinner Meetup with Jeffrey Palermo tonight! >>

MVC - The Anti-Controller Revolution Heats Up!

posted @ Friday, June 19, 2009 12:51 PM

In February of last year, I posted my thoughts on current MVC abstractions in Rails, and .NET, and how I felt that Controllers were really just glorified namespaces, where related action methods sit.  This leads to a lot of pain in separating concerns, routing flexibility, action reuse, etc.  In JP Boodhoo's course (the first time I got to attend, in October 2007) he showed a simple example of a "FrontController" model where a single application controller handled all requests and routed them to an appropriate command object.  In that scenario, "Actions" are separate classes, and therefore become first-class citizens.  This really appealed to my Object-Oriented aesthetic, because if a Class is supposed to be a cohesive unit, even the most cohesive Controllers are only bound by the "resource" they represent, such as a ProductController and its associated CRUD actions.  Each action on a controller can (and often does) have its own set of dependencies and concerns.  Why not just group by folder or namespace or some other way of grouping related Action classes, rather than as methods on a controller?

Recently, several folks that are smarter than myself have started thinking about this.  I first noticed Jimmy Bogard making a few comments on Twitter about it that echoed my thoughts very closely.  Then, this ringing endorsement from Jimmy must have prompted Chad Myers to write a nice summary of the idea on his blog.

I’m really looking forward to what the community comes up with here, and hopefully I may even be able to contribute something.  FubuMVC, Chad and Jeremy Miller's (and other contributors now) open source MVC framework has a lot of incredible ideas in it, though it’s still in progress, and perhaps too architecturally advanced for even the mainstream ALT.NET community (there are a lot of advanced C# techniques and some very academic-sounding naming strategies that I could see frightening away all but the most ambitious of programmers/teams).  I’ve been toying with the idea of building my own controller-less framework, and have taken a couple stabs, and often used FubuMVC as a reference for some pieces.  So far, I haven’t really fleshed out my ideas to be useful enough for anyone else though.

What I had come to believe is that unlike JP’s model (at least as I last saw it in November 2008), it seemed that mapping a request to one action was still not an optimal abstraction.  I based this on two basic scenarios, though I’m sure there are more.  First, requests with the same url and/or post data may require different responses dependent on whether the client was a browser, a mobile browser, an API call (expecting JSON/XML), etc.  In this scenario, you could use the same command to return the ViewModel data, but any number of "Response Commands" could be used to render the data in the appropriate way for the client.  Secondly, there may be distinct view logic that needs to occur, for example, when rendering a view for a browser, permissions, localization, and other user-specific customization may need to occur, where as returning pure data to an API call may not require any of that logic.  It seems to me if you include all this rendering knowledge along with the actual "business" of the request into a single command, you’ve only taken a baby step beyond the Controller/Action paradigm.

In Chad’s post (read it now, if you haven’t yet!) he talks about mapping requests to a group of commands, or command chain.  This takes my idea of separate Request and Response commands a step further, and I think it is probably a better idea, and could open up a huge opportunity for very succinct commands that are triggered by a request, of which some could be asynchronous, synchronous, etc.

I’ll be watching this closely as it progresses, and hope that we can make writing web apps easier, cleaner, and more testable for all of us soon!

Comments

  1. Jeffrey Palermo

    Posted on: 6/20/2009 12:19 AM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    This is very interesting. Here is my spike of it:
    http://jeffreypalermo.com/blog/the-asp-net-mvc-actioncontroller-ndash-the-controllerless-action-or-actionless-controller/

  2. Posted on: 6/20/2009 11:53 AM

    # The ASP.NET MVC ActionController – The controllerless action, or actionless controller

    There has been quite a bit of discussion about how controllers are really namespaces trying to get out once you use the concept on a nontrivial application.

  3. Kyle Baley

    Posted on: 6/21/2009 4:17 AM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    Though I haven't given it as much thought as you have, the idea of the same request returning different responses is what led me to OpenRasta. It requires a much deeper understanding of how to make such requests (providing content headers and other bits of data that escape me now), but once you have that understanding, it feels much cleaner in its usage.

  4. Brian Donahue

    Posted on: 6/22/2009 1:29 PM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    Hi Jeffrey,

    Thanks for the follow-up post. One action per controller would help some of the issues we're discussing, but it seems like treating the symptoms and not the root issue. I know this was just a quick spike on the idea, and it is something that can be done without much effort using the existing MVC framework.

    The idea that appeals to me is to do away with the concept of multiple controllers, and have a single FrontController that can find the right command or commands to handle a request. Chad Myers does a great job describing it in his post: http://is.gd/19p1F

    I think that inheriting from Controller and keeping the Controller/Action MVC nomenclature could make things more confusing than necessary.

  5. Brian Donahue

    Posted on: 6/22/2009 1:34 PM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    @Kyle,

    I have looked at OpenRasta a bit, and I think it is a great step. I still don't really like the "class per resource" and method per request type" model. I really like the idea of a chain of commands and being able to pick the correct command to handle events at configuration, or even runtime.

    Admittedly, I don't have a clear idea about how I would group actions relating to a particular resource - whether they'd simply be related by URL, or by some convention in the codebase - namespace, folder, class name, or some such...

  6. Scott Bellware

    Posted on: 8/5/2009 7:21 PM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    Why is the command pattern more object oriented than any other pattern? Why not represent all methods as objects? Or for that matter, why not use a language that actually does represent methods as objects?

    Once you decompose everything down to objects, including methods, you're left with a need for an organizing concept. That's what a namespace is, and one of a class's roles is to be a namespace.

    Honestly, there's a lot of class-oriented programmer self-indulgence going on here trying to solve problems that can addressed in much less elaborate ways.

    Understanding the right level of abstraction sooner or later becomes much more important than pushing decomposition to its ultimate limit.

  7. Joe

    Posted on: 8/10/2009 1:42 PM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    I've been struggling with how easily controllers get bloated. I use the CodeIgniter framework, and I lately have been looking at the controller as more a page controller. The framework uses a Front controller, then delegates to page controllers. I think someone mentioned one controller per view. I like that concept because it keeps your controllers neat, small, and very specific to the tasks it needs to perform.

    I see a lot of websites that for example have a user controller. And that controller can do the following:

    user/view
    user/login
    user/logout
    user/list
    user/add
    user/edit
    user/delete
    etc...

    When does it stop? Why would I need the overhead of 10 other methods when I only need to run 1 method on the controller at a time?

    Does this make sense to everyone else?

  8. online craps

    Posted on: 8/15/2010 1:03 PM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    This is a great read, I really think you've really helped clarify how MVC - The Anti-Controller Revolution Heats Up and work. In fact, the explanation of context versus behavior is pretty clear in your example.It requires a much deeper understanding of how to make such requests.

    I think putting a presentation together on this topic is in your future!

  9. agile informatics

    Posted on: 8/18/2010 10:35 AM

    # re: MVC - The Anti-Controller Revolution Heats Up!

    the weeks since the Entrepreneurial Revolution article appeared in HBR, government leaders, business executives, entrepreneurs, NGO directors, heads of institutes, university professors

Your comment:



 (will not be displayed)


  Please add 1 and 1 and type the answer here: