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"

不过,Model下面好像还有3层像这样

此外,如果我想为我的控制器配备一个 repository,它应该放在哪里?我知道这对于小型应用程序可能不是必需的,但我只是想了解和学习正确的方法,然后我可以决定我的应用程序是否需要它。

我的问题是什么是业务逻辑层,什么是数据访问层? repository 放在哪里?

我将不胜感激一个例子的解释。

以下片段摘自 MSDN 上的一些教程,您可能会觉得它们有用:

Data Access Layer

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

Business Logic 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?