In this four-part series, of which this is the first article, I will make clear to you whether the newly-produced Microsoft ASP.NET MVC architecture will take the place of ASP.NET Web Forms solution, which you may have been exploring. In fact, by comparing the main pros and cons of both, you will see that neither solution will replace the other; they will keep on evolving and be leveraged to solve different kinds of tasks.
Contributed by Xianzhong Zhu Rating: / 10 November 04, 2008
Beyond a doubt, it was a great revolution that the ASP.NET technique substituted the traditional ASP approach in constructing web applications. Later, the full-fledged framework, Microsoft ASP.NET 2.0 came into being, which brought many wonderful and excellent features as well as efficient tools to developers so that they can succeed in building a basic ASP.NET web application in just a few minutes.
At the same time as it provides popular object-oriented programming features, ASP.NET delivers a wealth of goodies, tools, and powerful system features. For example, lots of programmer-friendly classes let you develop pages using typical desktop-like methods, and the Web Forms model supplies an overall event-driven approach which seems quite comfortable to desktop application developers.
However, in November 2007, a group of ASP.NET guys invented some new ASP.NET MVC stuff. And shortly after the announcement of the new framework, millions of ASP.NET programmers from all over the world published their blogs focusing upon this framework by presenting elementary sample projects, digging into the components of the MVC architecture, introducing other related third party helper or extension tools, and so forth. (Also,http://www.asp.net/mvc/ provided videos and tutorials as well as quick starts for ASP.NET MVC). On the other hand, it's worth noting that within no more than nine months, MVC evolved from Preview 1 to Preview 4 with many more features added at an astonishing rate of speed.
In this article, we will unveil the advantages and disadvantages of the two kinds of architecture to decide whether it is time to say "Good-by stored procedures, hello 'Linq to Sql.'"
Now, let's first recall and enumerate the main strong points that ASP.NET Web Forms gives us.
The Strong Points of ASP.NET Web Forms
In general, the Web Forms-based framework offers the following advantages:
(1) The Event Model
Web Forms supports an event model that preserves state over HTTP, which benefits line-of-business related web applications development. The Web Forms-based application provides dozens of events that are supported in hundreds of server controls. As you've seen, because the Windows Forms model stems from the typical event-driven desktop programming style, it facilitates millions of developers from the VB6 world.
(2) The Page Controller Pattern
Web Forms uses a Page Controller pattern that adds functionality to individual pages. As you've also seen, an ASP.NET page is divided into two parts: the .aspx itself, and the relevant CodeBehind .aspx.cs (or .aspx.vb) file, with the former acting as the front view and the latter as the controller (which resembles the View and the Controller in the ASP.NET MVC term). Similar to the controller in the MVC case, the CodeBehind .aspx.cs (or .aspx.vb) file plays the role of a broker (the controller) that schedules the .aspx view page and the back end business logic (which in turn attaches to the various database storage areas).
We can say that, although the new ASP.NET MVC provide a clearer division for the Model, View, and Controller, the Page Controller pattern of Web Forms is still good. Under the .NET platform, we don't need to implement the MVC pattern by ourselves. As for the View, ASP.NET already provides the common Web controls, and also, we can define the custom user controls by deriving them from System.Web.UI.UserControl, or create our own Web controls from System.Web.UI.WebControl. Then we can assemble the UserControl or Web controls with an .aspx Page.
On the other hand, ASP.NET defines the System.Web.UI.Page class which, to some degree, acts as the Controller of the MVC pattern, which can deal with user requests. In addition, the code-behind technique can completely separate the rendering of the user interface from the implementation of the UI logic, which greatly facilitates handling the code.
As for the Model component, it should include the field objects of the business logic tier, as well as the data objects of the data access tier. And moreover, the .NET platform provides the DataSet-like objects through ADO.NET, which facilitates the binding with the data source of the Web controls.
(3) The ViewState and Server-based Forms
Web Forms uses ViewState or server-based forms, which, to a great degree, make managing state information easier. As is well known, HTTP is a stateless protocol, which means two successive requests across the same session have no knowledge of each other. By introducing the re-entrant form mechanism, ASP becomes successful in working around such a system limitation. What was once an ASP best practice has been standardized and integrated into the ASP.NET runtime to become the key feature of ASP.NET applications with automatic state maintenance.
The ASP.NET runtime carries the page state back and forth across page requests. When generating HTML code for a given page, ASP.NET encodes and stuffs the state of server-side objects into a few hidden, and transparently created, fields. When the page is requested, the same ASP.NET runtime engine checks for embedded state information-the hidden fields-and uses any decoded information to set up newly created instances of server-side objects. However, it's just such kinds of ViewState-related items that brought ASP.NET Web Forms plenty of issues when it's compared with ASP.NET MVC. This will be discussed in just a few moments.
Web Forms works well for small teams of Web developers and designers, who want to take advantage of the large number of components available for rapid application development. As is the case with Visual Basic (under which a large number of third-party components came into existence), mature ASP.NET techniques have resulted in a large marketplace of third-party server controls and components.
Therefore, it's a very real advantage that, with the already-existing ASP.NET solutions and various third-party components, a medium-sized ASP.NET application may be built within just a few days.
(5) Less Complex
In contrast to the newly-introduced ASP.NET MVC, Web Forms is less complex for application development, because the components (the Page class, controls, and so on) are tightly integrated and usually require less code than the MVC model.
At the same time, however, ASP.NET Web Forms has also produced some real problems, mainly centering around low response speed and inefficiency in setting up large web applications.
In the next part of this four-part series, I will cover the weaknesses of the ASP.NET Web Forms approach. I will illustrate my points with copious code examples, and then start easing toward a discussion of the ASP.NET MVC approach. Check back next week; you won't want to miss it.