MVC and three-tier architecture

Source: Internet
Author: User
Tags ldap xml xsl web2py

three-tier architecture and MVC

The three-tier architecture (3-tier application) divides the entire business application into: The presentation layer (UI), the Business Logic layer (BLL), the data Access Layer (DAL). The purpose of distinguishing hierarchy is to "high cohesion, low coupling" thought.

1. Presentation layer (UI): the interface displayed to the user, which is what the user sees when they use a system.

2, the Business Logic layer (BLL): The operation of the data layer, the data business logic processing.

3, data access Layer (DAL): direct operation of the database, for the data to add, delete, modify, update, find and so on.

MVC is Model-view-controller, and strictly speaking, these three add up is the UI layer in the three-tier architecture, which means that MVC divides the UI layer in the three-tier architecture again into controllers, views, entities. The controller completes the page logic by using the entity to complete the call with the interface layer, while the C layer directly talks to the BLL in the three layer.

MVC can be a presentation layer in layer three, which belongs to the presentation layer. Layer three and MVC can coexist. Layer three is based on business logic, is an architectural design, and MVC is based on the page to divide, is a design pattern.

Introduction to the MVC pattern

The MVC pattern is the guiding mode of GUI interface development. It divides the program into three parts based on the thought of the separation of the presentation layer: Model-view-controller, triangular structure.

Model refers to the data and application logic, which is the view of the model, which is the user interface. Both are well understood, and the key point is the controller's role and the relationship between the three. In the MVC pattern, the controller and view belong to the presentation layer, which usually appears in pairs. A controller is designed to handle the logic of user interaction.

MVC is the architecture of the presentation layer, and MVC's model is actually ViewModel, which is the data for the view to show. ViewModel does not contain business logic, nor does it contain data reads. In the N-tier architecture, there is usually a model layer that corresponds to the database table, which is called the O in Orm. This model may be poco, or it may be an entity class that contains some validation logic, and generally does not contain data reads. Data is read by the data access layer. MVC, as the UI layer, typically does not manipulate the data access layer directly, and there is a business logic layer that encapsulates the business logic and invokes the data access layer. The UI layer (Controller) obtains the data (Model) through the business Logic Layer and encapsulates (ViewModel), then selects the appropriate view.

MVC exists in the desktop program, m refers to the data model, v refers to the user interface, C is the controller. The purpose of using MVC is to separate the implementation code for M and v so that the same program can use a different representation. For example, a batch of statistical data can be represented by a histogram, pie chart, respectively. C exists to ensure the synchronization of M and V, and once M is changed, V should be updated synchronously.

How MVC works

MVC is a design pattern that makes it mandatory to separate the input, processing, and output of an application. Using an MVC application is divided into three core parts: model, view, controller. Each of them handles their own tasks.

    1. model M models represent enterprise data and business rules. Of the three parts of MVC, the model has the most processing tasks. The data returned by the model is neutral, meaning that the model is independent of the data format, so that a model can provide data for multiple views. Because the code applied to the model can be reused by multiple views only once, it reduces the repetition of the code.

    2. The Controller C Controller accepts the user's input and invokes the model and view to complete the user's needs. So when you click a hyperlink in a Web page and send an HTML form, the controller itself does not output anything and do any processing. It simply receives the request and decides which model component is called to process the request, and then determines which view to use to display the returned data.

The model model is the main part of the application. Models represent business data, or business logic. Implementation of the specific business logic, state management functions.

View view is a part of the user interface in an application that is the interface that the user sees and interacts with. is the page that interacts with the user, usually realizes the input and output function of the data.

Controller controller controllers work to control user interface data display and update model object status according to user input. Play the role of controlling the whole business process, and realize the collaborative work between view layer and model layer.

Why use MVC

Most Web applications are created using a process like asp,php, or CFML, which has been fully supported by the object-oriented model since the PHP5.0 version. They mix data-tier code like database query statements with presentation layer code such as HTML. The more experienced developers separate the data from the presentation layer, but this is often not easy to do and requires careful planning and ongoing attempts. MVC is fundamentally mandatory to separate them. Although it takes some extra work to construct an MVC application, the benefits it brings to us are no doubt.

First, the most important point is that multiple views can share a model, and now more and more ways to access your application. One solution to this is to use MVC, whether your users want a flash interface or a WAP interface, to handle them with a single model.

Second, you have separated the data and business rules from the presentation layer, so you can maximize the reuse of your code.

Thirdly, the data returned by the model is not formatted, so the same artifacts can be used by different interfaces. For example, many of the data may be represented in HTML, but they may also be represented by Adobe Flash and WAP. The model also has the capability of state management and data persistence processing, for example, session-based shopping carts and e-commerce processes can also be reused by flash sites or wireless networking applications.

The model is self-contained and separate from the controller and view, so it's easy to change the data layer and business rules of your application. If you want to port your database from MySQL to Oracle, or change your RDBMS-based data source to LDAP, just change your model. Once you've implemented the model correctly, the view will display them correctly, regardless of whether your data comes from a database or an LDAP server.

Since the three components of an MVC application are independent of each other, changing one does not affect the other two, so you can construct good loosely coupled components based on this design idea. For me, the controller also provides the benefit of using a controller to join different models and views to complete the user's needs, so that the controller can provide a powerful means of constructing the application. Given some reusable models and views, the controller can select the model to process based on the user's needs, and then select the view to display the processing results to the user.

Take a simple login module, said, the requirement is that you enter a user name, password, if the input is the same as predefined, then go to the correct page, if not the same, prompted an error message "You y don't put me here, the wrong input!" ”。 In this small module, the page that starts with the username and password is the same as the page that is displayed after the check. A controller page is also required to receive the username and password entered, as well as a flag returned after the check (this flag is used to determine whether you entered the correct, and jump to the corresponding page). Finally, there is a model, then you are the class for the calibration, he is to deal with your input is not the same as the pre-ordered, and then return to a flag. This completely realizes the separation between the logic and the page, my page no matter what you do, I will be a display, and controller also regardless of your model how to judge right, anyway, I gave you the user name and password, you have to give me the whole back to a flag, and model it, Anyway, you dare give me a username and password, I will give you a flag.

The MVC pattern was introduced in the 70 's and was originally used on the Smalltalk platform. MVC is a presentation pattern, which is used to show the user that many of the formed patterns (ui/presentation Patten) MVC have three roles:

    • Model: The component used to store data (different from the domain model concept, which crosses each other)
    • View: A component that gets data from the model for content presentation. The same model can show different effects under different view. Gets the state of the model and does not manipulate it.
    • Controller: Accept and process user instructions (Operation model (business)), select a view to operate.

MVC Overview: There are one-way references to collaboration, such as model does not know the presence of view and controller. View does not know the existence of a controller. This isolates performance and data. View and controller are one-way references. In fact, the view and controller also have data interaction.

The important feature of MVC is separation. Two separations: the separation of view and data (model) uses different view to present the same data, separates the visible and invisible components, and can independently test the model. Because separating the visual components reduces the external dependencies to facilitate testing. (The database is also an external component) the separation Controller of view and Representation logic (Controller) is a component of performance logic, not a business logic component. MVC can be used as a pattern of expression as well as a constructive pattern, meaning that the controller can also be a business logic. Separate logic and concrete presentation, allowing for independent testing of logic.

is MVC and three-tier architecture similar to the three-tier architecture?

View--->ui Layer | | Controller--->bussiness Layer | | Model--->data Access Layer is actually wrong. MVC is the representation pattern (Presentation pattern) Three-tier architecture is a typical schema pattern (Architecture pattern) The layered pattern of the three-tier architecture is a typical up-down relationship, with the upper layer dependent on the lower layer. But MVC, as a performance model, does not exist in the upper and lower relations, but is a collaborative relationship. Even if MVC is used as an architectural pattern, it is not a layered pattern. MVC and the three-tier architecture are basically not comparable and are applied to different areas of technology.

MVC mode with three-tier architecture:


the relationship between MVC and layer threeSummary

In this chapter, we learned about the MVC pattern. MVC is a very important design pattern for organizing applications into three parts: models, views, and controllers.

Each part has a clear responsibility. The model is responsible for accessing the data and managing the status of the app. Views are the external manifestations of a model. Views do not have to be graphical, and text output is a good view. A controller is a connection between a model and a view. The proper use of MVC ensures that the application of the final output is easy to maintain and easy to scale.

The MVC pattern is the SOC principle applied to object-oriented programming. This principle is similar to how a new house is built, or how a restaurant operates.

The Python framework web2py uses MVC as the core architectural concept. Even the simplest examples of web2py use MVC for modularity and maintainability. Django is also an MVC framework, but it uses the name MTV.

When using MVC, be sure to create intelligent models (core functions), thin controllers (the ability to communicate between views and models), and fool-like views (external performance, minimized logic processing).

MVC and three-tier architecture

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.