Application Architecture Guide 2.0 学习笔记(一)前言 - Presentaion Layer

来源:互联网 发布:网络水军公司如盛 编辑:程序博客网 时间:2024/05/21 06:03

Presentation Layer

The following guidelines will help you to understand the fundamental factors you must consider when designing the presentation layer for your application. Use these guidelines as a starting point toward understanding key concerns, such as how to validate input on the client and on the server, and how to choose between the Model-View-Controller (MVC) and Model-View- Presenter (MVP) patterns.(下面的指导将帮助你理解在为应用设计表现层时必须考虑的基础因素。例如:如何在客户端与服务器端验证输入,如何选择MVC模式还是MVP模式)


• How to validate input(如何验证输入)


Assume that all input data is malicious(要假设所有的输入数据都是有恶意的). Constrain, reject, and sanitize your input(约束,拒绝并清洁你的输入) because it is easier to validate data for known valid types, patterns, and ranges than it is to validate data by looking for known bad characters(因为验证已知合法的类型、模式和范围的数据是要比查找恶意字符来验证要容易得多的). Validate data for type, length, format, and range(验证数据类型、长度、格式和范围). Consider using regular expressions for validating the inputs(考虑使用正则表达式来验证输入). For an improved user experience, consider using client-side validation(为更好的用户体验,考虑使用客户端验证), but always perform validation at the server as well(但是也要一直进行服务器端验证). Encode your output(将输入编码).


For more information, see Chapter 3, “Architecture and Design Guidelines.”


• How to use the MVC pattern(如何使用MVC模式)


To use the Model-View-Controller (MVC) pattern effectively, you must understand the division of labor within the MVC triad (the Model, the View, and the Controller)(要高效的使用MVC模式,就必须要理解MVC三元组的分工). You must also understand how the three parts of the triad communicate with each other to process requests from user input(你也必须要理解这三部分是如何通信来处理用户输入请求的). The Model represents data(Model表示数据), and the View is the UI, which displays the data to the user and contains controls for the user to interact with the data and the application(View是用户界面,用来将数据显示给用户并包含用户与数据交互的控件). The Controller is responsible for handling requests, initializing the Model and choosing the appropriate View(Controller控制器用来处理请求、初始化Model并选择适当的View). Common Web implementations use HTTP handlers or Internet Server API (ISAPI) filters to intercept requests and send them directly to a controller(普通的Web实现使用Http Handlers或者ISAPI过滤器来拦截请求并将它们直接发送给Controller). There are two main variations of the MVC pattern: the Passive Model and the Active Model(MVC模式有两种不同的形式:被动模式与主动模式). In the Passive Model pattern, changes to the Model are only captured when the Controller processes a request(在被动模式下,对与Model的改动只会在控制器Controller处理请求的时候被捕获). However, in the Active Model pattern, an Observer pattern implementation can be used to notify the View and/or the Controller when changes occur in the Model(然而,在主动模式下,可以使用观察者模式,在Model发生变化的时候通知View或者Controller). One of the limitations with this pattern is that, because the controller is responsible for intercepting and handling requests, you lose capabilities associated with the View such as the ability to handle control events and the use of viewstate in ASP.NET(这种模式有一个限制,因为Controller只负责拦截与处理请求,就丧失了某些与View相关的能力,例如处理控件事件的能力与使用Asp.net ViewSate的能力).


For more information, see Chapter 10, “Presentation Layer Guidelines.”


• How to use the MVP pattern(如何使用MVP模式)


The Model-View-Presenter (MVP) pattern is very similar to the MVC pattern(MVP模式与MVC模式非常相似), with the main difference being that Views handle requests and pass them to a presenter, which provides controller logic(主要区别在于在MVP模式里,View处理请求并将它们传送给一个Prensenter表现者,这样来提供控制器逻辑). Similar to the controller in MVC, the Presenter is responsible for processing requests and initializing the model(与MVC里的Controller类似,Prensenter负责处理请求并初始化Model). The main advantage of using this pattern over MVC is that, because the view handles requests, you also have support for control events and the ability to maintain the state of controls in the view(使用MVP模式相对于MVC模式最大的优势就是,因为View来处理请求,你也就能支持控件事件,View也具备了维护控件状态的能力。). There are two main variations on this pattern, Passive View and Supervising Controller(在这种模式上,有两个重要的变动:被动View与监督Controller). With Passive View, requests are intercepted by the View and passed to the Presenter, and then the Presenter initializes fields in the View through an interface(使用被动View,View拦截请求并传递给Prensenter,然后,Prensenter通过一个接口初始化View的字段). This variation completely decouples the view from the Model and provides the highest level of testability(这个变动彻底的将View从Model解耦,并提供了最高级的可测试性). With Supervising Controller, the View passes requests to the Presenter, the Presenter notifies the View when the Model is initialized, and the View accesses the Model directly(使用监督Controller,View将请求传递给Prensenter,Prensenter在Model初始化完成时通知View,View直接访问Model). This variation is useful when you have a lot of data that needs to be presented in the View, which makes Passive View impractical(这个变动在你有很多数据需要在View上表现是很有用,这样使得被动视图不切实际???).


For more information, see Chapter 10, “Presentation Layer Guidelines.”

原创粉丝点击