如何在没有循环依赖的情况下使用环回4实现链接模型 [英] How to implement chained models with loopback 4 without getting a Circular dependency

查看:107
本文介绍了如何在没有循环依赖的情况下使用环回4实现链接模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当模型具有循环依赖性时,回送会报告错误。我正在寻找一种无需更改模型即可解决此问题的方法。

When models have circular dependencies Loopback reports an error. I am looking for a way to solve the issue without changing the models.

如果模型与自身相关,则解决方案似乎正在使用Getter.fromValue()。但是,当关系通过多个模型循环时,此解决方案似乎不太有用。

If a model relates to itself the solution seems to be using Getter.fromValue(). However, when relations are circular through more models this solution seems not so usefull.

一个具有三个模型A,B和C的示例。让我们假设A引用B ,B引用C,而C引用A。然后,按照LB4文档,存储库将如下所示:

An example with three models A, B, and C. Let us assume that A references B, B references C, and C references A. Then the repositories would look like this when following the LB4 documentation:

Repository A:
constructor(
@repository(BRepo) private bGetter: Getter<BRepo>,
...

Repository B:
constructor(
@repository(CRepo) private cGetter: Getter<CRepo>,
...

Repository C:
constructor(
@repository(ARepo) private aGetter: Getter<ARepo>,
...

但这会导致循环依赖项失败。因此,问题是-如何在不更改模型依赖性的情况下解决此问题。

But this leads to a circular dependency failure. So the question is - how to fix this without changing the model dependencies.

推荐答案

该解决方案似乎将存储库绑定为Singletons

The solution seems to be binding the repositories as Singletons

@bind({scope: BindingScope.SINGLETON})
export class Repository ...

默认绑定是Transient,它会触发循环依赖项检查。

Default binding is Transient which triggers the circular dependency check.

这篇关于如何在没有循环依赖的情况下使用环回4实现链接模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆