艾伟_转载:[一步一步MVC]第六回:什么是MVC(上)?

本文深入解析MVC架构的核心概念,包括M、V、C及Routing的作用,详细阐述MVC的执行过程,并与传统的ASP.NET WebForm应用进行对比,突出MVC在简化复杂架构、实现有效隔离、支持测试与扩展等方面的优点。

本系列文章导航

[一步一步MVC]第一回:使用ActionSelector控制Action的选择

[一步一步MVC]第二回:还是ActionFilter,实现对业务逻辑的统一Authorize处理

[一步一步MVC]第三回:MVC范例大观园

[一步一步MVC]第四回:漫谈ActionLink,有时“胡搅蛮缠”

[一步一步MVC]第五回:让TagBuilder丰富你的HtmlHelper

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

引言

所谓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这个话题,还有很多东西,我们下回再见吧。

参考文献

转载于:https://www.cnblogs.com/waw/archive/2011/08/29/2157013.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值