Umu*_*zer 1 dependency-injection typescript inversifyjs
我正在尝试通过inverseify对我的节点项目进行依赖注入。
我有一个这样的内核配置: 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
Run Code Online (Sandbox Code Playgroud)
一类: config.ts
import {injectable} from "inversify";
@injectable()
export class Config {
constructor() {}
}
Run Code Online (Sandbox Code Playgroud)
具有属性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) {}
}
Run Code Online (Sandbox Code Playgroud)
如果我不使用属性注入来绑定类,那么一切都会按预期进行。当我尝试绑定一个类时,@lazyInject如示例所示
kernel.bind<interfaces.Newable<Collection>>("Collection").toConstructor<Collection>(Collection);
Run Code Online (Sandbox Code Playgroud)
导入行inversify.config.ts开始处理 Collection.ts,该行
import kernel from "../inversify.config";`
Run Code Online (Sandbox Code Playgroud)
在里面。但是,当我们到达Collection.ts时,我们已经在处理inversify.config.ts文件行
import kernel from "../inversify.config";`
Run Code Online (Sandbox Code Playgroud)
不知何故返回undefined使内核undefined的Collection类。因此,@lazyInjectDI失败。
最终,当我尝试阅读时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)
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法可以通过将属性定义DI绑定到一个类@lazyInject 而不将内核定义移动到带有一个类的同一文件中。我正在寻找一种可以立即导入内核的方法,但是可以使其正常工作。
您的问题是您具有循环依赖关系:
我将更改您的代码并添加几个其他文件。尽管如此,types.ts不需要he文件,但建议将所有类型标识符都放在一个位置。
更改代码后,依赖关系图将更改为以下内容:
如您所知,我们已经消除了循环依赖。
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 };
Run Code Online (Sandbox Code Playgroud)
let TYPES = {
Config: "Config",
Collection: "Collection",
};
export default TYPES;
Run Code Online (Sandbox Code Playgroud)
import { provide } from "../inversify.config";
import TYPES from "./types";
@provide(TYPES.Config)
export class Config {
constructor() {}
}
export default Config;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
导入所有依赖项时,需要先导入它们,然后执行@provide并生成绑定。
import Config from "./lib/config";
import Collection from "./models/collection";
//...
Run Code Online (Sandbox Code Playgroud)
我们已经能够使用inversify-binding-decorators中的@provide装饰器消除循环依赖。
| 归档时间: |
|
| 查看次数: |
2111 次 |
| 最近记录: |