作者:
Anytao 來源:
博客園 發布時間: 2010-01-13 11:40 閱讀: 2314 次 推薦: 0
原文鏈接 [收藏]
系列文章導航:
[一步一步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] o_anytao-mvc-09-01[1]](https://imageproxy.pixnet.cc/imgproxy?url=https://images.cnblogs.com/cnblogs_com/anytao/186609/o_anytao-mvc-09-01.gif&width=244&height=124)
對MVC而言,分離是最大的優點,尤其是Model將不依賴于Controller和View,對于隔離應用、進行UI測試打下很好的架構級支持。
MVC Execution Process
關于MVC的執行過程,我們就不多言了,從MSDN獲取的執行過程可以被解析為:
![o_anytao-mvc-09-02[1] o_anytao-mvc-09-02[1]](https://imageproxy.pixnet.cc/imgproxy?url=https://images.cnblogs.com/cnblogs_com/anytao/186609/o_anytao-mvc-09-02.gif&width=260&height=325)
在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這個話題,還有很多東西,我們下回再見吧。
參考文獻
文章列表