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

标签 c# web-services dependency-injection ninject

我有 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/

相关文章:

PHP-Laravel 依赖注入(inject) : pass parameters to dependency constructor

wcf - 使用 WCF 代理的依赖注入(inject)

c# - Nullable<T> 实现

c# - string.IndexOf OrdinalIgnoreCase 与 CurrentCultureIgnoreCase 的性能

web-services - 在哪里可以找到有关 REST 核心概念的良好文档?

c# - ASP.NET Web 服务 - 如何使用连接字符串将其连接到数据库?

c# - ITextSharp 异常读取 pdf : Rebuild failed: Dictionary key "" is not a name

c# - 确定两个 SyntaxToken 是否相同

asp.net - ASP.NET 兼容模式下的 WCF 服务 - 传递用户名/密码的最简单方法是什么

java - 使用 Spring JdbcTemplate - 注入(inject)数据源与 jdbcTemplate