避免 .NET Core 中 n 层架构的循环依赖

Avoid cyclic dependency in n-layer architecture in .NET Core

我在.net core(console app)中创建了这样的n层架构:

Company.App
   -> reference to Company.Services
   -> reference to Company.Infrastructure

Company.Services
   -> reference to Company.Infrastructure

   + IWorkStarter
   + WorkStarter - (ExternalClient is injected)

Company.Infrastructure
   + RabbitMqConsumer - (IWorkStarter must be injected) - Cyclic dependency
   + IExternalClient

如您所见,Services 已经引用了 Infrastructure。而且我还必须在兔子消费者内部使用 IWorkStarter 。似乎我不需要以正确的方式分离这些组件所需的知识。请你帮助我好吗?项目很简单

遵循我在 Clean Architecture(洋葱架构)中经常看到的具有 ASP.NET Core 的设计。

查看以下内容

Company.App
   -> reference to Company.Services
   -> reference to Company.Infrastructure
   -> reference to Company.Core

    + Compsition Root maps everything
   
Company.Services
   -> reference to Company.Infrastructure
   -> reference to Company.Core

   + WorkStarter - (ExternalClient is injected)

Company.Infrastructure
   -> reference to Company.Core
   
   + RabbitMqConsumer - (IWorkStarter must be injected)
   
Company.Core
   + IWorkStarter
   + IExternalClient

将抽象移到他们自己的关注中

下面的参考库提供了一个很好的例子

https://github.com/ardalis/CleanArchitecture

The Core Project

The Core project is the center of the Clean Architecture design, and all other project dependencies should point toward it. As such, it has very few external dependencies.