2017-07-16 1 views
2

J'ai 2 services que vous pouvez voir avec ces constructeurs:utilisation ninject à la fois le service crée une dépendance cyclique C#

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; 
    } 

Un autre:

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; 
    } 

Avec ces tout constructeurs fonctionne très bien mais quand je l'ajoute ReceptionService au TestService comme ceci:

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; 
    } 

Je reçois cette erreur:

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

Vous injectez ITestService dans ReceptionService et IReceptionService dans TestService. Je ne suis pas surpris que ça ne marche pas. Je ne suis pas sûr de savoir quelle est votre question, mais si cela va dans le sens de "comment se débarrasser de cette erreur?", La réponse est "ne faites pas dépendre A d'une dépendance de B si B dépend de UNE". – oerkelens

+3

Copie possible de [Cyclic dependency with ninject] (https://stackoverflow.com/questions/3511547/cyclic-dependency-with-ninject) – mjwills

+0

@oerkelens J'ai besoin du service de réception à l'intérieur du service de test –

Répondre

3

Mon expérience est que les dépendances cycliques sont généralement causées par Single Responsibility Principle (SRP) violations. En d'autres termes, une ou plusieurs des classes faisant partie du cycle ont trop de responsabilités.

Les deux classes en question ont toutes deux beaucoup de dépendances. Il s'agit d'une odeur de code appelée Constructor Over-Injection et la surinjection du constructeur est également une indication d'une violation de la SRP. En d'autres termes: vos classes enfreignent SRP et la solution consiste à les diviser en plusieurs composants plus petits et plus fins. Cela résout non seulement le SRP (et donc le problème de maintenabilité que cela provoque), mais aussi le problème de dépendance cyclique.