zoukankan      html  css  js  c++  java
  • 什么是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这个话题,还有很多东西,我们下回再见吧。

  • 相关阅读:
    HDU 1317 XYZZY(floyd+bellman_ford判环)
    UVa 10791 最小公倍数的最小和(唯一分解定理)
    UVa 12169 不爽的裁判
    UVa 11582 巨大的斐波那契数!(幂取模)
    POJ 1142 Smith Numbers(分治法+质因数分解)
    HDU 1595 find the longest of the shortest
    UVa 11090 在环中
    UVa 10917 林中漫步
    UVa 11374 机场快线
    POJ 1503 Integer Inquiry
  • 原文地址:https://www.cnblogs.com/lvcha/p/1714259.html
Copyright © 2011-2022 走看看