. NET Beginner Architecture Design Guide (iv) Model-view-controller

Source: Internet
Author: User
Tags response code

Model-view-controller is simply MVC, which is an architectural form of graphical interface (GUI) applications. Model is the business domain layer, such as the account, Entry, Bill, invoice and so on that we mentioned in the preceding two articles, these classes form the business Domain layer of a telecommunication accounting system; View is the user interface Controller refers to the controller between the user interface and the business object, the role of the controller is to obtain data from the business object to display to the user interface, and to collect user input and action from the interface, and then invoke the business object to complete the business function.

The work of most software systems can be summed up in the following processes: Obtaining data from the place where the data is stored, displaying them in the user interface, and then modifying the data in the interface, and then writing the data back to the store. Data flows back and forth between the storage and the interface. This seemingly simple method of analysis often makes developers have the impulse to write the interface and data together, so that a lot less level, less code, can reduce the process of operation, it seems to speed up the operation of the program efficiency. But in fact, this straightforward chimney-like system is not very good isolation interface and business code, in the development and maintenance of the process will bring a lot of trouble.

The separation of the interface and the business code of the program can bring a lot of benefits. Generally speaking, the interface changes more frequently than the business logic, when modifying the interface, it should not have an impact on the business code; The techniques used in developing these two sections vary greatly; In some systems, you need to develop a variety of interfaces for the same function, such as a Windows Forms community Face to the backend management staff to use, a Web page interface for the masses, but also need to do a PDA browser interface, if the interface and business code is mixed together, a variety of interface development needs to do a lot of repetitive work; interface and business code Split-leaving also makes it easy to automate unit tests, creating unit test code for the user interface is cumbersome, while unit testing of business code is simple and necessary.

As a result, experienced developers create a business domain layer in the design process, and there are many business objects at this level that directly reflect the core of the business requirements. The user interface layer does not implement its own business functions, but rather calls the business object in the background. The user interface displays the properties of the business object to the user, captures the user's input, and invokes the business object's methods to achieve various functional requirements. When both the business logic layer and the user interface layer are in place, the remaining question is how to glue the two layers together-that's what the controller needs to do.

The simplest way to control it is to invoke the business object directly in the interface, which is called the Model-view mode.

The Model-view model establishes a simplest dependency relationship between the interface and the business model, and the interface invokes the business model directly, and the model modifies the representation on the interface through a loose coupling of messages such as the one used in the C # language in the previous article to realize the display of alarms in the interface. This can achieve the level of separation, to improve the structure of the software system is a certain help.

Developers who have used various versions of Microsoft Visual Studio must be familiar with the Model-view control, not in VB, VC, or later in C #, ASP. NET, we drag and drop a button control onto a form, and then the mouse clicks on the control, which automatically generates an event response code. The following is a procedure written in this way, an interface of the "African Telecommunications company's accounting system", which is used by the salesperson to pay for the user:

The salesperson fills in the user's telephone number in the "Number" input box, fills in the amount which needs to pay in "The Amount" input box, then clicks "Submits" the button, can put the money into the account. If an exception occurs, the program appears prompted.

When the "Submit" button is pressed, the button issues the Click event, and the form captures the event and takes a response action. The controller is implemented with such a mechanism.

private void Button1_Click (object sender, System.EventArgs e)
{
Press the Payment button, call the business object, realize the payment function
Try
{
string phone_no = TextBox1.Text;
string money = TextBox2.Text;
Get user object based on phone number
User user = Getuserbyphonenumber (phone_no);
Get this user's account number
Account Account = user. account;
Call the account pay, the interface to pay the account
Pay interface after the cost of processing to determine the arrears,
If you no longer owe the fee to the switching network to send instructions to the user to boot
Account. Pay (float. Parse (money));
MessageBox.Show ("Payment successful");
}
catch (Exception e)
{
MessageBox.Show (E.message);
}
}

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.