1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        InversifyJS輕量級(jí)反轉(zhuǎn)控件容器

        聯(lián)合創(chuàng)作 · 2023-09-30 06:53

        InversifyJS 一個(gè)強(qiáng)大而輕量級(jí)的反轉(zhuǎn)控件 (IoC) 容器,適用于由TypeScript編寫(xiě)的JavaScript和Node.js應(yīng)用。它使用類構(gòu)造函數(shù)去定義和注入它的依賴。InversifyJS API友好易懂,鼓勵(lì)對(duì)OOP和IoC最佳實(shí)踐的應(yīng)用。

        InversifyJS有4個(gè)主要目標(biāo):

        1. 允許JavaScript開(kāi)發(fā)人員編寫(xiě)遵循 SOLID 原則的代碼。

        2. 促進(jìn)并鼓勵(lì)遵守最佳的面向?qū)ο缶幊毯鸵蕾囎⑷雽?shí)踐。

        3. 盡可能少的運(yùn)行時(shí)開(kāi)銷。

        4. 提供藝術(shù)編程體驗(yàn)和生態(tài)。

        使用

        步驟 1:定義接口

        // file interfaces.ts
        
        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";
        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 };

        步驟 3:創(chuàng)建并配置 IOC 容器

        // 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 };

        步驟4:依賴解析

        import { myContainer } from "./inversify.config";
        import { TYPES } from "./types";
        import { Warrior } from "./interfaces";
        
        const ninja = myContainer.get<Warrior>(TYPES.Warrior);
        
        expect(ninja.fight()).eql("cut!"); // true
        expect(ninja.sneak()).eql("hit!"); // true
        瀏覽 26
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        編輯 分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        編輯 分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            男女羞羞无遮挡 | A片视频免费观看 | 国产99久久久国产精品~~牛 | 男人添女人高潮免费视频试看 | 精品热九九 | 丁香婷婷五月激情综合深爱 | 国产1级片 | 大尺度做爰陈伟霆 | 一本大道东京热无码 | 在线免费观看成人 |