简体   繁体   English

我应该在分层架构中的什么地方放置实体框架查询?

[英]Where do I place the entity framework queries in layered architecture?

I have .net core api project with following folders:我有 .net 核心 api 项目,其中包含以下文件夹:

Models contains the database table classes and dbcontext模型包含数据库表类和 dbcontext

Services contain logic to send email, and business logic (example calculate student grade based on marks)服务包含发送 email 的逻辑和业务逻辑(例如根据分数计算学生成绩)

Controller contains the controllers with respective actions (api endpoints). Controller 包含具有相应操作(api 端点)的控制器。 The dbcontext is injected into the controller and the endpoints contain the LINQ queries (example: _ctx.Students.Where..... ) dbcontext 被注入到 controller 中,端点包含 LINQ 查询(例如: _ctx.Students.Where.....

I want to organize this into layered architecture.我想将其组织成分层架构。

UI layer will contain the api project (controllers) and reference the business layer dll. UI 层将包含 api 项目(控制器)并引用业务层 dll。

Business layer will contain the send email logic, and business logic (grading based on marks).业务层将包含发送 email 逻辑和业务逻辑(根据分数评分)。 I think this must reference the data layer to be able to fetch data.我认为这必须引用数据层才能获取数据。

Data layer will contain the table classes and db context.数据层将包含表类和数据库上下文。

Where do I place my entity framework queries which were previously in the controller action method?我在哪里放置以前在 controller 操作方法中的实体框架查询?

I usually recommend people to use the repository pattern to structure Asp.net application in a monolithic fashion.我通常建议人们使用存储库模式以整体方式构建 Asp.net 应用程序。 At a high level, there are usually three-layer在高层,通常有三层

  1. Repository/Data Layer存储库/数据层
  2. Service/Business layer服务/业务层
  3. Controller/API (Web Project)控制器/API(网络项目)

In Repository Layer, we define all our models and database call(Your Entity framework will be here).在存储层中,我们定义了所有模型和数据库调用(您的实体框架将在这里)。

In the Service Layer, We perform all the business logic.在服务层,我们执行所有的业务逻辑。

And in the web project, we define all the API endpoints and other client-side interaction services.在 web 项目中,我们定义了所有 API 端点和其他客户端交互服务。

The followings are some of the articles related to the Repository pattern:以下是一些与 Repository 模式相关的文章:

  1. https://www.linkedin.com/pulse/repository-pattern-c-pawan-verma/ https://www.linkedin.com/pulse/repository-pattern-c-pawan-verma/
  2. https://medium.com.net-core/repository-pattern-implementation-in-asp.net-core-21e01c6664d7 https://medium.com.net-core/repository-pattern-implementation-in-asp.net-core-21e01c6664d7
  3. https://codewithmukesh.com/blog/repository-pattern-in-as.net-core/ https://codewithmukesh.com/blog/repository-pattern-in-as.net-core/

Some articles, here use the same project to define all the layers but you can simply separate all layers into a separate project (class library).有些文章,这里使用同一个项目来定义所有层,但您可以简单地将所有层分离到一个单独的项目(类库)中。

I usually layer my application like this:我通常这样分层我的应用程序:

  • APIs - EndPoints API - 端点
  • Application Layer - All glueing code, mapping, orchestra code, utilities, and other application-level code comes here应用层——所有粘合代码、映射、管弦乐队代码、实用程序和其他应用层代码都在这里
  • Domain Layer - Purely contains domains, sub-domains, validations, interfaces for repositories and unit of work, and commands.领域层——纯粹包含领域、子领域、验证、存储库和工作单元的接口以及命令。
  • Data Layer - This layer contains the implementation of all the repositories and unit of work interfaces.数据层——该层包含所有存储库和工作单元接口的实现。 And this is the layer where I keep all my queries and database-specific code.这是我保存所有查询和特定于数据库的代码的层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分层架构中的实体框架 - Entity Framework in layered architecture 分层架构中的实体框架? - Entity framework in a layered architecture? 在分层架构中引用 MVVM 中的实体框架 - Referencing Entity Framework in MVVM in layered architecture 在使用Entity Framework的分层架构中,我应该从BLL返回POCO类吗? (需要架构指导) - In a layered architecture using Entity Framework, should I return POCO classes from the BLL? (Architecture guidance needed) 实体框架 - 分层设计 - 在哪里放置连接字符串? - Entity Framework - layered design - Where to put connectionstring? 业务逻辑方法在使用带有Visual Studio 2015的实体框架6的分层应用程序中的位置 - Where do business logic methods go in a layered application using entity framework 6 with Visual Studio 2015 我在哪里将自定义成员资格提供程序放在项目体系结构中? - Where do I place a custom membership provider in my project architecture? 分层体系结构中的实体框架 - Entity Framework in Layered Architectures 锋利的体系结构,如何以及在哪里实现查询? - Sharp architecture, how and where do I implement queries? 为什么使用分层体系结构时,首先在C#实体框架代码上检索数据的速度慢? - why data retrieving slow on C# Entity-Framework Code first when used layered architecture..?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM