我有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.
答案 0 :(得分:3)
我的经验是循环依赖性通常是由Single Responsibility Principle(SRP)违规引起的。换句话说,作为循环一部分的一个或多个类有太多的责任。
这两个类都有很多依赖关系。这是一种名为Constructor Over-Injection的代码气味,Constructor Over-Injection也是SRP违规的指示。
换句话说:您的类违反了SRP,解决方案是将它们拆分为多个更小,更细粒度的组件。这不仅解决了SRP(以及由此引起的可维护性问题),也解决了循环依赖问题。