Application Framework Combat 30: Presentation layer and ASP (i)

Source: Internet
Author: User

This article will cover some of the highlights of the presentation layer and ASP, especially the abstraction and encapsulation techniques of ASP. If you are not familiar with MVC, you can refer to "ASP. MVC4 Advanced Programming", author Jon Galloway, etc., this book by ASP. The MVC team is writing, pretty good.

the responsibility of the presentation layer

the role of the presentation layer is to present and collect data , present the data and logic of the domain layer, and gather information about the user input.

Once you understand the responsibilities of the presentation layer, you should be aware that the presentation layer is not the place where you should write the business logic, which is the core of the layered architecture.

If you want to show a calculated value, you should not complete the calculation directly in the presentation layer, instead, it is calculated by the domain layer, and the presentation layer directly gets the result and shows it, which makes the presentation layer code very concise, while the business logic is highly cohesive to the domain layer.

Many times, in order to improve the user experience, we will use JS on the page for client computing, so as to get a quick response, but also cause the business logic leaked into JS. In most cases, this is acceptable, but when you are ready to use JS to write these repetitive logic, you can first consider whether you can use the AJAX request server to complete. Whenever you need to perform real-time client computing, send a request to the server, the domain layer to complete the calculation, and return the results.

Minimize copies of business logic and make code synchronization easier when requirements change or bugs are found.

For the management system, as far as possible for programmers to write less JS, as far as possible to encapsulate and abstract, because JS is weak type, no compile-time check, code hints is very weak, even if the use of ReSharper, code hints still poor, slender tips scroll bar, see you two eyes straight take Venus. In addition JS a lot of tricks look very strange, beginners use these skills, bug rate will rise sharply. When the project to the new maintenance, long-length JS more make it miserable. Of course this is to the general team's advice, less write JS can make your project easy to maintain, high-level team please ignore.

Introduction to MVC

MVC is a presentation layer architecture pattern that divides the presentation layer into three parts, models, views, and controllers.

When an HTTP request is sent to the ASP. NET MVC engine, the MVC engine looks for the routing table to determine which controller action to handle the request.

The controller is a command center that invokes the backend service and gives the resulting model to the view display.

You can manipulate dbcontext or warehousing directly in the Controller method, and query using LINQ statements. When you gradually realize that the controller code becomes complex, you can create an application-tier service to simplify the presentation layer.

Application services provide a unique API access point for the presentation layer, significantly reducing the complexity of the controller, all of the controller's operations through the application service a clear API, not only the operation is more standardized, and the controller will become a very thin layer.

  the development point of the controller is to keep it as simple as possible, nothing less write code . Give your needs to the application layer service to implement, you just need to simply call its interface on the line.

The model refers to the data and business logic, the domain model, and most of the time you can manipulate the domain entities directly in the MVC view, and the view model ViewModel is not required. However, for some reason, the operation in the view may also be a dto or ViewModel, and when the interface becomes complex, the interface development can be simplified by introducing specialized viewmodel for a particular view.

You may have discovered a myriad of entity types: domain entities, DTOs, ViewModel, query entities, and so on, and I'll be discussing them in the next article to make it easier for you to choose.

conventions better than configuration

The "convention is better than the configuration" principle, it is recommended that you try to follow the default conventions and form a habit, which can greatly reduce the cost of learning and work, while obtaining a more consistent directory structure and code style, find a specific type of file becomes very easy, maintainability is greatly improved, you only need to be configured to override the default value.

The "Convention over configuration" principle has far-reaching implications for directory structure and naming.

The directory structure of the project is critical, but most programmers do not care much about the directory structure because the creation of folders has no technical content and is easy to overlook.

When you are ready to modify a feature, the first step is to find the appropriate file first.

If you don't pay much attention to the management of the directory structure, create a file very casual, casually find a place to throw in, with the increase in project files, your development work will be more and more chaotic, when you want to find a file, you will be in the directory tree everywhere, "Oh, not here, oh, not there, oh yes, finally found."

When you're looking for a controller, you're going to look for the controllers directory, not a directory called ABCD, which is the naming convention. If there is a controller named TestController, the default is to have a test directory in the views directory corresponding to it, so it is very easy to find the corresponding view of the controller.

When all of your documents can be categorized into clearly named directories according to the conventions, the quality of the whole project will be greatly improved.

When I encapsulated Easyui, I made some conventions about naming the control ID, such as a table called grid. So many JS callback function, can be done in-house, do not need you to manually handle, if you follow these conventions, develop a simple easyui crud operation, basically do not need to write JS.

. NET Application Framework Exchange QQ Group: 386092459, welcome interested friends to join the discussion.

Thank you for your continued attention, my blog address: http://www.cnblogs.com/xiadao521/

Application Framework Combat 30: Presentation layer and ASP (i)

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.