第三章 MVC模式
内容:
- MVC模式含义
- 和其他模式的比较
- ASP.NET Core 的项目习惯
1. MVC模式
- Models 用户业务逻辑和实体模型
- Views 显示数据
- Controllers 处理请求
1.1 Model
Model有2大类: ViewModel 仅仅表示从Controller传递给View的显示内容; Domain Model:包括业务领域的对象和逻辑操作
MVC中的model应该:
- 包括领域数据
- 包含创建、处理、修改领域数据的逻辑
- 提供一个简介的API来访问这些操作
MVC中的model不应该:
- 暴露任何 数据是如何管理的内部细节
- 包含根据用户交互内容来转换model的逻辑(这应该交给controller)
- 包含任何怎样给用户呈现的方式 (这应该交给View)
MVC的拆分并不代表着model就仅仅是数据对象,而不包括逻辑。 MVC实际上是把应用程序分成了三个不同部分,每个部分包含其应该负责的逻辑和数据,比如Model只应该包含处理数据的逻辑,而不是显示数据或者相应用户行为的逻辑。
1.2 Controller
包含action负责和model数据交互以及提供结果给View。
MVC中的controller应该:
- 包含能够针对用户交互来更新model的action
MVC中的controller不应该:
- 包含管理数据如何呈现的逻辑 (应该给View)
- 包含如何管理持久化数据的逻辑(应该给model)
1.3 View
包含展示数据给用户的逻辑,以及从用户处获取数据,以便让controller处理的逻辑。
View应该:
- 包含展示数据给用户的逻辑和标记
View不应该:
- 包含复杂的逻辑(这最好给controller)
- 包含创建、存储、管理domain model的逻辑
2. ASP.NET Core的MVC
controller是C#类, 通常继承自 Microsoft.AspNetCore.Mvc.Controller ,每个public方法都是一个action,对应着一个访问Url。Action负责处理请求,然后返回一个合适的结果。
ASP.NET Core MVC 使用 Razor View Engine 视图引擎,用来更方便的呈现View,渲染处对应的HTML。
3. 和其他模式的比较
3.1 SmartUI
Smart user interface(Smart UI)是一个非常常见的模式,大多数C#开发人员都编写过——Winform或者 ASP.NET Web Form。开发人员通过拖拽控件来构建UI,而控件通过一系列的event来相应用户操作。开发人员自己编写一系列的handler来处理对应逻辑。
这最终导致一个庞大的应用——UI页面操作和业务逻辑全部混杂在了一起。
SmartUI比较适合小型应用以及构建项目模型。
而缺点是难以扩展和维护。很难避免同样逻辑要写很多遍,而且要进行单元测试也很困难。
3.2 Model-View
SmartUI的一种改进,将所有业务逻辑抽离出来,和处理View的逻辑分离。
优点是测试性和扩展性有了一定提升。缺点是仍然不是很容易进行单元测试,因为View和Model的逻辑还是很紧密。并且操作数据持久化的逻辑和业务逻辑都写在一起。
3.3 经典三层
在之前的基础上,把business logic和data persistent 分离开。
使用范围非常广泛的一种模式。和MVC的区别在于,三层是一个项目整体的模式,并没有规定使用的是哪种View,如果当View仍然和处理逻辑结合很紧密的时候,还是不太好进行单元测试。并且UI部分如果非常复杂,那么会有大量代码在UI层; 而MVC仅仅是在UI层上的模式,提供了一种在UI层上怎样进行功能分离的思想。
3.4 Model-View-Presenter(MVP)
一种MVC的变形,用来更好的处理带有状态的GUI应用,比如Winform和ASP.NET Web Form。 Presenter和controller的功能很类似,但是它会更加直接的管理Stateful view,根据用户的输入和行为来管理什么data应该显示给View。
3.5 Model-View-View Model(MVVM)
微软提出,在WPF中先使用的模型。区别在与ViewModel,它不仅包含了View中显示的数据内容,还包含了View中的所有状态和操作逻辑。它并不知道View到底是什么,比如显示一个列表到底使用DropdownBox还是一系列的text,这些信息对于ViewModel是透明的,它管理的仅仅是数据和页面状态, 使用和View双向绑定来暴露它内部的操作和数据。
4. ASP.NET Core MVC Projects
创建项目的时候,VisualStudio会提供一系列的模板,这些都只是它根据你的选择给你推荐的文件,这些都是可以不需要的。作者习惯使用 Empty来创建项目,之后自己构建,以此来了解项目结构。
4.1 文件目录
Folder or File |
Description |
/Areas |
Areas are a way of partitioning a large application into smaller pieces.I describe areas in Chapter 16. |
/Dependencies |
The Dependencies item provides details of all the packages a project relies on. I describe the package managers that Visual Studio uses in Chapter 6. |
/Components |
This is where view component classes, which are used to display selfcontained features such as shopping carts, are defined. I describe view components in Chapter 22. |
/Controllers |
This is where you put your controller classes. This is a convention. You can put your controller classes anywhere you like because they are all compiled into the same assembly. I describe controllers in detail in Chapter 17. |
/Data |
This is where database context classes are defined, although I prefer to ignore this convention and define them in the Models folder, as demonstrated in Chapter 8. |
/Data/Migrations |
This is where Entity Framework Core migrations are stored so that databases can be prepared to store the application data. I use migrations in Chapters 8, 9, 10, and 11 as part of the SportsStore project. |
/Models |
This is where you put your view model and domain model classes. This is a convention. You can define your model classes anywhere in the project or in a separate project. |
/Views |
This directory holds views and partial views, usually grouped together in folders named after the controller with which they are associated. I describe views in detail in Chapter 21. |
/Views/Shared |
This directory holds layouts and views that are not specific to a single controller. I describe views in detail in Chapter 21. |
/Views/_ViewImports.cshtml |
This file is used to specify the namespaces that will be included in Razor view files, as described in Chapter 5. It is also used to set up tag helpers, as described in Chapter 23. |
/Views/_ViewStart.cshtml |
This file is used to specify a default layout for the Razor view engine, as described in Chapter 5. |
/appsettings.json |
This file contains configuration settings that can be tailored for different environments, such as development, testing, and production. The most common uses for this file are to define database server connection strings and logging/debug settings, which I describe in Chapter 14. |
/bower.json |
This file contains the list of packages managed by the Bower package manager, as described in Chapter 6. |
/.csproj |
This file contains the configuration for the project, including the NuGet packages that the application requires, as described in Chapters 6 and 14. This file is hidden and can be edited only by right-clicking the project item in the Solution Explorer window and selecting the Edit <project>.csproj menu item. |
/Program.cs |
This class configures the hosting platform for the application, as described in Chapter 14. |
/Startup.cs |
This class configures the application, as described in Chapter 14. |
/wwwroot |
This is where you put static content such as CSS files and images. It is also where the Bower package manager installs JavaScript and CSS packages, as described in Chapter 6. |
4.2 Convention
- 第三方Js和Css放在 wwwroot/lib 文件夹中
- Controller的类名都以
Controller 结尾
- View放在文件夹
/Views/Controllername 下。Action默认的View和Action的名字相同:例如List 默认的View应该叫List.cshtml
- Layout文件以
_ 开头,且放在/Views/Shared 中
|
请发表评论