From L3 architecture to MVC-MVP

Source: Internet
Author: User

Of course, some questions about this architecture model will also be introduced in the following content. In addition, if you have any different points of view, you are welcome to make a brick (as long as you do not face it, haha ).

1. Who proposed MVC?

Model-View-controller (MVC) is Xerox PARC in 1980sProgramming LanguageA Software Design Model invented by Smalltalk-80 has been widely used so far. Sun's J2EE platform design model has been recommended in recent years and is welcomed by more and more developers who use ColdFusion and PHP. Model-View-controller mode is a useful toolbox, which has many advantages but also has some disadvantages.

Ii. Is MVC suitable for development of large projects?

The MVC framework is certainly suitable for development of large projects, but it does not mean that with the MVC framework, we can develop large projects. It sounds a bit difficult. In fact, the reason is that people (developers ). If you are a person who has in-depth research on the design concept of the MVC framework, you will have to consider some issues anytime, anywhere when using the MVC Framework for product and project development:

1. Low coupling (separation of view layer and business layer is emphasized)

2. testability (this is important)

3. High reusability and Applicability

4. Facilitate software engineering management and so on.

Here, I really appreciate Lao Zhao's academic attitude, because in hisArticleAndCodeI am thinking anytime and anywhere, especially when I think about testability and unit testing (not TDD here). It makes me feel connected. Because these problems need to be seriously considered in medium-sized or even large projects, it is by no means that the example given by Microsoft is our only criterion, and there must be either right or wrong in it. I believe that in front of MVC, there are not many cool people in China or even Microsoft.

After talking about this, you can realize that if you do not understand the figure below and have an understanding of MVC's "where is the so-called advantage, when MVC is used to develop large-scale projects, I think not only can we not make full use of the Asp.net MVC framework, but will always be subject to the constraints, configurations, and functional features in it, in the end, it is better to directly use Asp.net webform for development, isn't it? In this situation, I think not only can I use MVC for large-scale development, but can't even handle small and medium-sized enterprise applications.

Iii. Can I use the MVC Architecture for webform development?

Someone tried to use ASP. net MVC Framework for webform development. I personally do not like this practice. This is just as "weird" as using SQL statements simultaneously in the projects using LINQ ", this is confusing in the overall style of the Code, isn't it? Of course, Lao Zhao also mentioned in his video course that some MVC functions (such as modelbinder) are used on the webform page ), however, I think Lao Zhao's intention is not to make the development of this hybrid method popular. It must be "non-mainstream", so it is important to grasp it by yourself.

4. Traditional three (n) layer architecture and MVC, and the relationship between MVC and MVP

The three-tier architecture mentioned in this Article refers to: presentation layer (display layer), business logic layer, and data access layer (persistence ). If you need to put it together with MVC, then I can only "Twist it" here, that is:

The ASPX page of the "presentation layer" in the three-tier architecture corresponds to the view in MVC (different inherited classes)

The aspx of the "presentation layer" in the three-tier architecture. the CS page (class) corresponds to the Controller in MVC. It is not difficult to understand this point. Let's think about the Redirect we have previously written. Of course, it itself jumps to some link pages, the Controller in MVC is better, and it controls and outputs a view. The role of this function is to control the business process and display information, but the implementation methods are different.

In the three-tier architecture, the business logic layer and the data access layer correspond to the Model in MVC (view and controller must have been found in the "Mother-in-law", and the remaining model can only be the business layer and data access layer, haha ). However, some of Microsoft's MVC example projects do not understand this problem in this simple way. This point has been explained in my previous thoughts on the two MVC examples (mvcstore and oxite, I will not go into detail.

If you understand this relationship, you can try to migrate the previous three-tier structure to the MVC Framework. Of course, this process will certainly encounter one or another problems, but the principle is to take the advantages of MVC to the extreme, otherwise it is better not to do so. Speaking of this, as early as N years ago, someone proposed to use frontcontroller (front-end Controller) to redirect HTTP page requests (including data presentation ), to put it bluntly, httphandler is used to process page requests and bind data, rather than simply "expecting" normal page access redirection. The purpose of doing so is to be consistent with the starting point of the controller and action in routing, except that the routing implementation is more elegant and more underlying.

After talking about the three-tier architecture, let's look at MVC and MVP. In fact, before that, we need to look at this figure:

After reading the above figure, you will find that the biggest difference between MVP and MVC is that "the bottom of the model and view layer should not communicate (or even two-way communication ,). I think this is also the battlefield where experts who are currently engaged in these two fields are arguing. Each has its own benefits and the price to be paid for the benefits. At least the presenter in MVP mode must have "absolute power ". Without it, model and view are two isolated islands. Although each has its own territory (completely decoupled), they will not bring any useful value to the Enterprise. So here is a metaphor to describe MVPs:

Presenter-a woman with a strong desire for control, even "in what posture", it has to take care of it.

Of course, if you worry too much, you will have to do more and more things. In the end, it will face the increasing complexity of the layer of code, and it is not easy to write, even the BIND of this kind of chicken feathers to the event must be taken care of by it. In the end, we can see that the view in MVP really has a lot of code idle (for State-Owned Enterprise Employees). It is no wonder that as long as the view implements the corresponding attributes and some simple methods under the corresponding iview interface, it will be done, the view instance that finally calls the iview interface is completely handed over to the presenter, this reminds me of the support of the "Custom template engine" in MVC (the MVC framework is used to control the use of the system or custom template engine) "as well as the skin-changing function, which is commonly used at ordinary times, it is really interesting to think about it here (on the spiritual level ).

Of course, Microsoft's internal understanding of MVP is also different, as mentioned in the supervising controller mode and passiveview we have seen above.

The supervising controller mode is actually very similar to the MVC figure, but provides "two-way communication" between the presenter and the view ". This practice also has many different opinions, at least for developers who support "one-way dependency", it is "sneer.

Speaking of this, let me express my point of view. I prefer passiveview. Although this mode is a bit overbearing, it must be a real decoupling between the model and view, it provides developers with the greatest sense of accomplishment in control. You can say that you can control the view if you want to control it, but the problem caused by this is the amount of code writing and the complexity of implementation.

5. What should be included in the Controller and model?

Because the logic in the view is relatively simple, the consideration of system complexity should basically be focused on the model, while the controller is the control of business processes, which should be "refreshing code ". But can the line between the two be so clear during project development? The answer is "keep it as secure as possible ". It must be the "characteristic" of MVC.

In addition, we recommend a good method "robustness". With this method, you can easily find out which system methods should be placed in the model and which should be included in the control logic, I mentioned this method in an article two years ago. If you are interested, you can refer to this link and use the [iconix] method to practice blog design 4 [robustness analysis]. its core content is as follows:

Entity object ):

Objects in the domain model (that is, the real world) usually correspond to database tables and files. These data tables and files store the data required to execute use cases. Some entity objects are "temporary" objects (such as search results) and will disappear after the use case ends.
Boundary object ):

Participants use it to interact with the system, which usually contains windows, screens, dialogs, and menus. If you have a GUI prototype, you will know what many major boundary objects are.

Control object ):

Associates boundary objects with object objects (usually called controllers because they are not really objects). It contains most of the application logic, they build a bridge between users and stored data. The control object contains frequently modified business rules and policies. In this way, you only need to modify these objects, instead of the user interface and database mode. The Controller occasionally (20% of the time) will also be the "real object" in the design, but most of the time, the controller is just a placeholder, this prevents you from missing any functions and system actions required by the use case.

The above three objects correspond to model, view, and controller respectively.

As mentioned in this Article, this method also provides the following benefits:

1. It helps you ensure the correctness of the case text, and does not specify unreasonable or impossible behavior (based on a group of objects to be used), thus providing health check.

2. It helps ensure that all necessary branch processes are taken into account in the use case, thus providing integrity and correctness checks.

3. this allows you to (continuously) discover objects because some objects may be missed during Domain Modeling, and these objects are not easy to be found when drawing sequence diagrams, and it is likely that it makes it impossible to draw a sequence chart.

4. narrow the gap between analysis and design to final the preliminary design (the preliminary design review will be introduced in the next article ).

6.Can MVC and MVP appear in an SLN or even a project at the same time?

I don't want anyone to count on this point. Only user requirements are king. Users can use some specific functions in the current project. This function is exactly where MVC or MVP works, the word"Use".

At last, I would like to explain one more point:

Business logic is part of the core value of the system architecture. Its focus is mainly on the formulation of business rules, the implementation of business processes, and other system designs related to business needs. Therefore, for a system, business logic is everywhere. View is the display logic, and controller is the process control logic). The model is simply a "logical base camp.

When using the MVC framework, we need to split the "frequently changing" Business Rules (in the Controller) and the relatively stable business logic (in the model), and at the same time adopt the interface method at the model layer, to cope with changing business needs in the future.

Http://developer.51cto.com/art/200904/122075.htm

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.