gpt4 book ai didi

typescript - 依赖注入(inject) : recommended pattern for injecting NPM modules

转载 作者:搜寻专家 更新时间:2023-10-30 21:23:48 25 4
gpt4 key购买 nike

我想使用 Inversify 移除对 NPM 模块的硬依赖,并将它们作为构造函数参数注入(inject)。在我试一试之前,这似乎要简单得多。

事实证明,大多数 DefinitelyTyped 模块都懒得导出接口(interface),即使它们导出接口(interface),也很少包含代表整个模块的接口(interface)。此外,当一个类被导出时,我仍然必须手动为该类定义一个构造函数接口(interface)。

这意味着我必须为几乎每个模块做这样的事情:

import * as ConcreteModule from 'module'
import { ContainerModule } from 'inversify'

export interface ModuleInstance {
// copy a ton of stuff from DefinitelyTyped repo,
// because they didn't export any interfaces
}

export interface ModuleConstructor {
new (...args: any[]): ModuleInstance
}

export const ModuleConstructorSymbol = Symbol('ModuleConstructor')

export const ModuleContainer = new ContainerModule((bind) => {
bind<ModuleConstructor>(ModuleConstructorSymbol).toConstantValue(ConcreteModule)
})

有什么方法可以简化其中的一些吗?注入(inject) NPM 模块的开销非常大,Inversify 文档中没有任何指导。管理您需要的所有不同导入/导出(接口(interface)、符号和容器)的名称是一件痛苦的事情,需要提出某种一致的命名方案。似乎没有 TypeScript 支持从模块自动创建接口(interface)的某种方式,就没有办法以理智的方式注入(inject) NPM 包。

我想我可以只对模块使用 jest 的自动模拟功能,但我真的不喜欢将我的代码设计成只能使用特定测试框架进行单元测试的方式。

如果我能做到这一点,这似乎至少更容易实现:

import * as ConcreteModule from 'module'

export interface TheModule extends ConcreteModule {}

但这仅在模块导出一个类(不是工厂)并且仍然不能真正帮助我使用构造函数时才有效。

最佳答案

以下示例演示了如何将 npm 模块(lodashsequelize)注入(inject)到类 SomeClass 中。

目录结构如下:

src/
├── entities
│ └── some_class.ts
├── index.ts
└── ioc
├── interfaces.ts
├── ioc.ts
└── types.

/src/ioc/types.ts

const TYPES = {
Sequelize: Symbol("Sequelize"),
Lodash: Symbol("Lodash"),
SomeClass: Symbol("SomeClass")
};

export { TYPES };

/src/ioc/interfaces.ts

import * as sequelize from "sequelize";
import * as _ from "lodash";

export type Sequelize = typeof sequelize;
export type Lodash = typeof _;

export interface SomeClassInterface {
test(): void;
}

/src/ioc/ioc.ts

import { Container, ContainerModule } from "inversify";
import * as sequelize from "sequelize";
import * as _ from "lodash";
import { TYPES } from "./types";
import { Sequelize, Lodash } from "./interfaces";
import { SomeClass } from "../entities/some_class";

const thirdPartyDependencies = new ContainerModule((bind) => {
bind<Sequelize>(TYPES.Sequelize).toConstantValue(sequelize);
bind<Lodash>(TYPES.Lodash).toConstantValue(_);
// ..
});

const applicationDependencies = new ContainerModule((bind) => {
bind<SomeClass>(TYPES.SomeClass).to(SomeClass);
// ..
});

const container = new Container();

container.load(thirdPartyDependencies, applicationDependencies);

export { container };

/src/entitites/some_class.ts

import { Container, injectable, inject } from "inversify";
import { TYPES } from "../ioc/types";
import { Lodash, Sequelize, SomeClassInterface } from "../ioc/interfaces";

@injectable()
class SomeClass implements SomeClassInterface {

private _lodash: Lodash;
private _sequelize: Sequelize;

public constructor(
@inject(TYPES.Lodash) lodash,
@inject(TYPES.Sequelize) sequelize,
) {
this._sequelize = sequelize;
this._lodash = lodash;
}

public test() {
const sequelizeWasInjected = typeof this._sequelize.BIGINT === "function";
const lodashWasInjected = this._lodash.cloneDeep === "function";
console.log(sequelizeWasInjected); // true
console.log(lodashWasInjected); // true
}

}

export { SomeClass };

/src/index.ts

import "reflect-metadata";
import { container } from "./ioc/ioc";
import { SomeClassInterface } from "./ioc/interfaces";
import { TYPES } from "./ioc/types";

const someClassInstance = container.get<SomeClassInterface>(TYPES.SomeClass);
someClassInstance.test();

/package.json

{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"inversify": "^4.1.0",
"lodash": "^4.17.4",
"reflect-metadata": "^0.1.10",
"sequelize": "^3.30.4"
},
"devDependencies": {
"@types/lodash": "^4.14.63",
"@types/sequelize": "^4.0.51"
}
}

/tsconfig.json

{
"compilerOptions": {
"target": "es5",
"lib": ["es6", "dom"],
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}

此示例现已在 inversify docs 中可用.

关于typescript - 依赖注入(inject) : recommended pattern for injecting NPM modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669697/

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