Why MVC for ASP. NET?

Source: Internet
Author: User
Tags classic asp
Why MVC for ASP. NET?

PostedJonathan AllenOn Dec 06,200 7 AM

Community
. Net
Topics
Web frameworks

Rick Strahl starts his look at web forms and MVC for ASP. net by talking about its strengths. he claims that web forms have been proven to be a very stable and mature platform. even in high performance scenarios, it is rare to have major scalability issues. at the same time, it is really easy to get started. rick continues,

Microsoft designed the entire Visual Studio web eco-system to provide an easy development experience with drag and drop functionality on a visual designer with property sheet support. developers write code to respond to events, which provides a fairly straightforward model for hooking up application logic that feels natural if somewhat un-web-like. this model provides a high level of parallelism action that is great for getting started, but it's also problematic because it separates the developer from the low level Web mechanics in each ways. i'll come back to this flip side a little later.

He concludes by mentioning how extensible the framework is and offers a nod to the large custom control industry. (There are probably more custom control vendors for ASP. NET than all other Web platforms combined .)

Rick then turns to the downside of the very operation action he praised earlier.

This is both a blessing and a curse. web forms allow developers to rapidly create applications simply by dragging and dropping controls and handling page-level events for both the page and the controls on the page. this works well, but it's a high-level of dynamic action and complete developers completely forget-or never learned-how the HTML layout actually works behind the scenes. as a result, it's common to end up with non-Validating HTML, or bloated and hard-to-manage HTML layout that is very designer unfriendly. add to that a huge amount of viewstate if you don't have tively manage viewstate properly and you can easily end up with pages that are much bigger than they need to be and slow as molasses.

One downside of the web forms framework is that behind this has action layer, Microsoft built a very complex engine that has side effects in the page pipeline. if you 've ever built complex pages that contain your components on the page it can sometimes get very difficult to coordinate the Event Sequence for data binding, rendering, and setup of the various controls at the correct time in Page cycle. Do you load data in the init, load or prerender events or do you assign values during PostBack events? Web forms need to run through a single server-side form so they can't easily be broken up into smaller logical units. in complex forms, event Handlers can get very bulky with the tasks they need to handle and often in ways that can't be easily refactored so you end up with code that is difficult to maintain and impossible to test.

He goes on to discuss the problems with viewstate, event management, and post-backs in detail. ASP. NET developers who are not already intimately familiar with these topics owe it to themselves to study his words carefully.

One issue that will probably surprise our non -. NET developers is that it is very hard to determine a controls ID at design time. short of relying on some fragile and unmarshented naming container chain, there is no static way to know that a control called "txtpassword" actually has an ID of something like "publicsitetemplate1_ctl00_txtpassword ". and this is a relatively short ID on a simple page.

The cryptic control IDs and sensitivities of viewstate become a serous problem for so-called Web 2.0 sites. the only framework that does not have serous viewstate initialize uption issues is ASP. net Ajax. but it is an immature library.

To me, Microsoft Ajax in general is not a great client-side library solution in the first place. it's big, not very modular and provides only limited functionality for the client-side developer with the client framework being more of a core framework/reference implementation than a useful api library that provides usable functionality for client-side scripting. if you compare it to the lean and mean and highly functional approach of libraries like jquery, prototype, mootools, and so on, Microsoft Ajax feels like it's seriously lacking practical functionality. add to that the rigidity and complexity of the control creation model and it's hard to justify using Microsoft Ajax for anything but the server-side controls provided in updatepanel and the Ajax toolkit controls.

The next topic Rick mentions is the tendency for web forms developers to overly mix business and presentation logic. While good separation of concerns can be done using web forms, it takes a lot of discipline.

A bigger concern for professional shops is the difficulty in testing. web forms simply don't lend themselves well to automated testing. this difficulty partially arises from simulating context, request, response, and session objects. the rest of the problems come from trying to work around its event-driven model.

MVC completely side steps the issues by throwing away the page class and its associated baggage. Rick describes it,

Microsoft implemented MVC as an alternate HTTP handler framework so it's not based on the page class and bypasses a lot of the complexities of the web forms framework. one of the key features is the ability to create custom views that are responsible for rendering output for specific tasks. the Controller is responsible for using the model to perform core application processing and then feeding result data to the view to render. it's quite conceivable to build re-usable views for rendering RSS feeds or other XML output or a specific page display view like a login form or error page for example. by default, however, aspx pages cane still be used to render a view's output using a viewpage class. this class is based on the page class, but it's not called directly as an HTTP handler but rather just as a Renderer from within the MVC engine which is still much more lightweight than running through the full page framework that a typical ASPX page runs through at runtime.

Some of the controls and techniques from web forms can be still be used, but only if they don't rely on post backs. instead of controls, developers will find themselves using a lot of inline scripting that looks like classic ASP.

MVC's model works through URL routing to specific controller methods and any operation routes back to a specific method in the controller. microsoft based the routing engine on a ruby-like mechanic that parses clean URLs (like http: // localhost/Mers MERs/show/1) and routes these URLs to specific controllers based on the URL scheme. the routing mechanic-like most things in the MVC Framework-can be overridden easily so you can use different URL semantics to route a URL to a controller.

Rick finds some amusement in how things have gone full circle in the last ten years,

It's interesting to hear this discussion given that web forms has been the darling of web development on. NET platform. in fact, I find it somewhat ironic to see that MVC is in some ways reverting back to more ASP classic-style ways of working with script-like HTML Rendering and raw manual output, but with a much more strictly regimented approach to enforce methodology and code logic. I also re Member the early days of ASP. net when the ASP. net Nazis were going around und disdainfully condemning any use of SCRIPT tags and inline script or assigning explicit URLs instead of using postbacks, yet some of those same folks undoubtedly are the ones welcoming MVC with open arms now. ironic, ain't it?

It's too early to tell whether MVC will actually address the needs of most web developers. certainly from what's been shown so far it looks like MVC will require a lot more code to build even simple pages. I 've seen an early build of the framework and it's not really clear to me how to handle tively handle more complex pages without at some point getting mired into huge case statements to route code decide which portion of the page needs updating. I have built several web frameworks in the past that use a similar approach to MVC and while you can easily do pretty simple things, in my experience this approach really starts falling apart when you have pages that contain between different and somewhat independent components. managing this complexity through a controller-based approach that manages no state for you is much more difficult than what web forms provides today. we'll have to wait and see how the framework shapes up and what patterns emerge to handle more complicated tasks. it probably helps to look at some of the Java or even some of the existing. net frameworks that have been around for a while for doing MVC Based Web development and compare notes.

Rick concludes,

For the moment I'm not quite ready to throw out the baby with the bathwater and put all of my hopes on a new unproven framework. there are a lot of good things going for web forms and just because Microsoft has decided to try something new doesn't mean we all have to jump to it. after all, MVC frameworks have been around und for some time and they haven' t made any significant inroads today. MVC will ap Peal to a specific set of developers immediately-for the rest of us, Microsoft will have to build a kick-ass framework to make it at least comparable in flexibility, extensibility, and usability as web forms. there are some interesting times ahead. i'll be watching with interest...

Bookmark
Digg +,
Reddit +,
Del. icio. us +,
Dzone +
Tags
MVC,
ASP. NET

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.