Tuesday, October 09, 2007

ASP.NET MVC or Scott Guthrie is My Hero

The collective jaws were dropped at ALT.NET.  Just about everyone attended this talk by Scott Guthrie.  If you’d like to watch this, Scott Hanselman has recorded the video and posted on his blog.

ScottGu-MVC

Rather than regurgitate it all here, I’ll quickly summarize what it’s like.

Remember HTML?  Yeah, that.  We are going back to our roots and back to the model that the web really lives in, not a psuedo, event-driven state-based model that is ASP.NET Web Forms.

Why don’t we like ASP.NET Web Forms?  I do like it, but it does have it’s problems.  The benefits of ASP.NET often cause other concerns to rise up.  Tired up re-populating data on the form?  Here’s some Viewstate for you.  Of course Viewstate causes page sizes to explode because most people just leave it on, even for read-only pages with no post-backs.  Think of a grid that you place on a page.  If you don’t have to postback to the same page again (like with a command field or something) then you can turn viewstate off and have a savings of almost 2X!  I for one think that ViewState can be awesome, but it is more likely abused than leveraged.  This is a pain point for many people.

The entire postback model, combined with the event-driven model is something that I have had to learn in-depth the hard way.  I really don’t think any of that knowledge is really helpful at all outside of ASP.NET.  I spent a crapload of time trying to get server controls to work in the complicated ASP.NET lifecycle, and does that knowledge transfer?  Probably not.

Lastly I think that ASP.NET’s templated control syntax often requires an equal or even more lines of code than the actual HTML would take in the first place!  Take the GridView for example.  Sure it’s great and it lets you bind data and get columns autogenerated for you, but take a look of that list of properties.  There is so much there that you almost need a degree in GridViews to be effective with it.

Microsoft has been paying attention to the community.  We’ve been readily ditching ASP.NET Web Forms in favor or Ruby on Rails or MonoRail.

ScottGu presented the new MVC framework (codenamed Scalene) to a huge audience.  There was a lot of trash talking (in a good way) between ScottGu and Scott Bellware.  It’s not easy to take blundt criticism from people like ScottB, but The Gu took it incredibly well and even dished it out a bit.

What I really liked about ASP.NET MVC is that there isn’t that much to it.  Here’s what you’d need to do to start using it on your project (exisitng or new):

  • Add the System.Web.MVC.dll (or however it will be packaged — no installer exists yet)
  • Add 1 HttpHandler to handle a new extension
  • Add 1 HttpModule, which provides all of the routing and black magic of MVC

Create a controller class, like this:

//This example is for you, Jacob Lewallen :)

public class ClownsController : Controller

{

    [ControllerAction(DefaultAction = true)]

    public void List()

    {

        ViewData = Clown.FindAll();  //you write this method or use Castle ActiveRecord for example

        RenderView("List");

    }

 

    public void SomethingSecret()

    {

        //this cannot be accessed by a url 
        //because it doesn't have the attribute

    }

}

Next you’ll need a view.  You can use aspx, Brail, or NVelocity as your templating language.  That’s freaking great that you can choose.

Here’s a quick example of an aspx view:

<h1>Clowns</h1>

<table>

   <tr>

    <th>Name</th>

    <th>Number of Balloons</th>

    <th>Likes Kids?</th>

   </tr>

 

   <% for(var c in ViewData) { %>

 

   <tr>

    <td><%=c.Name%></td>

    <td><%=c.Balloons.Count.ToString() %></td>

    <td><%=c.LikesKids? "yes" : "no" %></td>

   </tr>

 

   <% } %>

</table>

Notice how I get to use standard HTML constructs, use code blocks to provide view functionality & data, and I don’t have to mess with postback or viewstate at all because I don’t need it. 

Wait, you mean we have to learn HTML again?  D.H.H. often reminds us that the HTML is provides beautiful constraints that he is happy to work inside of.  We don’t have any naming containers here.  In fact, we don’t have any unnecessary id elements at all!  If we did, they would be exactly what we typed here, making javascript integration a lot easier.

To wrap this all up, a customizable route in the Global.asax will map the following url to render this page:

http://your_url/clowns/list    <—  look how clean!

The framework knows how that the format is http://your_url/[controller]/[action] and so it instantiates your ClownsController and looks for an action called List().

So much more to talk about, but I’ll be posting more details once I get my hands on it.

Tuesday, October 09, 2007 3:20:13 PM (Central Standard Time, UTC-06:00)
No that's what I'm talkin about... nice synopsis/value discussion Ben.
Brad Mead
Tuesday, October 09, 2007 4:12:09 PM (Central Standard Time, UTC-06:00)
NOOOOOOOOOOOOOOOO! Not Tables again!!!!! ;-)
Tuesday, October 09, 2007 4:19:25 PM (Central Standard Time, UTC-06:00)
Where would one get a degree in GridView? :)
Just kidding...nice post and I agree that since we are communicating in HTML we should WRITE html.
Mike
Tuesday, October 09, 2007 6:29:51 PM (Central Standard Time, UTC-06:00)
Hey, maybe I can create a Certified GridView Surgeon program going! ;)

Adam: you wouldn't try to replicate a grid using CSS wouldja?
Tuesday, October 09, 2007 9:36:32 PM (Central Standard Time, UTC-06:00)
I could see a <div><span> implementation for the example above, but for a full blown editable grid...uh..no..css would just not do. I would need CSS/Javascript solution :P

I'll dust off my copy of HTMLKit and fire up Dreamweaver. Man! Just when I was really enjoying the idea of never having to type <table border="1"> ever again. If it means progress...I'll gladly regress. :)
Wednesday, October 10, 2007 4:26:59 AM (Central Standard Time, UTC-06:00)
Probably one should look at Castle MonoRail(http://www.castleproject.org/monorail/index.html) first which for the past 2 years does all that MS.MVC does and a lot more. Microsoft should be given credit only for adopting this design style. I watched Scott's presentation and I thought I am attending a MonoRail presentation with different namespaces. IMHO, An "official" MVC framework from Microsoft is kind of late for ASP.NET community. Too many developers are already "polluted" by webforms model.

I really like Castle Monorail and its community. People are very active with ideas floating around in Castle's Devel Google groups: http://groups.google.com/group/castle-project-devel.

The good part is that MS finally got it. They really looked at open source works in .NET space and it seems they were inspired and try to play nice with them. I am not 100% comfortable witht this statement because I think nobody has access to code base yet, but, taking ScotGu's word for granted, everything is built around interfaces and therefore can be tested or replaced with alternative implementations. Support for Dependency Injection is an incredible thing, also.

I am looking forward for their first beta.
Robert Mircea
Wednesday, October 10, 2007 11:45:27 AM (Central Standard Time, UTC-06:00)
Just watched the video that Scott H. shot of Scottgu presenting this.

I am left with the feeling: Microsoft is creating their own MonoRail.

That is it. If you have used MonoRail, then you know MS MVC.

+1 to waht Robert posted.
Wednesday, October 10, 2007 5:32:20 PM (Central Standard Time, UTC-06:00)
Robert: I guess I didn't mention where Monorail fits into all this. Here's my take on it.

I really like Monorail. I like the entire Castle project and the people behind it. I think it gives us a lot of good separation, and has a nice design. It is not easy however so it has SO LITTLE adoption in the industry. I have never been able to get a client to use Monorail. Why?

1) it's hard to grok (at first)
2) it's not coming from Microsoft
3) most people don't understand what's wrong with web forms

MonoRail does have some nice features, but overall there are a lot of working parts in Monorail. Having personally seen what System.Web.Scalene does, it's a lot cleaner solution. Plus, since they are exposing the interfaces to the core runtime objects, it gives us a testing story that we just didn't have with Monorail.

I will now be able to go into a client and tell them that System.Web.Mvc is the way to go for "real" applications. No need to get approval, no need to go through any open-source technology committees, no fuss about "what if they go away?" -- it's coming from MSFT and the masses will just accept it. This is what MonoRail doesn't have now.

And as for the stuff that MonoRail has ... give it time. The framework is completely extensible, meaning you can use static/dynamic languages, plug in your own controller base classes, plug in your own templating syntax, plug in your own IoC container, etc.

I think that MonoRail will still continue to innovate and build useful features. I also think that they can rip out a good chunk of their code base and use System.Web.Mvc as the base of it all.

Hammet, are you out there? Can you enlighten us?
Wednesday, October 10, 2007 8:33:05 PM (Central Standard Time, UTC-06:00)
Hi Ben,

I have a question about MVP framework. MVP framework dictates that you should have views with a single responsibility and each view is accompanied with a presenter which is responsible for well.. presenting the view to the client. User controls can act as a view. The question is that if I have a page that consists of 10 views then should I create 10 presenters for each single view. Also, should the page be then responsible for initializing all the views using presenters. That seems like a lot of work!
Thursday, October 11, 2007 10:59:10 AM (Central Standard Time, UTC-06:00)
Firstly, this is an MVC framework, and the differences are subtle, but relevant. I've used MVP with WebForms, and the presenter generally has lots of responsibilities and the view does not.

Mostly this is because we can't test our view (which is code-behind) in any easy fashion.

Second, you are right that user controls *can* act as a view. Or the whole page is one view. Depends on the complexity.

If you have a page with 10 user controls, then yes you could easily warrant the need for 10 controllers. Or just use 1 controller that has a bunch of actions on it and a view for each. It depends on the scenario. But regardless, if you have 10 distinct responsibilities on one page, you have yourself a complex page, and a new framework isn't going to make that any easier.

I'd say break up your page.
Comments are closed.
Homeowner Loan - Renegade motorhomes - Car Insurance - Debt Help