[英]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
在高层,通常有三层
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 模式相关的文章:
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:我通常这样分层我的应用程序:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.