Model/Business Layer/Data Access 和 Repositories 在 MVC 架构中有什么区别?
What is the different between Model/Business Layer/Data Access and Repositories in the MVC architecture?
我想通过声明我是 .NET Framework 的新手来开始我的问题,并且完全 ASP.NET。但是,我正在尝试学习 ASP.NET 5 MVC 6。我已经阅读了很多教程来帮助我加快速度。我从中学到很多的主要教程是“Learn MVC in 7 Days”
我想我总体上了解了 MVC
体系结构,但是有一些 terminology/layers 让我感到困惑,即模型、业务逻辑层、数据访问层和视图模型。
这里是我对MVC架构的整体理解"Please correct me if wrong"
- (M) 模型:是表示数据库的对象 table。数据库中的每个 table 都是一个模型。每个 table 中的每一列都是模型对象中的一个属性。例如,如果我有一个名为 "users" 的 table,其中包含以下列
id
、firstname
、lastname
和 username
,那么模型将是称为 user
和“id
、firstname
、lastname
和 username
”是属性。
- (V) 视图:是通过将数据放入 HTML 页面来向最终用户呈现数据的方式。
- (C)Controller:是路由引擎会调用的层。控制器 class 对用户应该看到的内容 data/views 持有一些逻辑。例如,
UsersController
class 有一个名为 Index()
的方法,它从 user
模型请求一些数据,然后 returns 一个名为 ShowAllUsers
的视图。
不过,Model下面好像还有3层像这样
- 查看模型:这似乎是一种将来自模型的原始数据转换为现有table "view ready" 格式的方法。例如,如果我们想向最终用户显示用户的全名,但我们没有将全名作为模型中的属性。然后我们这一层将创建一个新对象,它与模型对象相同,具有一个名为 fullname 的附加虚拟属性。因此,我们现在可以在视图中显示obj.fullname。
- 业务逻辑层
- 数据访问层
此外,如果我想为我的控制器配备一个 repository,它应该放在哪里?我知道这对于小型应用程序可能不是必需的,但我只是想了解和学习正确的方法,然后我可以决定我的应用程序是否需要它。
我的问题是什么是业务逻辑层,什么是数据访问层? repository 放在哪里?
我将不胜感激一个例子的解释。
以下片段摘自 MSDN 上的一些教程,您可能会觉得它们有用:
When working with data one option is to embed the data-specific logic directly into the presentation layer (in a web application, the ASP.NET pages make up the presentation layer). This may take the form of writing ADO.NET code in the ASP.NET page's code portion or using the SqlDataSource control from the markup portion. In either case, this approach tightly couples the data access logic with the presentation layer. The recommended approach, however, is to separate the data access logic from the presentation layer. This separate layer is referred to as the Data Access Layer
The Data Access Layer (DAL) created in the first tutorial cleanly separates the data access logic from the presentation logic. However, while the DAL cleanly separates the data access details from the presentation layer, it does not enforce any business rules that may apply. For example, for our application we may want to disallow the CategoryID or SupplierID fields of the Products table to be modified when the Discontinued field is set to 1, or we might want to enforce seniority rules, prohibiting situations in which an employee is managed by someone who was hired after them. Another common scenario is authorization – perhaps only users in a particular role can delete products or can change the UnitPrice value.
In this tutorial we'll see how to centralize these business rules into a Business Logic Layer (BLL) that serves as an intermediary for data exchange between the presentation layer and the DAL.
从 Code Project article 中,我发现对数据访问层用途的描述特别有用:
The data access layer provides a centralized location for all calls into the database, and thus makes it easier to port the application to other database systems.
我还找到了 this blog post,它很好地说明了业务逻辑层如何与存储库集成:
最后,这里是微软对business logic的定义:
Business logic is defined as any application logic that is concerned with the retrieval, processing, transformation, and management of application data; application of business rules and policies; and ensuring data consistency and validity. To maximize reuse opportunities, business logic components should not contain any behavior or application logic that is specific to a use case or user story.
我希望我能提供我自己对这些层的专家描述,但我也在学习这个主题,所以我想我会分享我发现的东西,希望我们都能从中学到一些东西.例如与这些层相关的问题,请参考我上面链接的教程。
MVC 只是关于表现层的。不要仅仅因为它包含单词 Model
就将它与业务和数据层的解决方案混淆
首先让我们谈谈分层架构是如何工作的:
这个想法很简单。顶层依赖于下面的层。因此,表示层依赖于业务层,业务层依赖于数据层。也就是说,Business层定义业务逻辑并为Presentation层提供接口,Data层定义数据访问逻辑并为Business层提供接口
现在让我们谈谈什么是MVC:
- V(View) - UI 逻辑存在的地方。
- M(Model) - 是业务逻辑层的接口。理解很重要
它本身不是业务逻辑,它只是接口。这就是为什么
我们说表示层依赖于业务层 -
因为它使用它的接口
- C(Controller) - 这是你调用业务层接口的地方
将接收到的数据映射到视图。
同时检查这些答案:
- How should a model be structured in MVC?
- Architecture more suitable for web apps than MVC?
我想通过声明我是 .NET Framework 的新手来开始我的问题,并且完全 ASP.NET。但是,我正在尝试学习 ASP.NET 5 MVC 6。我已经阅读了很多教程来帮助我加快速度。我从中学到很多的主要教程是“Learn MVC in 7 Days”
我想我总体上了解了 MVC
体系结构,但是有一些 terminology/layers 让我感到困惑,即模型、业务逻辑层、数据访问层和视图模型。
这里是我对MVC架构的整体理解"Please correct me if wrong"
- (M) 模型:是表示数据库的对象 table。数据库中的每个 table 都是一个模型。每个 table 中的每一列都是模型对象中的一个属性。例如,如果我有一个名为 "users" 的 table,其中包含以下列
id
、firstname
、lastname
和username
,那么模型将是称为user
和“id
、firstname
、lastname
和username
”是属性。 - (V) 视图:是通过将数据放入 HTML 页面来向最终用户呈现数据的方式。
- (C)Controller:是路由引擎会调用的层。控制器 class 对用户应该看到的内容 data/views 持有一些逻辑。例如,
UsersController
class 有一个名为Index()
的方法,它从user
模型请求一些数据,然后 returns 一个名为ShowAllUsers
的视图。
不过,Model下面好像还有3层像这样
- 查看模型:这似乎是一种将来自模型的原始数据转换为现有table "view ready" 格式的方法。例如,如果我们想向最终用户显示用户的全名,但我们没有将全名作为模型中的属性。然后我们这一层将创建一个新对象,它与模型对象相同,具有一个名为 fullname 的附加虚拟属性。因此,我们现在可以在视图中显示obj.fullname。
- 业务逻辑层
- 数据访问层
此外,如果我想为我的控制器配备一个 repository,它应该放在哪里?我知道这对于小型应用程序可能不是必需的,但我只是想了解和学习正确的方法,然后我可以决定我的应用程序是否需要它。
我的问题是什么是业务逻辑层,什么是数据访问层? repository 放在哪里?
我将不胜感激一个例子的解释。
以下片段摘自 MSDN 上的一些教程,您可能会觉得它们有用:
When working with data one option is to embed the data-specific logic directly into the presentation layer (in a web application, the ASP.NET pages make up the presentation layer). This may take the form of writing ADO.NET code in the ASP.NET page's code portion or using the SqlDataSource control from the markup portion. In either case, this approach tightly couples the data access logic with the presentation layer. The recommended approach, however, is to separate the data access logic from the presentation layer. This separate layer is referred to as the Data Access Layer
The Data Access Layer (DAL) created in the first tutorial cleanly separates the data access logic from the presentation logic. However, while the DAL cleanly separates the data access details from the presentation layer, it does not enforce any business rules that may apply. For example, for our application we may want to disallow the CategoryID or SupplierID fields of the Products table to be modified when the Discontinued field is set to 1, or we might want to enforce seniority rules, prohibiting situations in which an employee is managed by someone who was hired after them. Another common scenario is authorization – perhaps only users in a particular role can delete products or can change the UnitPrice value. In this tutorial we'll see how to centralize these business rules into a Business Logic Layer (BLL) that serves as an intermediary for data exchange between the presentation layer and the DAL.
从 Code Project article 中,我发现对数据访问层用途的描述特别有用:
The data access layer provides a centralized location for all calls into the database, and thus makes it easier to port the application to other database systems.
我还找到了 this blog post,它很好地说明了业务逻辑层如何与存储库集成:
最后,这里是微软对business logic的定义:
Business logic is defined as any application logic that is concerned with the retrieval, processing, transformation, and management of application data; application of business rules and policies; and ensuring data consistency and validity. To maximize reuse opportunities, business logic components should not contain any behavior or application logic that is specific to a use case or user story.
我希望我能提供我自己对这些层的专家描述,但我也在学习这个主题,所以我想我会分享我发现的东西,希望我们都能从中学到一些东西.例如与这些层相关的问题,请参考我上面链接的教程。
MVC 只是关于表现层的。不要仅仅因为它包含单词 Model
就将它与业务和数据层的解决方案混淆首先让我们谈谈分层架构是如何工作的:
这个想法很简单。顶层依赖于下面的层。因此,表示层依赖于业务层,业务层依赖于数据层。也就是说,Business层定义业务逻辑并为Presentation层提供接口,Data层定义数据访问逻辑并为Business层提供接口
现在让我们谈谈什么是MVC:
- V(View) - UI 逻辑存在的地方。
- M(Model) - 是业务逻辑层的接口。理解很重要 它本身不是业务逻辑,它只是接口。这就是为什么 我们说表示层依赖于业务层 - 因为它使用它的接口
- C(Controller) - 这是你调用业务层接口的地方 将接收到的数据映射到视图。
同时检查这些答案:
- How should a model be structured in MVC?
- Architecture more suitable for web apps than MVC?