[一步一步MVC]第六回:什么是MVC(上)?

anytao.net | 《你必须知道的.NET》网站 | Anytao技术博客 

发布日期:2009.05.10 作者:Anytao
© 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处。

/// <summary>
/// 本文为MVC introduction的第一部分。
/// </summary>

引言

所谓MVC,其实就是M、V、C而已。归根揭底,MVC是一种表现模式,是一种软件架构模式。其中有几个重要的概念:

  • M,Model, 引用系统数据,管理系统功能并通知View更改用户操作。
  • V,View,就是用户接口,用于显示数据。
  • C,Controller ,将用户操作映射到Model,并操作视图。
  • R,Routing ,MVC的奥秘在于通过Routing实现了对URL的路由选择,完成了上述3个基本概念的基础逻辑。

我们先来了解这几个概念之间的联系。

 

o_anytao-mvc-09-01[1]

对MVC而言,分离是最大的优点,尤其是Model将不依赖于Controller和View,对于隔离应用、进行UI测试打下很好的架构级支持。

MVC Execution Process

关于MVC的执行过程,我们就不多言了,从MSDN获取的执行过程可以被解析为:

o_anytao-mvc-09-02[1] 

 

在MVC模式下,不同于WebForm时代,业务逻辑的处理和HTML的输出不是View(或Page)一个人的事儿,这些逻辑被清晰的分解为M、V和C的逻辑,具体的执行流程为:

ASP.NET MVC Execution Process

Stage

Details

Receive first request for the application

In the Global.asax file, Route objects are added to the RouteTable object.

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}
 
public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new Route
    (
         "Category/{action}/{categoryName}"
         , new CategoryRouteHandler()
    ));
}

Perform routing

The UrlRoutingModule module uses the first matching Route object in the RouteTable collection to create the RouteData object, which it then uses to create a RequestContext object.

 

Create MVC request handler

The MvcRouteHandler object creates an instance of the MvcHandler class and passes the RequestContext instance to the handler.

Create controller

The MvcHandler object uses the RequestContext instance to identify the IControllerFactory object (typically an instance of the DefaultControllerFactory class) to create the controller instance with.

Execute controller

The MvcHandler instance calls the controller's Execute method.

Invoke action

For controllers that inherit from the ControllerBase class, the ControllerActionInvoker object that is associated with the controller determines which action method of the controller class to call, and then calls that method.

Execute result

The action method receives user input, prepares the appropriate response data, and then executes the result by returning a result type. The built-in result types that can be executed include the following: ViewResult (which renders a view and is the most-often used result type), RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.

我就不翻译了,反正很好理解。

 

优点和优点

 

我们不说废话,先对MVC与传统ASP .NET WebForm application进行一番比较。

MVC

  • 通过model、view和controller有效的简化了复杂的架构,体现了很好的隔离原则。
  • 一切皆可测试。
  • 一切皆可扩展:ViewEngine、HtmlHelper还有Filter。
  • 适用于大型架构开发。
  • 强类型View实现,更安全、更可靠、更高效。
  • 开源,意味着更好的控制和理解。
  • 没有View State,没有Server Control,这显然是个好处。

 

WebForm

  • 支持事件驱动模型开发,拖拽即可形成应用,简单易懂。
  • 可以应用MVP模型,为每个page添加功能。
  • 应用ViewState和Server Control,显然也是优点。
  • 适用于快速开发。

 

本文仅仅是个简单的介绍,关于什么是MVC这个话题,还有很多东西,我们下回再见吧。

 

参考文献

 

相关导航

 

更多精彩,尽在anytao.net

 

anytao | © 2009 Anytao.com

2009/05/10 | http://anytao.cnblogs.com/ | http://anytao.net/blog/post/2009/05/10/anytao-mvc-09-introductionmvc-part1.aspx

本文以“现状”提供且没有任何担保,同时也没有授予任何权利。 | This posting is provided "AS IS" with no warranties, and confers no rights.

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted @ 2009-05-10 17:20  Anytao  阅读(10715)  评论(20编辑  收藏  举报