Sunday 26 April 2009

ASP.NET Model View Controller (MVC)

Another 'link collection' (along the lines of my MVVM list)... about ASP.NET MVC (Model View Controller)...
Chris Tavares' Building Web Apps without Web FormsChris explains the MVC concepts and in the process builds a simple wiki-like tool - great because it is a more 'real life application' type example than you'll see elsewhere.
Rob Conory on why You Should Learn MVCRob Conery's well-thought-out argument for giving ASP.NET MVC is a great read, with some classic quotes such as:
WebForms is a lie. It’s abstraction wrapped in deception covered in lie sauce presented on a plate full of diversion and sleight of hand. Nothing you do with Webforms has anything to do with the web – you let it do the work for you.
This, friends, is a big deal (at least to me): You’re working in a lie. The web is *not* stateful and works with this stuff called HTML sent across wires using another thing called HTTP – you need to know this, love this, and feel it at the bone level.
He provides seven excellent reasons to back up his assertion (I'm paraphrasing the headings here, you should read the original text):
  1. Testability - yes, automated testing can be conducted much closer to your 'ui surface' without needing actual ui automation (ala WatiN et al)
  2. Control over Html - no 'funky' id munging or VIEWSTATE!
  3. Extensibility (my favourite so far - the Spark View Engine)
  4. Makes you think - and that's a good thing :)
  5. Javascript has 'come of age'
  6. Learn new concepts - what are those ALT.NET guys on about, anyway?
  7. It's fun!
Jeremy D. Miller's Oversimplified ASP.Net MVC Pros and ConsTo sum up:
  • CON: ASP.NET MVC is a 'version 1' product - so be warned
  • PRO: It's easy to customize and extend, to get around point #1
Simple eh?
The Book
Professional ASP.NET MVC 1.0
Written by the "fantastic four" (Rob Conery, Scott Hanselman, Phil Haack, Scott Guthrie), I can only imagine it's a good read since I haven't bought it (yet)...
However, the first chapter by Scottgu(180+ pages) is a free download!, and you can view the sample app - and download the code.
K. Scott Allen's MSDN article Life And Times of an ASP.NET MVC ControllerIntroduces Routing, Controllers, Action/ActionResult and Helpers.

AND 6 Tips for MVC Model Binding (and validation) - a hidden gem in the MVC framework!
Models incSlightly OT for a pure MVC post - applies to MVVM or any of the related patterns really - but I agree with the premise: Keep your system/tier/layer/whatever boundaries simple (DTOs) and
if there is more application specific needs than a simple DTO can provide, then create an Application Model
I guess that kinda leads into this discussion on creating View Models in MVC - I wondered about the "IEnumerable as the Model Type" in NerdDinner - Stephen discusses a 'proper' ViewModel approach too (see Listing 5 and 6 - although I'm not sure I'd put it in *Controller.cs necessarily).
Los Techies' Jimmy Bogard's How we do MVCThe 'voice of experience' from 9 months of MVC, including advice like:
  • Thin (not FAT) Controllers
  • Strongly-typed views, and discouraging the dictionary part of ViewData
  • Distinct ViewModels (seperate from the domain)
  • No magic strings
  • receive the EditModel as an action parameter, not some collection object
  • and my favourite advice:
    • Use partials when you have common markup, and the data is in your top-level ViewModel object.
    • Use RenderAction when you have common markup, but the information is orthogonal to the main concern of your view. Think like the "login widget" at the top of every screen. A filter is too much indirection for that scenario, RenderAction is very explicit.

Also links to Jeremy D. Miller's "Opinions" on MVC which is full of more sage advice!
ASP.NET MVC - the websiteMicrosoft's 'official' MVC home page - download the bits, watch videos, download templates, download sample applications and link to many more useful blog posts than described above!
VideosFrom Scottgu:
MSDN documentationIf you must...
Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC’s AntiForgeryToken() helperThis is a 'specific feature' unlike many of the above links, but an important one IMO...
Your application can be vulnerable to cross-site request forgery (CSRF) attacks not because you the developer did something wrong (as in, failing to encode outputs leads to XSS), but simply because of how the whole Web is designed to work.
ALL developers of internet-accessible websites should be making themselves familiar with XSS/CSRF: how they work and how to protect against them. If this feature makes that easier/simpler using MVC... then that's probably another reason to use MVC!
Check out Stephen Walther's draft ASP.NET MVC Framework Unleashed bookRead the great draft chapters Stephen has generously posted on his blog, provide feedback and then buy the book!

UPDATED: Added How we MVC and MVC Thunderdome Principle. Both provide exactly the kind of real-world 'lessons learned' that I want to read before starting an MVC project - to know that others have experienced real-world problems, found real-world (and neat) solutions, and shared them. Now I'm free to go and make my own new mistakes :)

UPDATED: You should NOT use ASP.NET MVC if... lists some reasons why MVC may not be for you: "You rely on 3rd party vendor controls for lots of the UI" is a good one which may not be 'immediately obvious' to those looking at MVC for the first time.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.