简体   繁体   English

如何解决循环依赖问题?

[英]How to fix circular dependency issue?

I have 3 Class Libraries, example below: 我有3个类库,示例如下:

  • ApplicationCore (Main Class Library) ApplicationCore(主类库)
  • Application1 (Added Reference to ApplicationCore) Application1(添加对ApplicationCore的引用)
  • Application2 (Added Reference to ApplicationCore) Application2(添加对ApplicationCore的引用)
  • I would like to use Application 1 features in Application Core Class Library without a circular dependency issue. 我想使用Application Core类库中的Application 1功能,而不会出现循环依赖问题。 What is the best practice to implement features of Application1 to ApplicationCore? 将Application1的功能实现到ApplicationCore的最佳实践是什么?

    将共享功能移至ApplicationCore或新的共享类库。

    Another option is interfaces and dependency injection (DI). 另一个选择是接口和依赖项注入(DI)。

    • Create an interface in the MainClass Library. 在MainClass库中创建一个接口。 (ApplicationCore) (ApplicationCore)
    • Maybe have a default implementation of that interface in ApplicationCore. 也许在ApplicationCore中具有该接口的默认实现。
    • In Application1, setup the DI to user the implementation from Application1 for the interface. 在Application1中,将DI设置为使用Application1中接口的实现。

    In Application2 it will use the default implementation. 在Application2中,它将使用默认实现。

    In Application1 it will use Application1's implementation. 在Application1中,它将使用Application1的实现。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM