微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

InversifyJS

编程之家收集整理的这个编程导航主要介绍了InversifyJS编程之家,现在分享给大家,也给大家做个参考。

InversifyJS 介绍

nversifyjs是一个强大的轻量级( 4kb ) ( IoC ) 反转控制容器,用于app和JavaScript应用。 pico容器使用类构造函数来标识和注入它的依赖项。 Inversifyjs拥有友好的API,并鼓励使用最好的OOP和IoC实践。 

Inversifyjs被设计为允许JavaScript开发人员编写符合实体原则的代码。Inversifyjs已经开发了 3个主要目标:

允许JavaScript开发人员编写符合实体原则的代码

促进并鼓励遵守最佳OOP和IoC实践。

尽可能增加运行时开销。 

WebIDE 是函数计算团队研发的一款产品,为了解决函数计算本地环境差异和配置繁琐的问题。WebIDE 前端是 monorepo 风格的项目,即插件化构建 WebIDE 前端。插件之间存在依赖关系。构建、扩展和以及使用一个插件将是一个复杂的问题,而且对使用插件的开发人员不透明。通过使用 inversify 就能很简单的实现。通过 inversify 能很容的实现插件的构建、扩展和使用。

创建。将服务类注入到容器中

替换。通过 rebind api 可以在其他模块中从新绑定某个服务

使用。在类中通过装饰器注入需要使用的服务,服务的具体实现不需要关心,容器为我们管理

安装

由于 Inversifyjs 用到了反射来获取装饰器的相关元数据,所以需要额外安装库 reflect-Metadata

npm install inversify reflect-Metadata --save

另外,Inversifyjs 要求 Typescript >= 2.0 并且需要配置如下编译参数:

{

"compilerOptions": {

"target": "es5",

"lib": ["es6","dom"],

"types": ["reflect-Metadata"],

"module": "commonjs",

"moduleResolution": "node",

"experimentalDecorators": true,

"emitDecoratorMetadata": true

}

}

使用

步骤 1:定义接口

// file interfaces.ts

// 定义服务对象标识

export const Warrior = Symbol.for('Warrior');

export const Weapon = Symbol.for('Weapon');

export const ThrowableWeapon = Symbol.for('ThrowableWeapon');

export interface Warrior {

fight(): string;

sneak(): string;

}

export interface Weapon {

hit(): string;

}

export interface ThrowableWeapon {

throw(): string;

}

步骤 2:定义依赖

// file entities.ts

import { injectable,inject } from 'inversify';

import 'reflect-Metadata';

import { Weapon,ThrowableWeapon,Warrior } from './interfaces';

@injectable()

export class Katana implements Weapon {

public hit() {

return "cut!";

}

}

@injectable()

export class Shuriken implements ThrowableWeapon {

public throw() {

return "hit!";

}

}

@injectable()

export class Ninja implements Warrior {

public constructor(

@inject(Weapon) protected katana: Weapon,

@inject(ThrowableWeapon) protected shuriken: ThrowableWeapon

) {}

public fight() { return this.katana.hit(); }

public sneak() { return this.shuriken.throw(); }

}

步骤 3:创建并配置 IOC 容器

// file inversify.config.ts

import { Container } from "inversify";

import { Warrior,Weapon,ThrowableWeapon } from "./interfaces";

import { Ninja,Katana,Shuriken } from "./entities";

const myContainer = new Container();

myContainer.bind<Warrior>(Warrior).to(Ninja);

myContainer.bind<Weapon>(Weapon).to(Katana);

myContainer.bind<ThrowableWeapon>ThrowableWeapon).to(Shuriken);

export { myContainer };

步骤4:依赖解析

import { myContainer } from "./inversify.config";

import { Warrior } from "./interfaces";

const ninja = myContainer.get<Warrior>(Warrior);

expect(ninja.fight()).eql("cut!"); // true

expect(ninja.sneak()).eql("hit!"); // true

小结

如果你熟悉 Spring,Spring 很多特性在 Inversify 中可以找到,如果你的项目规模比较大,可以采用 monorepo 多包结构来构建项目。每一个包(模块)包含一个 ContainerModule 容器管理本模块依赖,然后在项目入口对所有的模块容器进行统一加载。

网站地址:http://inversify.io/

GitHub:https://github.com/inversify/InversifyJS

网站描述:一个强大的和轻量级控制反转容器,支持JavaScript和Node.js

InversifyJS

官方网站:http://inversify.io/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。