ASP.NET MVC 實作 MVC 的架構,並區分至少 MVC 三層的職責,並可依需求擴增

關注點分離(Separation of Concerns,SOC)是 AOP(Aspect-Oriented Programming)內的概念,也是 ASP.NET 開發的重要原則

Model

Model 的職責

  • database 的存取機操作(CRUD)
  • 資料結構的定義
  • 資料格式的驗證

常用的技術

  • Entity Framework
  • NHibernate
  • LINQ to SQL
  • 強型別的 DataSet (Typed DataSet)
  • ADO.NET

View 的職責

  • 顯示的頁面
  • 頁面回傳後端的部分
  • 畫面的顯示及互動邏輯

Controller 的職責

是一種協調(coordinator)的角色,作為 View 與 Model 之間的溝通橋樑:

stateDiagram direction LR Client --> Controller: Request Controller --> Model: 資料交換 Model --> Controller Controller --> View View --> Controller View --> Client: Response

MVC 的生命週期

sequenceDiagram 瀏覽器 ->> 網址路由: 要求 網址路由 ->> Controller: 實體化 網址路由 ->> Controller: 執行 Action Controller ->> Model: 執行 Method Controller ->> Controller: 檢查 View Controller ->> View: 提供資料 View ->> 瀏覽器: 回傳頁面(HTML)