gpt4 book ai didi

typescript - 在 inversify 中将具有属性注入(inject)的类绑定(bind)到内核

转载 作者:行者123 更新时间:2023-12-02 04:07:09 25 4
gpt4 key购买 nike

我正在尝试通过 inversify 在我的节点项目上实现依赖注入(inject).

我有一个这样的内核配置:inversify.config.ts

import "reflect-metadata";
import {Kernel, interfaces} from "inversify";

import {Config} from "./config";
import {Collection} from "./collection";

let kernel = new Kernel();

kernel.bind<Config>("Config").to(Config).inSingletonScope();
// kernel.bind<interfaces.Newable<Collection>>("Collection").toConstructor<Collection>(Collection);
// it works without the line above

A类:config.ts

import {injectable} from "inversify";
@injectable()
export class Config {
constructor() {}
}

具有属性 DI collection.ts 的类

import {injectable} from "inversify";
import getDecorators from "inversify-inject-decorators";
import kernel from "../inversify.config";
let {lazyInject} = getDecorators(kernel);

@injectable()
export class Collection {
@lazyInject(Config)
private Config: Config;

constructor(a: string, b: string) {}
}

如果我不使用属性注入(inject)绑定(bind)类,一切都会按预期工作。当我尝试用 @lazyInject 绑定(bind)一个类时如示例所示

kernel.bind<interfaces.Newable<Collection>>("Collection").toConstructor<Collection>(Collection);

导入inversify.config.ts中的行开始处理Collection.ts和行

import kernel from "../inversify.config";`

在里面。然而,当我们到达时Collection.ts当我们已经处理inversify.config.ts时文件行

import kernel from "../inversify.config";`

以某种方式返回undefined制作内核undefined对于 Collection类(class)。因此@lazyInject DI 失败。

最终当我尝试阅读Config时在Collection它失败了:

TypeError: Cannot read property 'get' of undefined
at resolve (node_modules/inversify-inject-decorators/lib/decorators.js:24:30)
at Category.getter (node_modules/inversify-inject-decorators/lib/decorators.js:6:47)

我想知道是否有一种方法可以通过 @lazyInject 完成将类与属性 DI 绑定(bind)无需将内核定义移动到与其中一个类相同的文件中。我正在寻找一种方法来导入内核,但使其正常工作。

最佳答案

你的问题是你有循环依赖:

enter image description here

我将更改您的代码并添加几个附加文件。不过,不需要 types.ts 文件,但建议将所有类型标识符保留在一个位置。

更改代码后,依赖关系图将更改为以下内容:

enter image description here

正如您所见,我们已经消除了循环依赖。

inversify.config.ts

import "reflect-metadata";
import {Kernel, interfaces} from "inversify";
import getDecorators from "inversify-inject-decorators";
import { makeProvideDecorator } from "inversify-binding-decorators";

let kernel = new Kernel();
let {lazyInject} = getDecorators(kernel);
let provide = makeProvideDecorator(kernel);

export { lazyInject, provide };

类型.ts

let TYPES = {
Config: "Config",
Collection: "Collection",
};

export default TYPES;

config.ts

import { provide } from "../inversify.config";
import TYPES from "./types";

@provide(TYPES.Config)
export class Config {
constructor() {}
}

export default Config;

集合.ts

import { lazyInject, provide } from "../inversify.config";

@provide(TYPES.Collection)
export class Collection {
@lazyInject(TYPES.Config)
private Config: Config;
constructor(a: string, b: string) {}
}

export default Collection;

main.ts

您需要在导入 @provide 时导入所有依赖项,然后生成绑定(bind)。

import Config from "./lib/config";
import Collection from "./models/collection";

//...

我们已经能够使用inversify-binding-decorators中的@provide装饰器消除循环依赖。 .

关于typescript - 在 inversify 中将具有属性注入(inject)的类绑定(bind)到内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38927236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com