gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:39:36 26 4
gpt4 key购买 nike

我有 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构造函数过度注入(inject)也是违反 SRP 的迹象。

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

关于c# - 在两个服务中使用 ninject 在 c# 中创建循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45128896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com