Talking about. Net Program Design -- Talking about the MVC Architecture Model (I)

Source: Internet
Author: User

Recently, I have heard some people discuss the relationships and improvements between various architecture models of MVC, MVP, and mvvm. I also want to write something about these three models, and compare their differences. In daily development, we have many opportunities to access MVC and MVP. mvvm may be more exposed by colleagues engaged in WPF and Silverlight, however, it is certain that either mode is used to solve some practical problems. These three models have a certain evolutionary order. As we all know, the first thing we have come into contact with is MVC, followed by MVP, followed by mvvm in recent years. They solve different problems and use different scenarios. They can be said that each of their respective uses has its own advantages. So how can we determine when to adopt this mode? It may be possible to make relevant choices only when used in actual work.

In fact, in my actual work, I can only say that these three models are far from proficient. It may be that you have been catching up on the progress and have no time to stop and make a summary. So I would like to summarize the three modes here.

Let's get down to the point. Let's first discuss the MVC Architecture Model. I first came into contact with MVC in my work over the past few years (there were no later two models at the time, haha). At that time, the most intuitive feeling was to implement m and V.CodeTo separateProgramThere can be different forms of presentation; C is used to connect m and V. In fact, in MVC (Model View controler), m -- exponential data model, V -- view is the user interface, and C -- controller. MVC is Xerox PARC in 1980sProgramming LanguageA Software Architecture Model invented by Smalltalk-80.

Using MVC is mandatory to separate input, processing, and output. The three parts perform their respective duties. First, let's look at what views do.

A view is an interface for user interaction with a program. for net, the view is composed of HTML and ASP. currently, HTML is still playing a major role. Of course, RIA is very popular recently, and other technologies such as flex and Silverlight are emerging one after another. In fact, they are used to improve user experience, however, it is also a view. With the update of technology, the application interface becomes more and more challenging. One advantage of MVC is that it can make your program process different views, and there is no real logic in the view. Microsoft ASP. NET is event-driven, that is, its actions are implemented by control events. For example, click a button. The real business is encapsulated in the model layer.

Model: The business layer, which mainly represents data and its business logic. The model has the most processing tasks. The data published by the model is business-based, that is, it is not directly associated with the view. In other words, a model data can be displayed in multiple views, which can reduce code duplication. Of course, you can add a service layer to the model and view layers to encapsulate the business logic inside the service and expose the service to external users. This is more loosely coupled, it is also easier to expand and maintain. You can switch to SOA slowly. Of course, SOA is a very complicated model. We will discuss it later and will not elaborate on it. We will discuss about the knowledge of WCF in the future.

Then it should be the controler-controller layer. The controller accepts user input and calls the model and view to fulfill user requirements. Therefore, when you click the button and submit button on the web page, the controller does not output or process anything. It only receives the request and decides which model to call to process the request, and then determines which view to use to display the model to process the returned data.

Specific processing process, such:

 

(This figure is from the Internet)

Although the figure is relatively simple, the MVC logic has been illustrated. First, we can see that the user accepts the user input through the view layer and then passes the input to the Controller, the Controller determines which model should be used for processing, and then the model is used to process the final result data returned by the user's request. Then, the controller uses the view to format the data and finally displays the result to the user, this completes an interaction.

The above is the overall logical expression of MVC. Next, let's talk about the advantages of using MVC. Why should we use MVC and what are the shortcomings of using MVC?

What are the advantages of using MVC? First, the most intuitive thing is that multiple views can share a model. That is to say, the business logic is encapsulated separately and what view is used is flexible. We can use HTML or flash, silverlight can be used. In this way, the presentation layer is completely separated to implement the loose coupling of the UI to the business. This greatly improves the reusability of the Code.

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. For example, if Oracle is used in the back-end database, and the customer needs to change to sqlserver or MySQL, we may have to make earth-shaking changes and restructures. But to use MVC, we only need to change the model. We thought that the model layer is self-included and will not affect the other two layers, so that we can minimize the number of changes and increase the scalability to a high level.

The advantages of MVC are described above, but there is no perfect thing, and nothing is flawed. Although MVC is powerful, MVC is not clearly defined, the use of MVC requires careful design, and its implementation is complicated. In the early stage, it takes a lot of time to design. It takes time to consider how MVC can be combined with a program. Every layer requires careful testing. Flexibility brings about design complexity. It takes time to consider the abstraction and implementation of various conventions. MVC is generally not recommended for small projects. However, after a period of experience, the model is also encapsulated by a mature platform, therefore, it is much simpler to use, such as Microsoft's ASP. net MVC is a typical example.

In short, MVC is also a double-edged sword. I feel that everything must be used in different ways. I need to use it when I need it. If I don't need it, I don't need to fix it and try to find trouble for myself. The specific degree also takes time to experience and gradually summarize and feel it.

This article is just a theoretical explanation of the MVC Architecture Model. I will give you an example to learn more about it in the next article (I have been working overtime recently, so I 'd like to make a wish ).

If you have any shortcomings, please point them out. Thank you!

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.