在两个服务中使用 ninject 在 c# 中创建循环依赖

use ninject in both service creates cyclical dependency in c#

我有 2 个服务,正如您在这些构造函数中看到的那样:

public ReceptionService(DataContext ctx, IPaymentService PaymentService, IReceptionHistoryService ReceptionHistoryService, ITestService TestService, ITestRepository TestRepository ,IReferredRepository ReferredRepository, IAutomotiveRepository AutomotiveRepository,IReceptionRepository ReceptionRepository,IReferredService ReferredService,IAutomotiveService AutomotiveService)
    {
        _ctx = ctx;
        _automotiveRepository = AutomotiveRepository;
        _referredRepository = ReferredRepository;
        _receptionRepository = ReceptionRepository;
        _referredService = ReferredService;
        _automotiveService = AutomotiveService;
        _testService = TestService;
        _receptionHistoryService = ReceptionHistoryService;
        _paymentService = PaymentService;
    }

一个又一个:

 public TestService(DataContext ctx, IReceptionService ReceptionService ,IParameterService ParameterService, ILineService LineService, ITestRepository TestRepository, IReceptionHistoryService ReceptionHistoryService
        , IReceptionHistoryRepository ReceptionHistoryRepository)
    {
        _TestRepository = TestRepository;
        _parameterService = ParameterService;
        _receptionHistoryRepository = ReceptionHistoryRepository;
        _receptionHistoryService = ReceptionHistoryService;
        _lineService = LineService;
        _ctx = ctx;
    }

使用这些构造函数一切正常,但是当我像这样将 ReceptionService 添加到 TestService 时:

 public TestService(DataContext ctx, IReceptionService ReceptionService ,IParameterService ParameterService, ILineService LineService, ITestRepository TestRepository, IReceptionHistoryService ReceptionHistoryService
        , IReceptionHistoryRepository ReceptionHistoryRepository)
    {
        _TestRepository = TestRepository;
        _parameterService = ParameterService;
        _receptionHistoryRepository = ReceptionHistoryRepository;
        _receptionHistoryService = ReceptionHistoryService;
        _lineService = LineService;
        _ReceptionService = ReceptionService;
        _ctx = ctx;
    }

我收到这个错误:

Error activating IReceptionService using binding from IReceptionService to ReceptionService
A cyclical dependency was detected between the constructors of two services.

Activation path:
 5) Injection of dependency IReceptionService into parameter ReceptionService of constructor of type TestService
 4) Injection of dependency ITestService into parameter TestService of constructor of type ReceptionService
 3) Injection of dependency IReceptionService into parameter ReceptionService of constructor of type TestService
 2) Injection of dependency TestService into parameter instance of constructor of type NinjectIISHostingServiceHost{TestService}
 1) Request for NinjectIISHostingServiceHost{TestService}

Suggestions:
 1) Ensure that you have not declared a dependency for IReceptionService on any implementations of the service.
 2) Consider combining the services into a single one to remove the cycle.
 3) Use property injection instead of constructor injection, and implement IInitializable
    if you need initialization logic to be run after property values have been injected.

我的经验是,循环依赖通常是由 Single Responsibility Principle (SRP) 违规引起的。换句话说,作为循环一部分的 类 中的一个或多个责任太多。

有问题的两个 类 都有很多依赖关系。这是一种称为 Constructor Over-Injection 的代码味道,构造函数过度注入也是 SRP 违规的一个迹象。

换句话说:您的 类 违反了 SRP,解决方案是将它们拆分为多个更小、更细粒度的组件。这不仅解决了 SRP(以及由此导致的可维护性问题),还解决了循环依赖问题。