简体   繁体   English

在StartUp.cs中引用DbContext正在使.NET Core应用程序中的体系结构混乱

[英]Referencing DbContext in StartUp.cs is messing up my architecture in .NET Core app

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. 特别是我无法弄清楚EF的DbContext应该去哪里。 I don't want my API referencing my Data layer. 我不希望我的API引用我的数据层。 The only thing I can think of is installing EntityFramework to Domain layer and having the DbContext reside in there. 我唯一想到的就是将EntityFramework安装到Domain层并在其中放置DbContext。

TestProj.Data Class Library (.NET Core) TestProj.Data类库(.NET Core)
Entity Framework is installed. 实体框架已安装。 Contains UnitOfWork class, Repositories folder with all repositories that make database calls. 包含UnitOfWork类,Repositories文件夹以及所有进行数据库调用的存储库。 Will also contain EF Migrations. 还将包含EF迁移。 References TestProj.Domain for its Business Entities. 将TestProj.Domain引用为其业务实体。

TestProj.Domain Class Library (.NET Core) TestProj.Domain类库(.NET Core)
Models folder with all business entities, IUnitOfWork interface and all of the interfaces for the repositories in TestProj.Data ie ICustomerRepository. 具有所有业务实体,IUnitOfWork接口和TestProj.Data中存储库的所有接口(即ICustomerRepository)的Models文件夹。

TestProj.Api Web API Project (.NET Core) TestProj.Api Web API项目(.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, ie 我相信这应该只引用TestProj.Domain,但是我也需要引用TestProj.Data以便在StartUp.cs中设置所有服务,即

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

This is where I start getting confused. 这就是我开始感到困惑的地方。

My questions: 我的问题:
Is it alright for the Api project to reference both the Domain and Data projects? Api项目可以同时引用Domain和Data项目吗? Seems like I need to in order to setup dependency injection in StartUp.cs 似乎我需要在StartUp.cs中设置依赖项注入

Is it correct that I am putting the interfaces for everything in the Domain project? 我将所有项目的接口都放在Domain项目中是否正确?

What project should the TestProjDbContext sit for EF? TestProjDbContext应该适合哪个项目? My initial thought was the Data project? 我最初的想法是数据项目?

Where do items like DTOs/Pocos go? DTO / Pocos之类的物品去哪儿了? In the API project or the Domain project? 在API项目还是Domain项目中? 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可以安装AutoMapper,并且由于它引用了TestProj.Domain,因此可以将原始业务实体映射到该API中的DTO。

Finally, where does business logic go? 最后,业务逻辑去哪儿了? Rules in between the Data layer and the API. 数据层和API之间的规则。 I am assuming the proper place is TestProj.Domain. 我假设正确的位置是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. 如果API仅调用域中的业务逻辑,而不是将IUnitOfWork注入到我的api控制器中,那么我将注入TestProj.Domain.Services.CustomerService,这也许可以解决我的问题。 Does this make sense? 这有意义吗?

My opinion on this: 我对此的看法:

Is it alright for the Api project to reference both the Domain and Data projects? Api项目可以同时引用Domain和Data项目吗?

It's OK for me. 对我来说还好。

Is it correct that I am putting the interfaces for everything in the Domain project? 我将所有项目的接口都放在Domain项目中是否正确?

YES

What project should the TestProjDbContext sit for EF? TestProjDbContext应该适合哪个项目? My initial thought was the Data project? 我最初的想法是数据项目?

I usually place BlahBlahContext class into Domain project 我通常将BlahBlahContext类放入Domain项目

Where do items like DTOs/Pocos go? DTO / Pocos之类的物品去哪儿了? In the API project or the Domain project? 在API项目还是Domain项目中?

I made for this different project, called Dto. 我为这个名为Dto的项目进行了开发。 and then referenced on this when needed. 然后在需要时对此进行引用。

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可以安装AutoMapper,并且由于它引用了TestProj.Domain,因此可以将原始业务实体映射到该API中的DTO。

Sure 当然

Finally, where does business logic go? 最后,业务逻辑去哪儿了? Rules in between the Data layer and the API. 数据层和API之间的规则。

I use for this different project in solution -> called Services 我在解决方案->称为服务中使用了这个不同的项目

Hope this helps. 希望这可以帮助。

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

相关问题 如何在startup.cs的ConfigureServices方法中正确注入DbContext实例(ASP.net core 1.1)? - How to inject DbContext instance in the ConfigureServices method of startup.cs correctly (ASP.net core 1.1)? .net 核心 Startup.cs CreateScope 或 BuildServiceProvider - .net core Startup.cs CreateScope or BuildServiceProvider .NET Core program.cs 和 Startup.cs 未命中 - .NET Core program.cs and Startup.cs not hit 在我的.Net框架应用程序中,“startup.cs”(.Net Core)的等价物是什么? - what is the equivalent of “startup.cs”(.Net Core) in my .Net framework application? 使用DbContext的Singleton - 在Startup.cs中创建实例 - Singleton with DbContext - creating instance in Startup.cs 在启动文件中将配置设置为app.UseAPIResponseWrapper()时如何在.NET Core中返回视图 - How to return view in .NET Core when configuration set to app.UseAPIResponseWrapper() in startup.cs file ASP.NET Core 6 MVC 中的 Startup.cs 文件未显示在我的项目中 - Startup.cs file in ASP.NET Core 6 MVC is not showing in my project 如何在我的 ASP.NET Core 应用程序中使用 Startup.cs 之外的服务? - How can I use a service outside of Startup.cs in my ASP.NET Core Application? 在ASP.NET CORE中的Startup.cs中设置动态变量 - Set Dynamic Variables in Startup.cs in ASP.NET CORE 在Startup.cs .net core 2.1中加载程序集 - Load assembly in Startup.cs .net core 2.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM