• inversify 强大&&轻量级的基于typescript 的ioc 框架


    inversify 强大&&轻量级的基于typescript 的ioc 框架,以前有介绍过一个typedi 的类似框架
    inversify github 的star比typedi 多很多

    参考使用

    • 配置tsconfig.json
     
    {
        "compilerOptions": {
            "target": "es5",
            "lib": ["es6"],
            "types": ["reflect-metadata"],
            "module": "commonjs",
            "moduleResolution": "node",
            "experimentalDecorators": true,
            "emitDecoratorMetadata": true
        }
    }
    • 创建接口
    // file interfaces.ts
    export interface Warrior {
        fight(): string;
        sneak(): string;
    }
    export interface Weapon {
        hit(): string;
    }
    export interface ThrowableWeapon {
        throw(): string;
    }
     
     
    • 创建类型
    // file types.ts
    const TYPES = {
        Warrior: Symbol.for("Warrior"),
        Weapon: Symbol.for("Weapon"),
        ThrowableWeapon: Symbol.for("ThrowableWeapon")
    };
    export { TYPES };
    • 创建实体
    // file entities.ts
    import { injectable, inject } from "inversify";
    import "reflect-metadata";
    import { Weapon, ThrowableWeapon, Warrior } from "./interfaces";
    import { TYPES } from "./types";
    @injectable()
    class Katana implements Weapon {
        public hit() {
            return "cut!";
        }
    }
    @injectable()
    class Shuriken implements ThrowableWeapon {
        public throw() {
            return "hit!";
        }
    }
    @injectable()
    class Ninja implements Warrior {
        private _katana: Weapon;
        private _shuriken: ThrowableWeapon;
        public constructor(
          @inject(TYPES.Weapon) katana: Weapon,
          @inject(TYPES.ThrowableWeapon) shuriken: ThrowableWeapon
        ) {
            this._katana = katana;
            this._shuriken = shuriken;
        }
        public fight() { return this._katana.hit(); }
        public sneak() { return this._shuriken.throw(); }
    }
    export { Ninja, Katana, Shuriken };
     
     
    • 配置容器
    // file inversify.config.ts
    import { Container } from "inversify";
    import { TYPES } from "./types";
    import { Warrior, Weapon, ThrowableWeapon } from "./interfaces";
    import { Ninja, Katana, Shuriken } from "./entities";
    const myContainer = new Container();
    myContainer.bind<Warrior>(TYPES.Warrior).to(Ninja);
    myContainer.bind<Weapon>(TYPES.Weapon).to(Katana);
    myContainer.bind<ThrowableWeapon>(TYPES.ThrowableWeapon).to(Shuriken);
    export { myContainer };
     
     
    • 依赖解析
    import { myContainer } from "./inversify.config";
    import { TYPES } from "./types";
    import { Warrior } from "./interfaces";
    const ninja = myContainer.get<Warrior>(TYPES.Warrior);
    console.log(ninja.fight()); 
    console.log(ninja.sneak())

    说明

    inversify 是一个不错的ioc 可选类库

    参考资料

    https://github.com/typestack/typedi
    https://github.com/inversify/InversifyJS
    https://github.com/rongfengliang/inversify-lerning

  • 相关阅读:
    Simple DirectMedia Layer常用API总结
    [游戏复刻] Super Mario Brothers(1985. Famicom)
    [游戏复刻] 2048(2014. Android)
    图的结构以及寻路算法的c实现
    散列查找的C实现
    【游戏编程从0开始】一、基本结构
    C++中const关键字用法总结
    C标准库常用函数概要
    字符串表达式计算器的设计
    初探数据结构
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/12879488.html
Copyright © 2020-2023  润新知