Nestjs 循环依赖

时间:2021-04-25 11:50:15

标签: nestjs

我有相互依赖的模块 A 和 B。模块 C 依赖于模块 B。 我将 forwardRef() 用于模块 A 和 B,但我没有用于 C 和 B,

// a.module.ts
@Module(
    {
        imports  : [
            forwardRef(() => ModuleB),
            forwardRef(() => ModuleC),
        ],
        providers: [
            ServiceA,
        ],
        exports  : [
            ServiceA,
        ]
    },
)
export class ModuleA {}

// a.service.ts
    constructor(
        @Inject(forwardRef(() => ServiceB)) private readonly serviceB: ServiceB,
        @Inject(forwardRef(() => ServiceC)) private readonly serviceC: ServiceC,
    ) {}

// b.module.ts
@Module(
    {
        imports  : [
            forwardRef(() => ModuleA ),
            ModuleC,
        ],
        providers: [
            ServiceB,
        ],
        exports  : [
            ServiceB,
        ]
    },
)
export class ModuleB {}

// b.service.ts
    constructor(
        @Inject(forwardRef(() => ServiceA)) private readonly serviceA: ServiceA,
                                            private readonly serviceC: ServiceC,
    ) {}

// c.module.ts
@Module(
    {
        imports  : [
            forwardRef(() => ModuleA),
        ],
        providers: [
            ServiceB,
        ],
        exports  : [
            ServiceB,
        ]
    },
)
export class ModuleC {}

// c.service.ts
    constructor(
        @Inject(forwardRef(() => ServiceA)) private readonly serviceA: ServiceA,
    ) {}

这会导致模块之间的循环依赖问题,但我无法跟踪根,因为模块 A 位于另一个模块中,并且模块已深度集成/嵌套。 不知道NestJS是如何解决循环依赖的,或者如何更好的调试。 我读过I should avoid it as much as possible

1 个答案:

答案 0 :(得分:0)

我已经设法让它工作了,但你应该检查你的架构,因为如此多的循环依赖肯定是一种不好的做法。

// app.module.ts
@Module({
  imports: [AModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}


// a.module.ts
@Module({
  imports: [BModule, CModule],
  providers: [AService],
  exports: [AService],
})
export class AModule {}

// a.service.ts
@Injectable()
export class AService {
  constructor(
    @Inject(forwardRef(() => BService)) private bService: BService,
    @Inject(forwardRef(() => CService)) private cService: CService,
  ) {}
}

// b.module.ts
@Module({
  imports: [forwardRef(() => AModule), CModule],
  providers: [BService],
  exports: [BService],
})
export class BModule {}

// b.service.ts
@Injectable()
export class BService {
  constructor(
    @Inject(forwardRef(() => AService)) private aService: AService,
    @Inject(forwardRef(() => CService)) private cService: CService,
  ) {}
}

// c.module.ts
@Module({
  imports: [forwardRef(() => BModule), forwardRef(() => AModule)],
  providers: [CService],
  exports: [CService],
})
export class CModule {}

// c.service.ts
@Injectable()
export class CService {
  constructor(@Inject(forwardRef(() => AService)) private aService: AService) {}
}
<块引用>

模块 C 依赖模块 B

你写了这个,但在你的代码中,模块C实际上依赖于模块A,所以我遵循了后者。
此外,在您的模块 C 中,您的 ServiceB 中有 providers。我想这只是一个复制/粘贴错误,但它增加了混乱。

我的思考过程(我不确定我做对了但确实有效)

AModule 是第一个导入的(在 app.module.ts 中),因此它应该是第一个被初始化的。这就是 imports 数组不包含任何前向引用的原因。

BModule 相同,它应该在第二次实例化,最后是 CModule(这就是它包含 2 个前向引用的原因,因为它的优先级最低)。

在那之后,我插入了 @Inject(forwardRef(() => BService)),直到像 Nest can't resolve dependencies of the AService (BService, ?). 这样的所有错误都消失了。