ASP.NET MVC Overview
MVC的建筑模式分离为三个主要主键,Model、View和Controller,ASP.NET的MVC框架提供一种替代Web Forms创建Web程序的方式,MVC框架是一个轻量级、高可用性集合了ASP.NET特征的演示框架(类似于Web Forms),如Master Page和会员身份验证。MVC框架依赖于System.Web.Mvc程序集。
MVC is a standard design pattern that many developers are familiar with. Some types of Web applications will benefit from the MVC framework. Others will continue to use the traditional ASP.NET application pattern that is based on Web Forms and postbacks. Other types of Web applications will combine the two approaches; neither approach excludes the other.
MVC的标准设计模式能够对一部分使用Web程序的开发组织带来帮助,而其他人将继续使用依赖于Web Forms和PostBacks的传统ASP.NET程序,或者两则都使用,或者两则都不使用。
The MVC framework includes the following components:
· Models. Model objects are the parts of the application that implement the logic for the application's data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in a SQL Server database.
· 模型:模型对象是程序的逻辑数据层,模型对象用来检索和存储数据库中的数据对象。例如:一个产品数据对象需要从数据库检索、处理信息,并把信息写回到SQL数据库中。
In small applications, the model is often a conceptual separation instead of a physical one. For example, if the application only reads a dataset and sends it to the view, the application does not have a physical model layer and associated classes. In that case, the dataset takes on the role of a model object.
一个小的程序,模型经常只是一种分离物理数据(真实存在的数据)的假象,例如;一个程序仅仅是读取一个DataSet并发送到视图中,这个程序本省没有物理数据和相关类,事实上,数据只存在于模型对象中。
· Views. Views are the components that display the application's user interface (UI). Typically, this UI is created from the model data. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Product object.
· 视图:视图是显示程序UI的组件,这个UI从模型数据中创建,一个例子编辑产品数据,用视图把产品对象(保存在视图中的产品数据模型)显示在Text Boxes、Drop-Down Lists和Check Boxes中。
· Controllers. Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction. For example, the controller handles query-string values, and passes these values to the model, which in turn might use these values to query the database.
· 控制器:控制器是控制用户路由、处理模式和最终选择视图显示在用户UI上,在一个MVC程序里,视图显示信息,控制器控制用户的GET和POST路由。例如:控制器得到一个Query-String的值,用这个值到模式中查询数据库,填充到视图中,之后路由到相关视图的显示页面。
The MVC pattern helps you create applications that separate the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The pattern specifies where each kind of logic should be located in the application. The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps you manage complexity when you build an application, because it enables you to focus on one aspect of the implementation at a time. For example, you can focus on the view without depending on the business logic.
MVC模式帮助你容易的创建分层的应用程序(Input Logic、Business Logic和UI Logic),并提供这些元素之间的松耦合连接。这种模式帮助你区分程序中的不同逻辑,UI Logic是View(视图),Input Logic是Controller(控制器),Business Logic是Model(模型),这种分离帮助你管理复杂的应用程序,因为你可以把实施时间专注于自己擅长的某一方面,例如你可以专注于View(视图),而不用了解Model(模型)。
The loose coupling between the three main components of an MVC application also promotes parallel development. For example, one developer can work on the view, a second developer can work on the controller logic, and a third developer can focus on the business logic in the model.
MVC三个主要组件的松耦合连接使你可以在三个组件之间平行开发,例如:one developer can work on the view, a second developer can work on the controller logic, and a third developer can focus on the business logic in the model.(作者:问题是Business Logic可以没头没脑的开发,Input Logic和UI Logic如何不了解Business Logic那么他们还是基本上会有一大段的空闲时间)
Support for Test-Driven Development支持测试驱动开发
In addition to managing complexity, the MVC pattern makes it easier to test applications than it is to test a Web Forms-based ASP.NET Web application. For example, in a Web Forms-based ASP.NET Web application, a single class is used both to display output and to respond to user input. Writing automated tests for Web Forms-based ASP.NET applications can be complex, because to test an individual page, you must instantiate the page class, all its child controls, and additional dependent classes in the application. Because so many classes are instantiated to run the page, it can be hard to write tests that focus exclusively on individual parts of the application. Tests for Web Forms-based ASP.NET applications can therefore be more difficult to implement than tests in an MVC application. Moreover, tests in a Web Forms-based ASP.NET application require a Web server. The MVC framework decouples the components and makes heavy use of interfaces, which makes it possible to test individual components in isolation from the rest of the framework.
When to Create an MVC Application
You must consider carefully whether to implement a Web application by using either the ASP.NET MVC framework or the ASP.NET Web Forms model. The MVC framework does not replace the Web Forms model; you can use either framework for Web applications. (If you have existing Web Forms-based applications, these continue to work exactly as they always have.)
Before you decide to use the MVC framework or the Web Forms model for a specific Web site, weigh the advantages of each approach.
何时创建MVC
你必须慎重的考虑你的Web程序是使用MVC还是Web Forms,MVC不是用来替代Web Forms,而且如果你高兴的话还可以使用其他的方法来创建Web。就像是台式机和手提,两个没有不会死,但是有了台式你不会扔掉手提,反之。他们其实都不重要,重要的是钱,其实钱也不重要,重要的是自由,其实自由也不重要,重要的是你还活着。
Advantages of an MVC-Based Web Application
The ASP.NET MVC framework offers the following advantages:
· It makes it easier to manage complexity by dividing an application into the model, the view, and the controller.
· It does not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application.
· It uses a Front Controller pattern that processes Web application requests through a single controller. This enables you to design an application that supports a rich routing infrastructure. For more information, see Front Controller.
· It provides better support for test-driven development (TDD).
· It works well for Web applications that are supported by large teams of developers and for Web designers who need a high degree of control over the application behavior.
MVC的优势
有利于管理使用三层模型,复杂的应用程序。
不使用view state或server-based forms,可以满足程序员完全控制欲。
使用Front Controller(前端路由),完美的路由控制,这个等你看了一些相关文章后你会发现,世界是如此美好,你的工作量会大大减少,生命大大延长。
它提供了更好的支持测试驱动开发( TDD的) 。
方便大型团队开发,满足网页设计师(UI)的设计欲。
Advantages of a Web Forms-Based Web Application
The Web Forms-based framework offers the following advantages:
· It supports an event model that preserves state over HTTP, which benefits line-of-business Web application development. The Web Forms-based application provides dozens of events that are supported in hundreds of server controls.
· It uses a Page Controller pattern that adds functionality to individual pages. For more information, see Page Controller.
· It uses view state on server-based forms, which can make managing state information easier.
· It works well for small teams of Web developers and designers who want to take advantage of the large number of components available for rapid application development.
· In general, it is less complex for application development, because the components (the Page class, controls, and so on) are tightly integrated and usually require less code than the MVC model.
Web Forms的优势
支持Event Model和Http视图状态保持,有利于企业生产线的Web应用开发(作者:像VB一样开发程序,如果你的程序只是某一个方面小型的开发,那么Web Forms是最好的),提供大量事件支持和服务器控件。
使用Page Controller,更多信息请访问Page Controller。
很多爱好者目前已经开发了大量的,好用的控件。
Features of the ASP.NET MVC Framework
The ASP.NET MVC framework provides the following features:
· Separation of application tasks (input logic, business logic, and UI logic), testability, and test-driven development (TDD). All core contracts in the MVC framework are interface-based and can be tested by using mock objects, which are simulated objects that imitate the behavior of actual objects in the application. You can unit-test the application without having to run the controllers in an ASP.NET process, which makes unit testing fast and flexible. You can use any unit-testing framework that is compatible with the .NET Framework.
· An extensible and pluggable framework. The components of the ASP.NET MVC framework are designed so that they can be easily replaced or customized. You can plug in your own view engine, URL routing policy, action-method parameter serialization, and other components. The ASP.NET MVC framework also supports the use of Dependency Injection (DI) and Inversion of Control (IOC) container models. DI enables you to inject objects into a class, instead of relying on the class to create the object itself. IOC specifies that if an object requires another object, the first objects should get the second object from an outside source such as a configuration file. This makes testing easier.
· Extensive support for ASP.NET routing, which is a powerful URL-mapping component that lets you build applications that have comprehensible and searchable URLs. URLs do not have to include file-name extensions, and are designed to support URL naming patterns that work well for search engine optimization (SEO) and representational state transfer (REST) addressing.
· Support for using the markup in existing ASP.NET page (.aspx files), user control (.ascx files), and master page (.master files) markup files as view templates. You can use existing ASP.NET features with the ASP.NET MVC framework, such as nested master pages, in-line expressions (<%= %>), declarative server controls, templates, data-binding, localization, and so on.
· Support for existing ASP.NET features. ASP.NET MVC lets you use features such as forms authentication and Windows authentication, URL authorization, membership and roles, output and data caching, session and profile state management, health monitoring, the configuration system, and the provider architecture.
MVC框架的未来(如果看不懂,请看英文,因为我也看不懂,我的英文很差)
逻辑分层。TTD测试驱动开发,只要是TTD的优点。所有的核心工作流都可以使用基础接口使用虚拟对象(模仿实际运用)进行测试,使用任何符合.NET框架的测试单元测试程序,并且不需要运行在IIS进程中。
可扩展和热插拔的框架,MVC的所有组件都是可自定义,你可以开发自己的组件,像view engine(View控件), URL routing policy(控制器), action-method parameter serialization, and other components。支持Dependency Injection (DI,依赖注入) and Inversion of Control (IOC,反向控制)容器(敏捷设计原则吗?依赖注入->数据工厂?反向控制->智能程序)
强大的路由支持和URL映射,网址不需要包括扩展名,有利于SEO优化和象征意义的地址,类似于象形文字的象形地址。
支持现有.NET页面(aspx、ascx和master),并且在页面顶部加上<%= %>,就可以变成了Web Forms页面。
支持现有的ASP.NET功能。 身份验证Forms和Windows,Url授权,用户成员和Roles,(以上应该都是身份验证的内容),output和缓存,Session和profile state management, health monitoring,configuration(web.config)和the provider architecture。
浙公网安备 33010602011771号