Accustomed to the process of development, has been longing for object-oriented. Helpless halfway decent people, without a little internal force basis, read a lot of object-oriented books are just see confused. The concept of MVC is also incidental to learning, and the sense of being able to integrate with Web development allows the project to be object-oriented. Here's a little bit of my experience.
Let's first introduce the concept of MVC:
MVC (Model-view-controller), that is, an application of input, output, processing flow in accordance with model, view, Controller way to separate, such an application is divided into three layers-model layer, view layer, control layer.
Model: The process of business process/status and the development of business rules. That is the main core of MVC.
View: Represents the user interaction interface, the HTML interface of the Web.
Control (Controller): Can be understood as a request from the user to match the model with the view together to complete the user's request.
The following is a simple message board as an example of analysis.
1 from the above concept, the message board can be cut into three parts.
Model (M): Contains database links, generate datasets, database update operations.
View (V): Contains a dataset that gets the model, and populates the dataset with a variety of display interfaces, such as (Message board list, admin control page, add modify delete interface and so on all users see the page. )
Control (C): Data validation that contains database updates, which are forwarded to the view for rendering after the updated results are completed.
2) Three parts have come out, the following is further refinement.
Model (M)
Data Link: Data link parameters, link failure handling.
Data manipulation: Generating a fixed-format dataset, running a single-line SQL, resolving a fixed-format DataSet update database (including: Message records, administrator accounts, system settings).
File actions: The file internal format is sealed into a dataset with read write file operations. (Content includes: Message board style settings, copyright settings, search engine keyword settings, message board title set)
View (V)
Data display: Authorizes the controller [C] to request a dataset to the model [M] as required, and then displays the interface based on the dataset.
The result of the operation shows: according to the data returned by the control to determine the presentation of the information
Specific information includes:
Message Add Page: Judge whether the message is the administrator, judge the message is a reply or a new message.
Message Management page: Bulk Delete messages, query messages, Administrator account management, system settings.
Message Display page: Judge whether the message is for the administrator-specific, judge whether this message is for the administrator to release, whether to allow replies, whether to show sensitive information, whether the message needs to be audited and then displayed.
Control (C)
Data/File Operations: Receive operational data, verify the data are eligible, reference the model of the generated dataset class to generate the specified dataset, and then to the corresponding model action method operations, and get the results of the operation in a predetermined format to be forwarded to the view processing
3 The system is basically refined complete, the next step is to the above analysis of things encapsulated into class
Model (M):
Database links and data operations can be marshaled into one class.
The structure of the database dataset, which can refer to the dictionary control and array completion. The operation can also be sealed into a class that can be used to mimic the DataSet object in ASP.net.
File operation part encapsulated into a class
The file structure definition can be sealed into one class to facilitate control of the direct reference and then forwarded to the file action class for appropriate action.
View (V):
The view department can either appear in file mode or encapsulate some of the code and refer to it. You can also reference existing template classes to implement code separation, and so on.
Control (C):
Data validation: SQL Injection Defense processing, Japanese character replacement processing, a specific common check regular, this is the controller commonly used functions, can be sealed into class can also be a function of the existence of independent files, when used directly reference.
Data/File Update operations: Because of direct manipulation, you can avoid using class, but rather in file mode, in each file processing model (M) of a class. and complete control (C) of the work.
View [V] model [C] linker: Receiving a request from the view [V] requires a dataset, converting the request to the appropriate SQL sentence of the model [M], and then handing it to the model [m] Data integration generator to generate the dataset and then back to the View [v], which is a bit cumbersome to handle. If it's not necessary to be too rigorous, you can use the field name directly, and in view [V], request data from the SQL standard conditional sentence into the model [M].
Suspected:
View [V] model [C] linker, I'm still a little hesitant to set this up. Because in MVC, V is not required for C in the request of M, but the operation of this linker does conform to the concept of C, so here I put it in the controller [C]. Maybe he can put it in the view [V]. After all, this is the view [V] thing. This depends on everyone's understanding of MVC.
The above basic completion of the message board structure analysis and Class abstraction, there are a lot of places more cumbersome, even long-winded, a message board is not necessary to do so, but, this example to make a lot of class can be used directly in other larger projects, such as Controller [C] in the dataset request converter, Verify the partial, the database link in model [M], etc., the MVC model is designed to be more flexible, and the most important thing is that you can quickly break the project into small chunks, then fold them into smaller chunks and fold them into a single method attribute of the individual class. When you have finished the small pieces of code, the system is finished.