MVC series-entry, mvc entry

Source: Internet
Author: User

MVC series-entry, mvc entry

The full name of MVC is Model ViewController, short for model-view-controller. It is a Model of software design, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic. MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure.

MVC first exists in the desktop program, M refers to the business model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different forms of representation. For example, a batch of statistical data can be represented by a column chart and a pie chart respectively. The purpose of C is to ensure synchronization between M and V. Once M changes, V should be updated synchronously.

Let's see how mvc works with traditional web applications!

From the two figures, we can see that the traditional web program works in the following way: the client writes a url address (generally in the form of Http: // 192.168.xx.xxx: 8089/master/index. aspx), this address is directed to the IIS server, and the IIS server address will be.. net platform to create an index. aspx class object, and then return this object to the client.

In MVC mode, the client also writes a url (Http: // 192.168.xx.xxx: 8089/master/index), but this url is different from the traditional url, in this url, the master is the Controller name and the index is the method in this controller! As for why a page can be returned based on this, this is the routing mechanism in the MVC mode!

Let's take a look at the structure of the MVC program.

The same method exists in the Global File Globlal. asax In the MVC program.

Public static void RegisterRoutes (RouteCollection routes) {routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. mapRoute ("Default", // route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home ", action = "Index", id = UrlParameter. optional} // default value );}

This method will find the corresponding controller and controller methods based on the master and index in the url. This method returns the corresponding view!

Most Web applications are created using procedural languages such as ASP, PHP, and CFML. They mix data-Layer Code such as database query statements and presentation-Layer Code such as HTML. Experienced developers will separate the data from the presentation layer, but this is usually not easy to do. It requires careful planning and continuous attempts. MVC strictly separates them. Although constructing an MVC application requires some additional work, the benefits it brings to us are unquestionable.

 

First, the most important thing is that multiple views can share a model. As I mentioned, you need to access your application in more and more ways. In this regard, one solution is to use MVC, whether your users want the Flash interface or WAP interface, they can be processed using a model. Since you have separated Data from business rules from the presentation layer, You can reuse your code to the maximum extent.

Because the data returned by the model is not formatted, the same component can be used on different interfaces. For example, many data may be represented in HTML, but they may also be represented in Macromedia Flash and WAP. The model also provides State management and data persistence processing functions. For example, session-based shopping carts and e-commerce processes can also be reused by Flash websites or wireless networking applications.

Because the model is self-contained and separated from the Controller and view, it is easy to change the data layer and business rules of your application. If you want to port your database from SQL Server to Oracle, or change your RDBMS-based data source to LDAP, you only need to change your model. Once you implement the model correctly, the view will display the data correctly regardless of whether the data comes from the database or the LDAP server. Because the three components of an application using MVC are opposite to each other, changing one of them will not affect the other two, you can build a loose coupling component based on this design idea.

This article briefly introduces MVC. The next blog will introduce you to an example!

 


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.