10

有关 nestjs 的循环引用问题;

 2 years ago
source link: https://www.v2ex.com/t/872886
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Node.js

有关 nestjs 的循环引用问题;

  liuxsen93 · 49 分钟前 · 103 次点击

我有几个 module ,service 引用结构如下

A -> B -> A. 也就是说 A ,B 有相互引用关系;这样会导致循环引用问题; nestjs 编译会报错;

通过查询,了解到的解决方法有两种;

  1. 抽离一个 module C ,然后引入 Service A ,B ; 在 A ,B controller 中直接引入 Service C ;那样 service A ,B 就会是单纯业务逻辑,不会引发 circular dependencies
  2. 是使用 forwardref

这两种方法都可以;但是有点疑问,希望能得到解惑;谢谢了;

方法 1: 会多出一个 module ; 那如果有关联关系的业务实体,是不是都会引发一个新的 module 来避免 circular dependencies

那我是不是可以把所有的 service 集中在一起 Module Common ,然后供 controller 消费,就能避免在 service 中消费其他 service 引发 circular dependencies ;这样的话 Module Common 就变成了垃圾桶了,啥都放在里面; 

方法 2: 有可能发生未知错误;不知道引用关系到底是什么样子的;跟 nestjs 的依赖注入 设计有点相反;不太想用


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK