在StartUp.cs中引用DbContext正在使.NET Core应用程序中的体系结构混乱 [英] Referencing DbContext in StartUp.cs is messing up my architecture in .NET Core app

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

问题描述

我正在尝试提出一个项目结构,该结构将正常运行,但会遇到很多障碍.特别是我无法弄清楚EF的DbContext应该去哪里.我不希望我的API引用我的数据层.我唯一想到的就是将EntityFramework安装到Domain层并在其中放置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类,存储库"文件夹以及所有进行数据库调用的存储库.还将包含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)
具有所有业务实体,IUnitOfWork接口以及TestProj.Data中存储库的所有接口(即ICustomerRepository)的所有接口的Models文件夹.

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

我将所有项目的接口都放在Domain项目中是否正确?

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项目吗?

Is it alright for the Api project to reference both the Domain and Data projects?

我可以.

我将所有项目的接口都放在Domain项目中是否正确?

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?

我通常将BlahBlahContext类放入Domain项目

I usually place BlahBlahContext class into Domain project

DTO/Pocos之类的物品去哪儿了?是在API项目中还是在Domain项目中?

Where do items like DTOs/Pocos go? In the API project or the Domain project?

我为这个名为Dto的项目进行了开发.然后在需要时对此进行引用.

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

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

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之间的规则.

Finally, where does business logic go? Rules in between the Data layer and the API.

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

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

希望这会有所帮助.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆