• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

读书笔记:Pro ASP.NET Core MVC 2 [Chap3 MVC模式, 项目惯例]

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

第三章 MVC模式

内容:

  1. MVC模式含义
  2. 和其他模式的比较
  3. ASP.NET Core 的项目习惯

1. MVC模式

  • Models 用户业务逻辑和实体模型
  • Views 显示数据
  • Controllers 处理请求

1.1 Model

Model有2大类: ViewModel 仅仅表示从Controller传递给View的显示内容; Domain Model:包括业务领域的对象和逻辑操作

MVC中的model应该:

  1. 包括领域数据
  2. 包含创建、处理、修改领域数据的逻辑
  3. 提供一个简介的API来访问这些操作

MVC中的model不应该:

  1. 暴露任何 数据是如何管理的内部细节
  2. 包含根据用户交互内容来转换model的逻辑(这应该交给controller)
  3. 包含任何怎样给用户呈现的方式 (这应该交给View)

MVC的拆分并不代表着model就仅仅是数据对象,而不包括逻辑。 MVC实际上是把应用程序分成了三个不同部分,每个部分包含其应该负责的逻辑和数据,比如Model只应该包含处理数据的逻辑,而不是显示数据或者相应用户行为的逻辑。

1.2 Controller

包含action负责和model数据交互以及提供结果给View。

MVC中的controller应该:

  1. 包含能够针对用户交互来更新model的action

MVC中的controller不应该:

  1. 包含管理数据如何呈现的逻辑 (应该给View)
  2. 包含如何管理持久化数据的逻辑(应该给model)

1.3 View

包含展示数据给用户的逻辑,以及从用户处获取数据,以便让controller处理的逻辑。

View应该:

  1. 包含展示数据给用户的逻辑和标记

View不应该:

  1. 包含复杂的逻辑(这最好给controller)
  2. 包含创建、存储、管理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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.NetAjaxInAction图书源码发布时间:2022-07-10
下一篇:
asp.netcore2.0Microsoft.Extensions.Logging文本文件日志扩展发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap