Asp. NET no magic--asp.net MVC & Layering

Source: Internet
Author: User
Tags what is asp

The previous article briefly explains what MVC means and provides detailed steps for creating a project and its controllers, views, and so on, and finally completes a simple ASP. NET MVC program.
Note: MVC is not equal to ASP. NET MVC is a development pattern, and ASP. NET MVC is one of the implementations of MVC, which, if not specifically mentioned in this article, represents ASP.
What does this article represent from the m-v-c of ASP. How do I write the corresponding code? To discuss how to develop applications using ASP.
0asp.net MVC and Layering
What does M in 0asp.net MVC stand for?
How 0asp.net MVC's V and C interact
How C in 0asp.net MVC should handle business logic
0 how to use ASP. NET MVC

ASP. NET MVC and tiering

What is tiering?

Before understanding the layering, first understand the concept of hierarchy, which refers to the hierarchical order of the system in terms of structure or function. It can be divided into various criteria, such as mass, energy, movement state, spatial scale, time order and organization degree. Different levels have different properties and characteristics, both common laws and special laws. (from Baidu Encyclopedia)

  So stratification is actually based on certain standards and rules, a whole is divided into multiple levels, to ensure that each level of content has a common nature and characteristics, easy for each level of maintenance management.

Code layering:

Code layering is the code to divide code according to its functional characteristics, code layering is generally divided into three layers (so also known as the three-tier architecture), respectively, the UI layer, the business logic layer and the data layer, the hierarchical architecture is the entire system of data storage, business logic, UI implementation of the separation of concerns:
0 Data layer: Focus on how the data is stored without the need for business logic to process, only need to invoke the corresponding interface to complete the data acquisition, storage and other functions.
0 Business layer: Differentiate the business to better focus on the implementation of business logic, for some of the more complex business logic systems can use domain-driven (DDD) method to achieve. In addition, the business logic can be reused, a common example is MVC and Web Api,mvc for Web-side application development, the Web API can also be used for other clients or to provide third-party applications, so the implementation of business logic is not in the UI layer, Otherwise, there is a code in the controller that appears in MVC, and the same code exists in the controller of the Web API, which makes the code difficult to maintain.
0UI layer: All you have to do is design the user interface, and the designer just needs to care about how to express the content to the user perfectly and improve the user experience.

In addition, the layered structure of the "layer" should be based on business characteristics, system complexity and other factors to split, layering can let the right people do the right thing, and design patterns in the "dependency inversion" principle to define the module directly to rely on abstraction rather than implementation, as long as the definition of abstraction (interface) so many teams can simultaneously on the different " Layer "for development.

Note: The above layered architecture refers specifically to server-side tiering architectures, such as mobile apps and even some single-page applications that are deliberately layered to enjoy the convenience of layering-generated code management.

What is ASP. NET MVC?

In the previous article, the meaning of ASP. NET MVC and MVC were described. The concept of MVC seems to be similar to the concept of a three-tier architecture, which corresponds to the UI, business logic, and data, respectively. But they are very different,MVC's view, Controller, model is to handle the UI display, operation and data exchange. In other words, ASP. NET MVC is only an implementation of the UI layer in the three-tier architecture.

what m represents in ASP.   

m represents the data model, but there are multiple data models in the application, such as the data model corresponding to database one by one, the data model used for display, and ASP. NET MVC is easy to mix with these two models, for example, user information in addition to user characteristics will include the password for the login system and other information. Changes to the user information in addition to modifying the features will also change the password, etc., but in the display will certainly not show the password. Another situation is a list page, which not only shows the record entry, but also shows the paging information, but the paging information is not stored in the database, so the two models are clearly different.

When ASP. NET MVC is positioned as a UI implementation in a layered architecture, the m here should be considered a data model for display. Some people also call it the view model, but the view model is fundamentally different from the view model in the MVVM mode, where the view model is the data model used for the display.

how the V and C of ASP.

According to the previous analysis of MVC's model is used to show the data, so the appropriate controller should generate a model delivered to the view to render. Conversely, when you submit some data through a Web page, the data comes from view, and the view should also provide a model to the controller to perform the corresponding business, and their relationship is as follows:

  

how c should handle business logic in ASP.

The controller is used to process requests from the client and then gives a response. And this process corresponds to the actual business logic, and MVC is in the business logic layer, so the only thing the controller can do is to directly invoke the business logic, where the call should be sequential, without any logic judgment, all processing is delivered to the logic layer.
How to handle data exchange between controller and business layer? The view model or the Entity model (Database model)?
First, the view model is not available, because MVC is above the business layer, and if you use a view model, the underlying dependency is a violation of the dependency principle. And the view model may contain information such as paging, and is not required by the business layer.
Second, it can be swapped with the entity model, which looks as if MVC and the business are dependent on a solid model. But what if the domain driver is used to design the model? The "entity" at this point contains some of the business logic, and this part of the logic is generally not directly called by the UI layer.
Based on the above analysis, we found that there are non-conforming application scenarios, so introduced the concept of DTO (data Transfer object) Data Transfer object, the following analysis of this concept, the current blog system uses the entity model to process the data exchange first.

How to use ASP . NET MVC

Any tool, different people may have different effects, for software development in addition to complete the development of functional requirements, more importantly, to ensure the development efficiency, software quality, code quality and other non-functional requirements to ensure that the software can be developed at the appropriate time, code can be tested, maintainable. But different developers use tools that are inconsistent, and for ASP, some people may be accustomed to using model as Database model and page model, writing all business logic into controller, too much using viewbag and other objects to transfer data to view, etc. For these methods it is still able to achieve functional requirements, and for individuals to use the method of the most efficient, but for a team often cause a lot of trouble, everyone to maintain their own style, and finally the entire project code has been changed beyond recognition, the code is not readable, not maintained, unable to test and so on.
Therefore, you must unify the specification before using the tool. No rules inadequate surrounding area. ASP. NET MVC is also a specification for MVC in some ways.
There are good and bad norms, but for better or worse, as long as there are norms, it will be easier to modify the specification, this series of articles will be based on the "blog system" progress to set different specifications, for the use of ASP. NET MVC should have the following points:
0Controller can only invoke the business logic sequentially, and there is no judgment statement.
0View in general is only the model object transmitted by the Controller, to avoid the use of ViewBag and other objects.
0Model is used only as a transport object before the view and controller, regardless of the data entity.

Summary:
This chapter further analyzes the concept of MVC, and at the end of the specification, for the specification of more refers to coding specifications, such as naming norms, annotation specifications, and so on, here the specification is only to unify the use of ASP. NET MVC, make the code easy to maintain, test, It is even easier to use code generators to generate code later (more on the code specification, for later introductions).

This article connects: http://www.cnblogs.com/selimsong/p/7646445.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC & Layering

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.