On the MVC architecture-what are you capable of?

Source: Internet
Author: User

first, what is MVC 1. Concept

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

Small finishing touches: MVC is like a three-layer architecture, mainly using encapsulated (layered) ideas to reduce coupling, so that our system more flexible, more scalable.

2. Content

Model is the part of your application that handles application data logic. Typically, model objects are responsible for accessing data in the database .
View is the part of the application that handles the display of data. typically views are created from model data .
A controller is a part of an application that handles user interaction. typically the controller is responsible for reading data from the view, controlling user input, and sending data to the model

Small finishing touches:

3. Advantages (1) First, the most important point is that multiple views can share a model. The same model can be reused by different views, which greatly improves the reusability of the code.
(2) since the three modules of MVC are independent of each other, changing one of them will not affect the other two, so a good loosely coupled component can be constructed based on this design idea.
(3) In addition, the controller improves the flexibility and configuration of the application. the controller can be used 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.
4. Disadvantages

(1) Increase the complexity of the system structure and implementation.

For a simple interface, strict adherence to MVC, which separates the model, view, and controller, increases the complexity of the structure and may result in excessive update operations and reduced operational efficiency.
(2) The connection between the view and the controller is too tight .

Views and controllers are separated from each other, but they do have a tight connection, the view has no controller, its application is very limited, and vice versa, which prevents them from being reused independently.
(3) A view of inefficient access to model data .

Depending on the model operator interface, the view may need to be called multiple times to obtain sufficient display data. Unnecessary frequent access to unchanged data will also compromise operational performance.
(4) Currently, the MVC architecture is not supported by General advanced interface tools or constructors.

The cost of transforming these tools to accommodate MVC needs and to build separate parts is high, resulting in the difficulty of using MVC

Small finishing touches: According to the strengths and weaknesses of MVC, we know that every thing has its own scope of application.

for large applications that have a large number of user interfaces and complex business logic, MVC will make your software a new step in robustness, code reuse, and architecture. Although it will take a certain amount of effort to initially build the MVC framework, it will greatly improve the efficiency of later software development in the long run.

Therefore, we should consider the scope of application in the specific application,

second, the principle of work 1, the Web Working principle diagram



Small finishing touches: This picture depicts the processing of WebForm, which requests the server through a URL, accesses the Page_Load () method of the Index_aspx class, and then returns the result based on its logical structure .


Small finishing touches: This picture describes the way MVC is handled, the two are obviously different, the first to access the URL address is not the same, it represents the news object's index method, so first access to the method according to the routing configuration, Then the results are drawn according to the business logic, and finally the index.cshtml view is called to display the results

2, internal working principle diagram


Small finishing touches: This picture above is the process of MVC: first the controller receives the user's request and decides which model should be called to process. The model then processes the business logic according to the user request and returns the data. The last controller calls the corresponding view to format the data returned by the model and renders it to the user through the view.

3. Small example (1) First write a dog entity class in the model layer

    public class Dog    {public        int ID {get; set;}        public string Name {get; set;}        public override string ToString ()        {            return ' id= ' + this.id + ", name=" + this. Name;        }    }

(2) first write the corresponding code in the controller layer

 Controller Class (inherited controllers) public class Homecontroller:controller {#region 0.1 initialize data set void Intdata ()//            /<summary>///initialization data set///</summary> public list<models.dog> intdata () {                list<models.dog> list =new List<models.dog > () {new Dog () {id=1,name = "Sample ~ ~"}, New Dog () {id=2,name = "sample 2~~"}, New Dog () {id=3,name = "sample 3~~"}, New Dog () {id=            4,name = "Sample 4~~"},};        return list; } #endregion//GET:/home///<summary>///action method (can be called model of MVC design mode///</ summary>//<returns></returns> public ActionResult Index () {System.Text.            StringBuilder sbhtml = new System.Text.StringBuilder (4000); 2.1 Handles the current business (such as reading a database.)            Judgment, etc.)//2.1.1 create a data set (pseudo-data, and get the data list<models.dog> List = Intdata (); 2.1.2 timesCalendar collection, generate HTNL code, and store it in the builder list.                            ForEach (D + = {sbhtml.append ("<div>" + d.tostring () + "</div>");            }); 2.2 Using VIEWBAG to transfer data to a index.cshtml view with the same name//viewbag is a collection of dynamic types that can dynamically add properties and values of any name of any type VIEWBAG.HTMLSTR =            Sbhtml.tostring ();        return View (); }    }

(3) Finally display the data in the view layer

@{    Layout = null;} <! DOCTYPE html>

(4) Operation


Iii. Framework vs. design mode

1, the framework is usually code reuse, and design patterns are design reuse, the architecture is in between the two, some code reuse, part of the design reuse, and sometimes analysis can be reused.

There are three levels of reuse in software production: internal reuse , which is an abstract block that can be used publicly in the same application; code Reuse , which combines common modules into libraries or toolsets for use in multiple applications and domains, and application framework reuse , which provides a common or off-the-shelf infrastructure for specialized areas for the highest level of reusability.

2. The design pattern is a description of the recurring problems in an environment and the solution to the problem, which is more abstract than the framework . The framework can be represented in code, can be directly executed or reused, and only instances can be represented in code for a pattern.

The design pattern is a smaller element than the frame . A framework often contains one or more design patterns, and the framework is always targeted at a particular application area, but the same pattern can be applied to a variety of applications.

It can be said that the framework is software, while design patterns are software knowledge.

3, in short: The framework is a great wisdom, used for the division of Software design, design mode is a small skill, the specific problem of the solution to improve code reuse rate, reduce coupling degree.


Summary: through the MVC learning, we want to know that it is used to split the page and database access framework, reduce the coupling between the two, while the controller control, but also make the system more flexible, so that our code more robust, more reusable, But everything has his scope of application, we can not think it good, dare not 3,721 blind use, sometimes may be counterproductive, so the study of MVC also need us to slowly explore.


On the MVC architecture-what are you capable of?

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.