Best practice injecting service into another service [closed]

China☆狼群 提交于 2021-01-05 08:53:14

问题


We suppose that I have Two Entity (Subscription and Application) and for each one his Repository layer named (SubscriptionRepository and ApplicationRepository) and two Service for each one (ISubscriptionService and IApplicationService)

In some case, suppose we end up with a case where ApplicationService need to inject SubscriptionService and a case where SubscriptionService need to inject ApplicationService ( the reverse) and and of course it Is a Circular Dependency

My question is:

When I want to inject service into another how I should reflect to not fall into this type of problem. (it means how i can decide if i need to inject ApplicationService into SubscriptionService or the reverse)?


回答1:


You could invert the dependencies of both services to each other by making both depend on another abstraction instead. In your case, as both are domain services, domain events emitted by either your entities or the domain services could be a good fit.

But without knowing more details about your domain and what Application and Subscription are dedicated for it is hard to tell if you are not facing a design problem.

If you think there is a design flaw I suggest to ask yourself these questions which often help me when I am not sure if I have drawn the correct boundaries in my domain model.

  • Would it make more sense to merge these two components which are dependent on each other?
  • Does my component A access some feature of component B which should rather be part of component A?
  • Does my component B access some feature of component A which should rather be part of component B?
  • Should functionality of A or B which are accessed by the other rather be in a third not yet discovered component? This could be another interface, domain events or something similar...


来源:https://stackoverflow.com/questions/64927928/best-practice-injecting-service-into-another-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!