使用依赖注入的 TypeScript 应用程序初始化
TypeScript application initialisation that uses Dependency Injection
我正在使用 Webpack GitHub repo 构建一个 TypeScript 项目。默认情况下,应用程序允许依赖注入,但我没有看到任何安装的包提供 IoC,所以我只能假设 TypeScript 已经有某种基本的 DI?我找不到有关 TypeScript IoC 容器的文档。
我正在寻找的是一些设置应用程序初始化的方法,它不会使用 new
运算符创建可笑的 DI 链。参考下面的例子:
class Stage {
constructor(
private light: Light,
) { }
}
class App {
constructor(
private stage: Stage,
) { }
}
class Init {
constructor(
private app: App,
) { }
}
const init: Init = new Init(new App(new Stage()));
看来我需要用解析器做点什么?我是否必须安装类似 InversifyJS 的东西才能实现此目的?
我确信像下面的代码这样的东西是可以实现的,但我如何告诉 IoC 容器解决 Init 的依赖关系?有什么地方我必须创建解析器吗?
const init: Init = new Init();
最后我遇到了同样的问题,我最终使用了可以与 Typescript 一起使用的 inversifyJS。
图书馆很密集,但它绝对可以实现你要找的东西。
您首先需要安装以下内容:
npm install inversify reflect-metadata --save
并在您的 tsconfig.json 文件中进行以下更改:
{
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
然后,使用上面的示例:
import { injectable, inject } from "inversify";
import "reflect-metadata";
@injectable()
class Stage {
private light: Light;
constructor(
@inject(Light) light: Light,
) {
this.light = light;
}
}
@injectable()
class App {
private stage: Stage;
constructor(
@inject(Stage) stage: Stage,
) {
this.stage = stage;
}
}
@injectable()
class Init {
private app: App;
constructor(
@inject(App) app: App
) {
this.app = app;
}
}
然后您应该创建一个 inversify.config.ts
文件。这是将要注入的所有依赖项:
import 'reflect-metadata';
import { Container } from "inversify";
// import all of the Stage/Light/etc. classes also
let DIContainer = new Container();
DIContainer.bind<Stage>(Stage).toSelf();
DIContainer.bind<Light>(Light).toSelf();
export default DIContainer;
最后,转到实例化 Init
class 的文件并将其更改为以下内容:
import 'reflect-metadata';
import DIContainer from '../src/dependencies';
// import Init class as well
const init: Init = DIContainer.resolve<Init>(Init);
很可能我忘记了什么或某处有错误,但这是主要思想。希望这有帮助。
我正在使用 Webpack GitHub repo 构建一个 TypeScript 项目。默认情况下,应用程序允许依赖注入,但我没有看到任何安装的包提供 IoC,所以我只能假设 TypeScript 已经有某种基本的 DI?我找不到有关 TypeScript IoC 容器的文档。
我正在寻找的是一些设置应用程序初始化的方法,它不会使用 new
运算符创建可笑的 DI 链。参考下面的例子:
class Stage {
constructor(
private light: Light,
) { }
}
class App {
constructor(
private stage: Stage,
) { }
}
class Init {
constructor(
private app: App,
) { }
}
const init: Init = new Init(new App(new Stage()));
看来我需要用解析器做点什么?我是否必须安装类似 InversifyJS 的东西才能实现此目的?
我确信像下面的代码这样的东西是可以实现的,但我如何告诉 IoC 容器解决 Init 的依赖关系?有什么地方我必须创建解析器吗?
const init: Init = new Init();
最后我遇到了同样的问题,我最终使用了可以与 Typescript 一起使用的 inversifyJS。
图书馆很密集,但它绝对可以实现你要找的东西。 您首先需要安装以下内容:
npm install inversify reflect-metadata --save
并在您的 tsconfig.json 文件中进行以下更改:
{
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
然后,使用上面的示例:
import { injectable, inject } from "inversify";
import "reflect-metadata";
@injectable()
class Stage {
private light: Light;
constructor(
@inject(Light) light: Light,
) {
this.light = light;
}
}
@injectable()
class App {
private stage: Stage;
constructor(
@inject(Stage) stage: Stage,
) {
this.stage = stage;
}
}
@injectable()
class Init {
private app: App;
constructor(
@inject(App) app: App
) {
this.app = app;
}
}
然后您应该创建一个 inversify.config.ts
文件。这是将要注入的所有依赖项:
import 'reflect-metadata';
import { Container } from "inversify";
// import all of the Stage/Light/etc. classes also
let DIContainer = new Container();
DIContainer.bind<Stage>(Stage).toSelf();
DIContainer.bind<Light>(Light).toSelf();
export default DIContainer;
最后,转到实例化 Init
class 的文件并将其更改为以下内容:
import 'reflect-metadata';
import DIContainer from '../src/dependencies';
// import Init class as well
const init: Init = DIContainer.resolve<Init>(Init);
很可能我忘记了什么或某处有错误,但这是主要思想。希望这有帮助。