Original: WebForms VS. MVC (translator)
(This article translated from CodeProject wrote an article, the original address: Http://www.codeproject.com/Articles/528117/WebForms-vs-MVC, spoke about ASP, ASP. WebForms and ASP. Three technologies, this article helps ASP. NET beginners to understand some of the basic concepts. ）
I've been working on ASP for a long time, and I prefer to use WebForms to develop Web applications. In 2008, Microsoft introduced a thing called ASP. I was just as curious as a lot of people why I had to launch a new ASP. Many people say that ASP. NET MVC instead of ASP. WebForms, but that's not the case. They both have pros and cons, although no one has told us how to choose which technology to develop the Web program, but we can discuss some key facts to help us make a choice later. This article is an introduction to the two technologies.
We also find answers to the following questions,
- What is ASP.
- What is ASP. NET WebForms?
- What is MVC?
- What is ASP. NET MVC?
If you are a very experienced ASP. NET-MVC developer and already have a good understanding of MVC, this article will give you a refresher on the concepts associated with it.
- Talk about "visualization"
- Web Technology
- "Visualization" in web development
- What is ASP.
- What is an ASP. WebForms?
- ASP. 4.0
- What is MVC?
- What is ASP. NET MVC?
- How do I choose ASP. NET WebForms and ASP.
1. talk about "visualization"
Microsoft originally introduced a concept called "visualization", and with the support of some "visual technology" such as visual Basic, Visual C + +, Microsoft implemented the rapid Development (RAD) of the graphical user program (graphical user applications). Technologies such as drag, pull, and IntelliSense also enable developers to focus on the business logic of the system rather than on UI development. However, this "visualization" technology is limited to desktop application development (ie, Windows Desktop programs, translator note ), when it comes to Web program development, Microsoft can only choose ASP.
When we talk about Web technology, we are familiar with the classic asp,php,jsp,asp.net webforms,asp.net MVC and so on. The classic ASP is one of Microsoft's Web technology launches. ASP the biggest difficulty is its "Italian style" code style is not conducive to post-maintenance, we assume a scene: There are some textbox text input box and a button, when you click the button, the background will verify the text box input data, if the validation is successful, the input data into the database; A red error message appears on the page. Do you know the biggest problem in this scene? You have to do a lot of things by hand:
1. First create a "self-submitting" page, set the form form's Action property value to the page itself ( processing the data in the form, the translator note );
2. When you click the Submit button, the value in the TextBox control is emptied, so the only way to get the user input in the background is to find it from the form data set submitted by the page ( note The difference between Post and get method) , the Translator notes );
3. If the background verification data fails, you must show the ground:
1) Assign the submitted data one by one to each TextBox control (maintain the last state of the page, translator note );
2) Display error message.
(using Ajax is a way of validating data, and here I'm just trying to illustrate the work that we have to do manually in the classic ASP development)
3.Web in the "visualization"
As with the use of visual technology such as Visual Basic, Microsoft can only choose Classic ASP when developing Web programs. When we talk about Web and desktop ( this should be a Web program and a tabletop program, the Translator notes ), we should consider two things:
1. How is state management conducted?
2. How is the request/reply conducted?
The Web program is based on the "Stateless" HTTP protocol ( for a true definition of stateless, see the blogger in front of the article, the Translator note ), there is no correlation between the previous and the two requests, it does not have a temporary variable to store the state like a desktop program, but also fully adhere to the "event-driven model ”。 Like desktop programs, Web programs also need to wait for the user's input, but unlike web programs, the user's requests are independent of each other, that is, each request and response is completely new. Finally, Microsoft introduced a thing called ASP. WebForms, which is used as a new RAD tool and is easy to master.
4. What is asp.
ASP. NET is a CLR-based Web application development framework that can be used in multiple languages, such as C # and VB. NET, and so on. It supports two development modes: ASP. NET WebForms and ASP.
5. What is WebForms?
Microsoft's initial release of ASP-based ASP-WebForms technology was designed to help Web developers create a "stateless Web" layer of abstraction that simulates "stateful" features ( like desktop program development, translator notes ). Concepts such as self-commit (submitting form data to the page itself), ViewState (Saving control values on page commits) are introduced in WebForms. The most interesting thing is that we don't even have to write any line of code ( a website can run, the translator adds ), and in ASP. WebForms, Microsoft tries to visualize a visual like Visual Basic ( desktop, translator plus ) Development patterns are introduced into web development.
So now let's talk about the pros and cons of WebForms.
(All WebForms in this article refer to "ASP. WebForms", the translator notes )
- WebForms support for rich server controls
Many of the server controls like the GridView or ListView have the ability to "data binding," which greatly reduces our workload. ( Although the data binding sucks, hehe, the translator notes )
You may often hear that "HTTP is a stateless protocol" in this case, usually, between two requests, the page's control ( this refers to the HTML control, like input , etc., the translator notes is not able to save its own state, but in WebForms, there is a thing called ViewState can be in the form of hidden fields (hidden filed) in a static Web page, it can save the state of any server control.
With the help of the following technologies, Microsoft introduces event-driven programming in the Internet world.
2. Self-commit (submit the form data to yourself for processing)
Developers are no longer dependent on the post or get mode when they are processing user input data in the background. For example, TA only need to drag some controls (such as button) to the page, and then double-click Add Event handler, and then write the business logic in the auto-generated code, this is what the developer needs to do, TA does not have to think about what happened at the bottom ( such as how to get the user input from the data collection submitted, how to save the state of the page control, and so on, all of which are done by the system, developers just need to develop the Web program like a desktop program (like WInform) . Translator Note )
I don't think it's necessary to explain this in detail, the above three is enough to show that WebForms can speed up the development of web programs. It is equivalent to encapsulating an abstraction layer on the basis of traditional web development, and developers do not need to figure out the underlying complex process.
When developing Web applications using WebForms, there is no pre-defined unified project architecture, and developers are free to choose the architecture they like. Some people might choose a three-tier architecture (UI layer, business logic layer, data access layer), while others might prefer Model-view-presenter (MVP). Even if we can write all the code in the codes behind file (. cs file, translator note ), it is clear that this is not a good programming habit.
In WebForms, the Code behind file contains many event handlers, which makes automated unit testing very difficult.
Note: As far as I know, even with the help of some simulation tests (such as using MOQ or RHINOMOQ), we can hardly simulate two parameters in the event handler (sender and EventArgs) (sender generally represents the control that fires the event , the Translator notes ).
ViewState while solving some of the problems of classic ASP technology (such as the inability to automatically save page state, see previous, translator note ), but it also brings a lot of problems, viewstate in the form of hidden field exists in the Web page, Transfer back and forth along with the Web page, which undoubtedly increases the amount of page data to reduce performance.
Let's take a look at another example that needs to create two UI interfaces:
1) tax-paid employee interface
2) No tax-free employee interface
Many of the logic in the code behind codes for both pages is the same. One way to do this is to create only one UI page, and add some if judgment statements to the page's code behind. But doing so will:
1) This violates the single principle of responsibility (SRP)
2) Secondly it may possible that both UI ( not clear what the original meaning, the translator notes )
- Difficulty interacting with HTML controls
URLs are pointing to a fixed aspx page (with some query parameters), which reduces user affinity and also affects SEO.
- Parallel development Not supported
ASPX pages are closely linked to code behind, so it is difficult for many people to develop a page at the same time (such as one person developing an ASPX page and another person writing code behind).
ASP. NET 4.0 introduces a number of new features to address some of the issues mentioned above
Provides a way to control ViewState size or even disable viewstate (but there is no deterministic or mandatory rule that requires developers to do so).
Provides a way to replace the physical address of a page.
In ASP. NET 4.0, we were able to better control the ID of the page control, so that we can better use some similar Jqeury script libraries (but we still can't control the HTML generated automatically).
Even after the revolutionary update to ASP:
1) It still failed to solve the problem of unit test
2) I never saw someone go to disable ViewState (personal view, hehe)
7. what is MVC?
MVC is an architectural pattern that has been in the way for a long time. Many people use it in Java, it is not the concept of the heart that Microsoft put forward. Although ASP. NET MVC is the subject of our discussion, I think before that we should figure out some technical concepts first.
Simply put, the pattern is our solution to a problem.
The schema pattern refers to the way the problem is resolved at the subsystem level, which primarily addresses issues related to the project architecture. It tells us how to divide the system and why. We create class libraries, components, or Web services as required to solve the final problem.
When we discuss the system, it typically contains user input logic, business processing logic, and UI display logic, and MVC is an architectural pattern that allows us to develop loosely coupled applications between each module. The main purpose of MVC is "separation of concerns", which can be separated from the UI display, business logic, user input, and so on. According to the MVC rules, a system should be divided into three parts: model, view, and controller.
- Model: It is considered a smart body that can handle business rules, logic, and data, and it is independent of the other two parts of MVC (C and V) ( without access to the Controller and view, the translator notes )
- Controller: It is responsible for receiving user requests, assigning requests to handlers, which can access model and view.
- View: It is treated as a mute, specifically presenting the final data to the user. It can be an Excel form, or it can be a Web page that contains many records, or even a plain text. It can access the model.
8. What is ASP. NET MVC?
ASP. NET MVC is a new Web application development framework of Microsoft, which mainly emphasizes the separation and testability of functional modules. It runs in the CLR and is based on MVC, which no longer supports ViewState and server controls, so I feel like I'm back in traditional web development. Let's discuss the pros and cons of asp:
One of the advantages of ASP. NET MVC is that it enforces the separation of functional blocks, so it is very rare to make things complex and diverse.
- Test-driven development and reuse
1) in MVC, the controller is a separate class, so automated testing is called a possible
2) Controllers is not dependent on any view, so it can be reused by multiple view.
ASP. NET MVC does not support viewstate, so it does not increase page data size.
ASP. NET MVC does not support server controls and can only use HTML controls. The HTML that the user sees in the browser is almost identical to what we wrote in the development phase, and the HTML elements are well manipulated during development, so we can use the third-party scripting libraries such as jquery.
- Supports parallel development
In ASP., the parts are loosely coupled, one person can be responsible for the controller, the other one is responsible for the view, and the third person can be responsible for the model.
- SEO, URL Routing and REST
The rich URL routing feature allows us to treat each URL as a resource. At the same time, the readability of the URL also increases user affinity and facilitates seo.
The multi-view engine is supported in ASP.
Since ASP. NET MVC is based on a proven ASP. NET Framework, many of the original outstanding features can continue to be tried, such as Windows authentication, session, and so on.
The absence of event-driven and viewstate makes ASP. NET MVC no longer easy to learn, especially for those without web development experience.
9.asp.net MVC operating mechanism
1. The user uses the URL request server
2. Request to enter controller first (the routing engine determines which controller a request should be handled by, this is not covered in this article)
3. If necessary, the controller accesses the model to obtain the data
4.Model accesses the database, and the data is returned to the controller
5.Controller Select a suitable view (for example, include HTML table page)
6.Controller passes the data (4th step returned by model) to the view selected in step 5th
7.Controller sends the view to the user (browser).
The above is just a get-way request, the Post method is similar. The only difference is that the user does not enter a URL in the address bar, but instead clicks on the button on the returned page, and then the server-side process is similar.
How do I choose ASP. WebForms or asp?
For a particular system or a particular team member, both may be referred to as the "best choice." When faced with choosing one of these technologies as a development platform, it is important to understand the pros and cons of both.
When you have to make a choice, there are two very important factors:
1.RAD (Rapid application development)
If you want to quickly develop a system ( such as a demo demo, translator note ), the ASP. NET WebForms is your best choice, and you don't even have to consider using ASP. NET MVC for "rapid development."
2. Unit Testing
For your system, if automated unit testing is important, then ASP. NET MVC is your choice.
In addition to the above 2, you'd better write down your project's needs and then compare the pros and cons of WebForms and MVC, and if you can, try asking yourself the following questions:
1. Does your team have good webforms or Windows Forms development experience?
Well, if the answer is correct, then it would be difficult for your team to learn ASP. NET MVC would be a hard thing to do because they are already familiar with event-driven programming and accustomed to viewstate, "changing habits" is a tough job. At this time, cast a vote for ASP. WebForms.
2. Does your team have a good experience with ASP/NET MVC development?
Answer if yes, cast ASP. NET MVC one vote.
3. Does your team have ASP experience, or some non-Microsoft technology (Android, iOS, JSP, ROR, PHP) development experience?
If you used to be an ASP or JSP developer, you might be familiar with both the HTTP request (get and post), and you even know about MVC, because most of those technologies use MVC by default. This is a vote for ASP.
If so, cast ASP. NET MVC one vote, because it can manipulate HTML elements completely.
5. Pursuing high performance?
ASP. NET MVC does not support viewstate, so it can have better performance. This is a vote for ASP.
6. Do you plan to reuse similar input logic code?
If yes, cast ASP. NET MVC one vote.
I think that you should know enough information to decide which technology to use, and that depends heavily on your project and your development team. I hope you like this article, thank you for your patience to read this article.
The above is the translation of the full text, italic word for the translator to increase their own content. The full text is not word in accordance with the original translation, part of the translator's own understanding and written out.
WebForms VS. MVC (translator)