Can dependency injection prevent a circular dependency?

后端 未结 3 487
无人及你
无人及你 2020-12-06 04:01

Project#1 has some interfaces and classes that project#2 references.

Now I want to use the implementation of Project#2 in Project#1 but vs.net complains about a circ

3条回答
  •  一生所求
    2020-12-06 04:09

    You can often resolve circular dependency issues with Dependency Injection (DI) using an Abstract Factory. See here for an example.

    However, although you may be able to solve the problem with DI, it would be better to redesign the API to make the circular dependency go away.

    You can often break a circular dependency by changing one of the ends from a query-based API to an event-based API.

提交回复
热议问题