|
希望对感兴趣了同志们有用
此文章由 澳贼 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 澳贼 所有!转贴必须注明作者、出处和本声明,并保持内容完整
ASP.NET MVC vs. ASP.NET Web Forms
As you have seen, in the previous section, and can probably imagine MVC is going to be an architectural pattern that is going to be around for the foreseeable future, especially on the web. So it is very important to internalize and understand the major differences between ASP.NET MVC and the older ASP.NET Web Forms.
ASP.NET Web Forms
Starting with the .NET Framework Version 1.0, in January 2002, Web Forms was Microsoft’s first real attempt to provide a first class web application layer that was both robust and flexible enough to meet the demands of the web at that time. ASP.NET Web Forms has proven to be a mature technology that runs small and large scale websites alike. Web Forms, was built around the Windows Form construction, where you had a declarative syntax with an event driven model. This allowed visual designers to take full advantage of the drag and drop, WYSIWYG, interface that they had become accustom to under Windows Forms development in Visual Studio 6.0. In that they only needed to drop controls onto the ASP.NET page and then wire up the events, as was common in Visual Basic 6.0 development at the time. This made Web Forms a natural choice for Windows Forms developers, because the learning curve was low and the need to understand HTML and many of the web centric technologies almost zero. Web Forms have many strengths and weaknesses:
Strengths
* Mature technology
* Provides very good RAD development capabilities
* Great WYSIWYG designer support in Visual Studio
* Easy state management
* Rich control libraries from Microsoft and third party vendors
* Abstracts the need to understand HTTP, HTML, CSS, and in some cases JavaScript
* ViewState and PostBack model
* A familiar feel to Windows Forms development
Web Forms has grown so much since 2002 because it has the ability to do great things that are often much harder to accomplish in other frameworks.
Weaknesses
* Display logic coupled with code, through code-behind files
* Harder to unit test application logic, because of the coupled code-behind files
* ViewState and PostBack model
* State management of controls leads to very large and often unnecessary page sizes
Web Forms is not all roses and buttercups, there are some serious setbacks that usually show up when you are trying to optimize your code for scalability, the biggest problems are the ViewState and PostBack model. ViewState is a way to store the state of the controls, such as data, selections, etc, which is needed to preserve the Windows Form like development habits of the developers. ViewState was necessary, because the web is a stateless environment meaning that when a request comes in to the server it has no recollection of the previous request. So in order to give state to a stateless environment you need to communicate the previous state back to the server, in Web Forms this was accomplished using hidden <input /> fields that can become ridiculously large. This increased size becomes apparent when server controls such as GridView are added to the page. PostBack was another creation to facilitate the Windows Form development feel, it renders JavaScript for every subscribed event, which leaves web developer less control over how the browser communicates with the server.
ASP.NET MVC
ASP.NET was often overlooked as a viable platform for modern highly interactive websites that required a very granular control over the output of the HTML, because of the lack of control over the rendered HTML. This granularity of control was sacrificed in Web Forms to make if more like Windows Forms development, in other words easier for the drag and drop developers. This lack of control over the HTML rendering forced developers to move the platforms such as PHP and Ruby on Rails, which offered the level of control they required and the MVC programming model that provided a necessary separation of concerns for their highly complex web applications.
This led Microsoft to announce in the Fall of 2007 that they were going to create a platform based off of the core of ASP.NET that would compete against these other popular MVC web centric platforms. Microsoft implemented ASP.NET MVC to be a modern web development platform that gives a ‘closer to the metal’ experience to the developers that program with it, by providing full control and testability over the output that is returned to the browser. This is the main and most important different between Web Forms and MVC, in my opinion. MVC has many strengths and weaknesses
Strengths
* Provides fine control over rendered HTML
* Cleaner generation of HTML (well as clean as you keep it)
* Clear separation of concerns
* Provides application layer unit testing
* Can support multiple view engines, such as Brail, NHaml, NVelocity, XSLT, etc.
* Easy integration with JavaScript frameworks like jQuery or Yahoo UI frameworks
* Ability to map URLs logically and dynamically, depending on your use
* RESTful interfaces are used by default (this helps out with SEO)
* No ViewState and PostBack model
* Supports all the core ASP.NET features, such as authentication, caching, membership, etc.
* Size of the pages generated typically much smaller because of the lack of the ViewState
Weaknesses
* Not event driven by the framework, so it maybe more difficult for ASP.NET Web Form developers to understand
* Requires the need to understand, at least at the basic level, HTTP, HTML, CSS, and JavaScript
* Third party library support is not as strong
* No direct upgrade path from Web Forms
* No ViewState and PostBack model (makes it more difficult to preserve state)
http://www.coderjournal.com/2008 ... et-mvc-vs-webforms/
As you can see the pros and cons of MVC have to be weighed just as much as Web Forms, and MVC is not always the logical choice.
How do I choose?
It’s up to you to decide and you choice needs to be weighted with a number of other factors, such as team and application requirements, when deciding which ASP.NET technology to implement. I have developed the following worksheet that will hopefully help you decide, when you need to make this decision. |
|