我在哪里将实体框架查询放在分层体系结构中?
我有 .net core api 项目,其中包含以下文件夹:
模型包含数据库表类和 dbcontext
服务包含发送电子邮件的逻辑和业务逻辑(示例根据分数计算学生成绩)
控制器包含具有相应操作(api 端点)的控制器。 dbcontext 被注入到控制器中,端点包含 LINQ 查询(例如:_ctx.Students.Where.....
)
我想将其组织成分层架构。
UI层将包含api项目(控制器)并引用业务层dll。
业务层将包含发送电子邮件逻辑和业务逻辑(根据分数评分)。我认为这必须引用数据层才能获取数据。
数据层将包含表类和数据库上下文。
我应该将之前位于控制器操作方法中的实体框架查询放在哪里?
I have .net core api project with following folders:
Models contains the database table classes and dbcontext
Services contain logic to send email, and business logic (example calculate student grade based on marks)
Controller contains the controllers with respective actions (api endpoints). The dbcontext is injected into the controller and the endpoints contain the LINQ queries (example: _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.
Business layer will contain the send email logic, and business logic (grading based on marks). 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我经常建议使用存储库模式来构建 ASP.NET 应用程序,特别是在采用整体架构时。此模式通过将应用程序组织为三个主要层来促进明确的关注点分离:
分层结构概述:
要进一步阅读有关在 ASP.NET 中实现存储库模式的信息,请考虑这些富有洞察力的文章:
虽然一些示例显示了单个项目中的所有层,但将每个层分离到不同的类库中通常是有益的,以获得更好的模块化和可维护性。
I often suggest using the repository pattern to structure ASP.NET applications, especially when adopting a monolithic architecture. This pattern promotes a clear separation of concerns by organizing the application into three primary layers:
Layered Structure Overview:
For further reading on implementing the repository pattern in ASP.NET, consider these insightful articles:
While some examples show all layers within a single project, it’s often beneficial to separate each layer into distinct class libraries for better modularity and maintainability.
我通常会这样分层我的应用程序:
I usually layer my application like this: