InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Web Frameworks, MVC, and ASP.NET

Posted by Jonathan Allen on Oct 20, 2008

Sections
Development
Topics
.NET ,
Web Frameworks
Tags
AJAX ,
jQuery ,
ASP.NET MVC ,
MVC

After nearly a year as a community tech preview, Microsoft has released the first true beta of the ASP.NET MVC framework. ASP.NET MVC is a radical departure from the WebForms technology has promoted in the past, and in the opinion of many, a return to mainstream web programming. The MVC pattern provides the cornerstone for web frameworks such as Ruby on Rails and Java's Spring Framework.

MVC web frameworks should not be confused with their namesake, the Model-View-Controller pattern first proposed by Trygve Reenskaug. Under Reenskaug's pattern, the view and controller were tightly bound to one another with a one-to-one mapping between each view and controller. With a MVC web framework, the view and controller are loosely coupled and it is not unheard of for multiple views to be tied to a single controller.

Regardless of which definition of MVC you prefer, the model remains an independent representation of data that has no knowledge of what is using it. This is in contrast to WebForms, where it is common for data to be stored in the UI elements themselves in the form of viewstate.

Microsoft's MVC framework trades in the quick development time of forms and controls for the flexibility and precision you get from controlling all the HTML output directly. This change in philosophy will probably be a much easier transition for those familiar to classic ASP programmers or non-Microsoft languages than those already with a background in .NET programming.

Some of the new features in this, the first ASP.NET Beta release, seek to guide developers towards this new way of thinking. For example, developers can now create new views simply by right-clicking on the associated controller class or pressing Ctrl-M Ctrl-V, at which time they are prompted for the model the view will bind to.

Another departure from WebForms is the emphasis on JavaScript. While WebForms tried to hide JavaScript from the developer, either by wrapping it in controls or by processing data on the server, ASP.NET MVC embraces it. By default MVC web sites get a "Scripts" folder already pre-populated with ASP.NET AJAX and jQuery. Full intellisense is provided for the former, while jQuery only has partial support. This is temporary however, and the annotations needed for full jQuery support is expected in a few weeks.

Microsoft has always had a fascination with data binding, and ASP.NET MVC is no exception. Microsoft's "Model Binders" allow developers to quickly map HTTP POST data to object properties. These objects are then sent to the controller class's action method. With this release, default binders for common .NET classes were added. Keep in mind, however, that under most circumstances developers will be creating their own.

Automated testing of web sites is another mainstream concept Microsoft is only just now embracing. Unlike many frameworks where testing is only given lip service, Microsoft has planned for it from the beginning. Mocks are no longer needed for testing controllers and models, which is important as they tend to contain all of the testable logic. Testing of views will still have to be done externally as that involves checking the HTML against each supported browser separately.

Another aspect of ASP.NET is a return to a focus on HTTP verbs. While important in older technologies like classic ASP, a WebForms developer could almost forget they existed. Other than knowing that a post-back caused a POST and a Response.Redirect caused a GET, little could be done with them. In ASP.NET MVC, HTTP verbs are very important and the API reflects that. Common tasks like restricting some actions to a particular verb can be done simply by decorating the controller method with an AcceptVerbs attribute.

To facilitate replacing Microsoft's own methods for those written by the developer, any of the HTML helper methods have become extension methods. This allows them to be replaced partially or in whole simply by changing the using/imports statement.

On a concluding note for those die-hard WebForms fans, Microsoft isn't abandoning you. Scott Guthrie writes,

As I always like to make sure I point out: If you don't like the MVC model or don't find it natural to your style of development, you definitely don't have to use it. It is a totally optional offering - and does not replace the existing WebForms model. Both WebForms and MVC will be fully supported and enhanced going forward (ASP.NET WebForms in .NET 4.0 will add richer URL routing features, better HTML css markup support, complete control over the ClientId property, more AJAX features, and more that I'll be blogging about soon). So if you don't like the MVC option, don't worry, and don't feel like you should or need to use it (you don't).

What MVC wants to cover? by Willian Leite Posted
Re: What MVC wants to cover? by Francois Ward Posted
Views and Controllers by Chris Sutton Posted
Re: Views and Controllers by Jonathan Allen Posted
  1. Back to top

    What MVC wants to cover?

    by Willian Leite

    I really think about what is the strategy beyond this "new" web way from Microsoft to programming websites.

    Is Microsoft MVC the answer for UI tester? Is about more layers for scalability?

    I'm really worry about why Microsoft bring this now. In the beginning everybody tells that VB.net and C# are the same, but in a real world programming I saw a lot more space for professional programming for C#. I'm thinking the samething right now.

    Willlian Leite
    wleite.blogspot.com

  2. Back to top

    Re: What MVC wants to cover?

    by Francois Ward

    What they want to cover is making it easier to get devs in the bandwagon. WebForms may be advertised as the "RAD", easy framework, drag & drop click click, and it was that way in 1.1, but as of 2.0 and beyond, things changed.

    WebForms are very powerful and flexible, and allows you to make great composite apps (like Sharepoint), and have an horizontal architecture (as opposed to the more vertical MVC and other similar framework), and totally abstract the context from the components.

    That comes at a price though, where even the more senior asp.net devs are not knowledgeable of how it all works or how to get the most out of it. So comes MVC, where the barrier for entry to make a well architecure app is much lower. Sure, its longer to make a quick and dirty prototype, but making a full app will be much, much simpler. You'll lose out on abstraction and the component model, but thats better for many than making a mess with WebForms because the architect of the team doesn't understand it.

    I guess it IS a bit similar to VB.NET and C#. They are almost the same, but since VB.NET is advertised as being the "easy, newbie friendly" language, you see many more "disasters" in VB.NET than you do in C#.

  3. Back to top

    Views and Controllers

    by Chris Sutton

    I've been reading the original MVC documents from Trygve Reenskaug and they don't seem to say that there is a one to one mapping between the view and controller. There are several places where it uses the plural "views" relative to a controller.

    Chris Sutton

  4. Back to top

    Re: Views and Controllers

    by Jonathan Allen


    Unlike the model, which may be loosely connected to multiple MVC triads, Each view is associated with a unique controller and vice versa. Instance variables in each maintain this tight coupling. A view's instance variable controller points at its controller, and a controller's instance variable view points at its associated view.


    st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.