本文介绍了在 StartUp.cs 中引用 DbContext 弄乱了我在 .NET Core 应用程序中的架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一个可以正常运行的项目结构,但一直遇到障碍.尤其是我无法弄清楚 EF 的 DbContext 应该去哪里的地方.我不希望我的 API 引用我的数据层.我唯一能想到的就是将 EntityFramework 安装到域层并将 DbContext 驻留在那里.

I am trying to come up with a project structure that will flow properly, but keep running into road blocks. Especially one where I can't figure out where DbContext for EF should go. I don't want my API referencing my Data layer. The only thing I can think of is installing EntityFramework to Domain layer and having the DbContext reside in there.

TestProj.Data 类库 (.NET Core)
实体框架已安装.包含 UnitOfWork 类、包含进行数据库调用的所有存储库的 Repositories 文件夹.还将包含 EF 迁移.为其业务实体引用 TestProj.Domain.

TestProj.Data Class Library (.NET Core)
Entity Framework is installed. Contains UnitOfWork class, Repositories folder with all repositories that make database calls. Will also contain EF Migrations. References TestProj.Domain for its Business Entities.

TestProj.Domain 类库 (.NET Core)
Models 文件夹中包含所有业务实体、IUnitOfWork 接口以及 TestProj.Data 中存储库的所有接口,即 ICustomerRepository.

TestProj.Domain Class Library (.NET Core)
Models folder with all business entities, IUnitOfWork interface and all of the interfaces for the repositories in TestProj.Data i.e. ICustomerRepository.

TestProj.Api Web API 项目 (.NET Core)
我相信这应该只引用 TestProj.Domain,但我还需要引用 TestProj.Data 以便在 StartUp.cs 中设置所有服务,即

TestProj.Api Web API Project (.NET Core)
I believe this should only be referencing TestProj.Domain, but I need to reference the TestProj.Data also in order to setup all of the services in StartUp.cs, i.e.

services.AddDbContext<TestProjDbContext>(options =>           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddTransient<IUnitOfWork, UnitOfWork>();
        services.AddTransient<ICustomerRepository, CustomerRepository>();

这就是我开始困惑的地方.

This is where I start getting confused.

我的问题:
Api项目可以同时引用Domain和Data项目吗?似乎我需要在 StartUp.cs 中设置依赖注入

My questions:
Is it alright for the Api project to reference both the Domain and Data projects? Seems like I need to in order to setup dependency injection in StartUp.cs

我把所有东西的接口都放在域项目中是否正确?

Is it correct that I am putting the interfaces for everything in the Domain project?

TestProjDbContext 应该用于 EF 什么项目?我最初的想法是数据项目?

What project should the TestProjDbContext sit for EF? My initial thought was the Data project?

诸如 DTO/Pocos 之类的项目去哪里了?在API项目还是Domain项目?假设 API 可以安装 AutoMapper,并且由于它引用了 TestProj.Domain,因此可以将原始业务实体映射到 API 中的 DTO.

Where do items like DTOs/Pocos go? In the API project or the Domain project? U an assuming the API can have AutoMapper installed and since it references TestProj.Domain in can map the original business entities to the DTOs in the API.

最后,业务逻辑去哪里了?数据层和 API 之间的规则.我假设合适的地方是 TestProj.Domain.如果 API 只调用域中的业务逻辑,而不是将 IUnitOfWork 注入我的 api 控制器,那么这可能会解决我的问题,我会注入 TestProj.Domain.Services.CustomerService.这有意义吗?

Finally, where does business logic go? Rules in between the Data layer and the API. I am assuming the proper place is TestProj.Domain. Maybe this would fix my issue if the API only makes calls to the business logic in the domain instead of injecting IUnitOfWork into my api controllers I would inject TestProj.Domain.Services.CustomerService. Does this make sense?

推荐答案

我对此的看法:

Api 项目可以同时引用 Domain 和 Data 项目吗?

对我来说没问题.

我把所有东西的接口都放在域项目中是否正确?

TestProjDbContext 应该用于 EF 什么项目?我最初的想法是数据项目?

我通常将 BlahBlahContext 类放到 Domain 项目中

I usually place BlahBlahContext class into Domain project

诸如 DTO/Pocos 之类的项目去哪里了?在 API 项目中还是在 Domain 项目中?

我为这个不同的项目制作,称为 Dto.然后在需要时引用它.

I made for this different project, called Dto. and then referenced on this when needed.

假设 API 可以安装 AutoMapper,并且由于它引用了 TestProj.Domain,因此可以将原始业务实体映射到 API 中的 DTO.

当然

最后,业务逻辑去哪儿了?数据层和 API 之间的规则.

我在解决方案中用于这个不同的项目 -> 称为服务

I use for this different project in solution -> called Services

希望这会有所帮助.

这篇关于在 StartUp.cs 中引用 DbContext 弄乱了我在 .NET Core 应用程序中的架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:07