Observations on Microsoft MVC for ASP.NET
- 📅
- 📝 594 words
- 🕙 3 minutes
- 📦 .NET
- 🏷️ ASP.NET, ALT.NET
- 💬 9 responses
Anyone who’s tried to develop large complex web sites with ASP.NET has likely run into many problems covering the page and control life cycle, view state and post backs and subsequent maintainability including the difficulty in creating automated unit tests.
Microsoft, taking a cue from the popularity of Ruby on Rails and subsequent .NET related efforts such as MonoRail, are embracing the model-view-controller (MVC) pattern and developing something more suited to web development than WebForms which aimed to make web development as similar to Windows development as possible (despite the major underlying differences in architecture).
The prototype, currently named System.Web.Mvc or Microsoft.Web.Scalene depending on where you look, is headed by Scott Guthrie with both Phil Haack and Scott Hanselman involved (sounds like a dream project and team to be involved with) and a preview release (“drop”) is due within the coming weeks.
Guthrie and Hanselman presented Microsoft MVC at the ALT.NET conference which revealed some interesting details buried in the video, my rough observations and notes based on the prototype they showed follows:
Philosophy
- Don’t repeat yourself
- Highly extensible & pluggable
- Use generics to achieve strong-typing without code generation
- Good performance, fast enough for large-scale sites
- Separation of concern for maintainability
- Clean URLs in and out
- Clean HTML
Extensible
- Interfaces used extensively
- No sealed classes
- Plug-in points for view engines (e.g. MonoRail’s NVelocity, Brail)
- Support for Inversion of Control (IoC) engines (e.g. Windsor, StructureMap, Spring.NET)
Compatibility
- Runs on the .NET 2.0 CLR
- Some helper classes require .NET 3.5 (extension methods)
- Normal Request, Response objects (via interfaces for mocking)
- Does not support postback
form runat="server"
- Supports MasterPages, DataBinding, CodeBehind
- Existing .aspx’s are blocked using web.config
Visual Studio
- Solution templates for web project and unit testing
- Full designer & IntelliSense integration
Flow
- Route -> ControllerFactory -> Controller -> Action -> ViewEngine -> View
Routing
- Routes can be defined dynamically and support RegEx URL matching
IControllerFactory
pops out the requiredIController
- Routing is case insensitive by default
- Support REST, blog engine, Jango style mappings etc. default /controller/action/parameters
Controllers
- FrontController style
IController
exposes:Execute(IHttpContext context, RouteData routeData)
IViewEngine
ViewEngine property
- Some implementations available, all with virtual methods:
ControllerBase
(adds dispatching)Controller
(Populate parameters intoViewData["key"]
)Controller<T>
(Populates parameters by makingViewData
type T)
- Attributes
[ControllerAction]
attribute to expose methods as actions (secure default behavior by not exposing helper methods)Attribute
for output caching[ControllerAction(DefaultAction=true)]
to override default method of Index
Parameters (to the controller)
- Automatically parsed where a
TypeConverter
exists - Future versions will support more complex serialization of types
- Can be nullable — use null coalesce operator for defaults
View Engine
IViewEngine
IView LoadView(string viewName)
- Implementations include:
WebFormViewEngine
Views
IView
virtual void RenderView(object data)
- Implementations include:
ViewPage
— pick up parameters fromViewData[""]
in conjunction withController
ViewPage<T>
pick up parameters fromViewData
as type T in conjunction withController<T>
HTML
- Clean HTML generation (have they sorted out the id mangling by MasterPages/INamingContainer?)
- Static HTML class supports Link, PagingLinks and Url methods
- Map to action names using Lambda expressions to ensure follows refactoring, e.g.
string url – Html.Link<ProductController>(controller =>controller.Edit(4);
- Is there a way to follow the default action?
Model/data
- Pagination extension methods extend
IQueryable
for getting pages of data (skip, limit) - Pattern for view-update-view cycle
- Object property to form field id mapping available and pluggable, allows
product.UpdateFrom(Request.Form)
Testing
- Easy to write tests by using mock objects (request, response)
- Unit testing framework project (NUnit, MBUnit, xUnit.NET)
Finally
- What’s with ScottGu’s nametag, can the show’s organizers not afford anything more than a Post It note?
- What cool software is Scott Hanselman using to do the screen-cast video/zoom/overlay/highlighting?
[)amien
9 responses to Observations on Microsoft MVC for ASP.NET
This is very productive step to be taken. Specially the concept of the separation of concerns and eliminating the depedencies should be taken care of. After all the goal should be maximum reusable and extensiblity.
“This is a cool step in the right direction, especially for the likes of me who want to use .NET but also like the MVC methodology and do not wish to switch to PHP or other more Linuxy solutions.”
For what it’s worth, Struts (Java) implemented everything in this list in approximately 2000/2001 and real, production systems have been deployed on it, and other systems which have sprung up since as alternative takes on the model. You can basically get MVC frameworks most places except .Net, where once again they’re reinventing the wheel.
To be honest, the lack of any real MVC for .NET put me off moving solely to it, and I stuck with classic ASP. I built a few projects using Monorail from the Castle Project which brought MVC to .NET but compatibility issues (especially now, with trust problems in the apps i created on Win2k3 Server) made me step back as it was a non-standard solution.
This is a cool step in the right direction, especially for the likes of me who want to use .NET but also like the MVC methodology and do not wish to switch to PHP or other more Linuxy solutions.
Interesting stuff :D
About the clean HTML, ID generation: You get to handle it yourself. I, for one, welcome this change, as the naming containers tend to make th nastiest looking ID elements, just in case they might collide.
This will allow javascript integration with less… friction.
Screencast software I guess is something like Camtasia by TechSmith. I’m sure that records you and the screen at the same time.
As for the zooming, looks like ZoomIt!
Can’t remember seeing the highlighting.... His theme is available on his blog
great summarisation,
if i’m not mistaken ScottHa is using ZoomIt tool from sysinternals suite..
regards
Hi,
Great summary. Just a small clarification, Spring.NET’s web support is not MVC based. The approach is to still use WebForms, but remove as much of the pain as possible with that approach, i.e. dependency injection for aspx, custom http modules, as well as a better page lifecycle that allows for bi-directional databinding, validation etc. Of course, we will be working to bring added value to the new MVC stack.
Cheers, Mark
Better late than never… and they’re serious on soliciting feedback on this.
Talk about being late to the party; everyone else has been using MVC for web applications for about 8 years....