2010年10月22日 星期五 16:58
MVC架构
The model view controller pattern is a result of the recognition that at their heart, most web applications rely on the ability to:
MVC(Model View Controller)模式是人们心中认可的成果,大多数Web应用依赖其能力:
• store and retrieve data in a way that is natural to the programming language involved (the model)
• represent the data in various ways, most commonly as HTML pages (the view)
• write logic code to manipulate the data and control how it is interacted with (the controllers)
• 以相对相关的编程语言一种很自然的方式存储和检索数据(模型)
• 以不同的方式显示数据,最常见的为HTML页面(视图)
• 编写逻辑代码来操纵数据并控制它是如何交互(控制器)
In Pylons each of these components is kept separate. Requests are dispatched to a controller which is an ordinary Python class with methods called actions which handle the application logic. The controller interacts with the model classes to fetch data from the database. Once all the necessary information has been gathered the controller passes the key information to a view template where an HTML representation of the data is generated and returned to the user's browser. The user then interacts with the view to create a new request and the process starts again. The model and controller don't contain code for generating HTML and likewise the view templates shouldn't interact directly with the model.
在Pylons中每个组件保持独立。请求被派遣一个控制器,这是一个普通的Python类,带有处理应用逻辑的方法(动作)。该控制器与模型类交互以从数据库中提取数据。一旦所有必要的信息收集完,控制器传递关键信息给视图,视图生成数据的HTML页面表示并将之返回到用户的浏览器。然后,用户与页面互动,建立一个新的请求,该过程再次启动。模型和控制器不包含生成HTML的代码,视图模板也不应该与模型直接互动。
This architecture is very useful because it not only reflects what happens in most web applications but it also keeps your application easy to maintain because you always know where the code handling a particular aspect of your application can be found. For example Pylons uses a templating language called Mako to help you generate HTML and recommends the object-relational mapper SQLAlchemy to help you with your model. You'll learn much more about models, controllers and view templates in the following chapters.
这种架构是非常有益的,因为它不仅反映了在大多数web应用中发生的事情,它也使您的应用易于维护,因为你总是知道在哪可以找到处理应用程序某一方面的代码。例如Pylons使用Mako(templating language,模板语言)以帮助您生成HTML,并且推荐SQLAlchemy(ORM,对象关系映射)以帮助您编写模型。在下面的章节,您将了解更多有关模型,控制器和视图的知识。
Caution!
注意!
Those of you coming to Pylons from Django might be accustomed to Django's approach to the MVC architecture, which they refer to as MTV (Model/Template/View). While conceptually quite similar to Pylons' traditional approach to MVC, there are two key terminology differences:
你们那些从Django转到Pylons来的,可能习惯于Django中的MVC架构的方式,他们称之为MTV(模型/模板/视图)。虽然概念与Pylons中传统的MVC方式非常相似,但其中有两个关键术语的差异:
• Django's Template is equivalent to Pylons' View
• Django's View is equivalent to Pylons' Controller
• Django的模板(Template),相当于Pylons的视图(View)
• Django的视图(View)相当于Pylons的控制器(Controller)
The Model is treated similarly in Django and Pylons. There is a discussion of Django's reasons for its terminology on the Django website at
http://tinyurl.com/mnwg9
.
模型(Model)在Django和Pylons做类似处理。在Django网站有一个关于Django的术语命名原因的讨论,网址是
http://tinyurl.com/mnwg9
。
Convention Over Configuration
配置约定
A lot of the complexity of web development can be removed by assuming that the developer wants to do the most obvious thing. For example, almost every time a user requests a page from your site you will want to return a simple HTML page. There may be occasions when you want to return an image or perhaps to stream some custom binary data but most of the time, a simple HTML page is all that's required.
通过假设开发者想干什么最明显的事情,网页开发中大量的复杂性可以被移除。例如,几乎每一次用户从您的网站请求一个页面,你会想返回一个简单的HTML页面。可能有几次当你想返回一张图片或者一些自定义的二进制数据流,但大部分时间,一个简单的HTML页面是所有的需要。
With this in mind the Pylons developers designed Pylons to automatically assume that when you return data it is HTML data unless you specify otherwise. This means that for the common cases you don't have to configure the Content-type because the convention is that it will be text/html unless you want to do things differently.
考虑到这一点,Pylons框架开发者设定,当你返回数据时,Pylons自动假定该数据是HTML数据,除非您另外指定。这就是说,对于一般的情况,您不必配置Content-type(内容类型),因为设定默认是text/html,除非您想要做一些与众不同的事情。
Loose Coupling and Clean Separation
松耦合和清晰分离
Web frameworks such as Django and Ruby on Rails have become extremely popular in recent years because they provide a structure which allows you to quickly create good-looking websites by defining the way the data is structured. The tools they provide then work on that data to either automatically generate code (scaffold in the case of Ruby on Rails) or to create form interfaces at runtime (as is the case with Django).
Web框架,如Django和Ruby on Rails,近年来极受欢迎,因为他们提供了一个结构,可以让您通过定义数据结构化的方式快速创建漂亮的网站。然后他们提供的工具使用这些数据都自动生成代码(如Ruby on Rails)或在运行时创建form接口(如Django)。
Although these frameworks maintain a clean separation between the model, view and controller layers of code, they aren't loosely coupled in the way that Pylons is because the ability of the application as a whole to work relies heavily on the glue code found in the framework itself. Although it can be very easy to write a simple application with these frameworks it can also be harder to customize their behavior later on in a project because doing so frequently involves understanding how the code provided by the framework itself works before you can change its behavior. To make the framework itself easy to use the framework code sometimes has to be quite complex and as a result customization can sometimes be rather difficult.
虽然这些框架,在代码上,模型、视图与控制器层之间保持清晰的隔离,但是它们不是松散耦合的方式,Pylons是,因为使应用作为一个整体工作的能力在很大程度上依赖于框架本身中的胶水代码。虽然可以很容易用这些框架写一个简单的应用,也可以稍后在某个项目中更复杂地定制他们的行为,这样做往往涉及到需要理解框架本身提供的代码如何工作,在改变其行为之前。为了使框架本身易于使用,框架代码有时是相当复杂的,使得定制有时会相当困难。
Pylons on the other hand is much more loosely coupled. Because it doesn't provide tools to automatically generate a nearly-finished site for you from the definition of the model it doesn't need complicated glue code holding everything together. Instead it provides sensible low-level APIs and methodologies which allow you to quickly and easily glue together the component parts you choose to use for yourself.
另一方面,Pylons更松散耦合。因为它没有提供工具,使用定义的模型自动生成近制成品的网站,它并不需要复杂的胶水代码使得一切整合在一起。相反,它提供了合理的低级别的API和一套方法,让您快速,轻松把你选择使用的组件部分粘合起来。
This loosely coupled approach doesn't mean you have to code absolutely everything for yourself either. Pylons uses convention over configuration and assumes that you will want to use a standard setup when you create a new project. If you don't want a standard setup it is easy to customize the way Pylons works. For example by default Pylons uses a templating framework called Mako to help you generate the templates for your views but you are under no obligation to use it. By customizing your Pylons project you can easily use any of the other major Python templating languages including Genshi, TAL, or Breve and all work equally well because Pylons interacts with each through a standard API.
这种松散耦合的方式并不意味着你必须完全地为一切编写代码。Pylons使用约定的配置,并假设,当你创建了一个新的项目,你将要使用标准的设置。如果你不想使用标准的设置,也很容易定制Pylons工作的方式。例如默认情况下,Pylons使用了Mako(模板语言)以帮助您生成视图模板,但你并非一定要使用它。通过自定义您的Pylons项目,您可以轻松地使用任何其他主要的Python模板语言,包括Genshi,TAL,Breve,以及所有同样出色的作品,因为Pylons通过一个标准的API与每个交互。
Other Features
其他特点
In addition to handling a model, views and controllers, modern web frameworks also have to provide tools to facilitate each of the following processes:
除了处理模型,视图和控制器,现代网络框架也提供工具,以帮助下列的每个过程:
• mapping the URL a user visits to the code to be run
• reading data such as form posts that are sent via HTTP
• validation and re-population of forms
• dealing with user accounts
• storing session information, perhaps using a cookie
• client side programming including CSS layouts and AJAX
• 映射用户访问的URL到特定的运行代码
• 读取通过HTTP 发送的form(表单)的数据
• 表单的验证和数据传入/回传(re-population)
• 用户帐户处理
• session信息存储,或cookie使用
• 客户端编程,包括CSS布局和AJAX
They also often provide:
他们还经常提供:
• A server component to run the application
• Automatic documentation generation tools
• Systems for packaging, distribution and deployment
• Testing tools
• Internationalisation tools and Unicode support
• Interactive debugging tools
• Logging facilities
• Any number of other useful features
• 运行应用程序的服务器组件
• 文档自动生成工具
• 系统的打包,发布和部署
• 测试工具
• 国际化的工具和Unicode支持
• 交互式调试工具
• 日志管理
• 大量其他有用的功能
Pylons is no exception and provides tools and methodologies for handling all of these things. You'll learn about each of them during the course of the book.
Pylons也不例外,提供工具和一套方法来处理所有这些事情。您可以在这本书了解它们每一个。
原文: http://hi.baidu.com/zhuifengdenanhai/blog/item/06b1b738c8c14bc5d4622553.html
Zeuux © 2024
京ICP备05028076号