【问题标题】:InversifyJS Injecting Literal Constructor ParametersInversifyJS 注入文字构造函数参数
【发布时间】:2018-03-01 13:37:36
【问题描述】:

是否可以使用 InversifyJS 获得以下行为:

constructor(IDependency resolvedDependency, string myLiteral)
                        ^                          ^
                Automatically resolve      Defined Literal

如果是这样,最好的方法是什么?

【问题讨论】:

    标签: typescript inversifyjs


    【解决方案1】:

    您可以做一些事情。在这里,我将发布其中的一些...

    将文字作为常量值注入

    let TYPES = {
        IWarrior: Symbol("IWarrior"),
        IWeapon: Symbol("IWeapon"),
        rank: Symbol("rank")
    }
    
    interface IWeapon {}
    
    @injectable()
    class Katana implements IWeapon {}
    
    interface IWarrior {
        weapon: IWeapon;
        rank: string;
    }
    
    @injectable()
    class Warrior implements IWarrior {
        public weapon: IWeapon;
        public rank: string;
        public constructor(
            @inject(TYPES.IWeapon) weapon: IWeapon,
            @inject(TYPES.rank) rank: string
        ) {
            this.weapon = weapon;
            this.rank = rank;
        }
    }
    
    let kernel = new Kernel();
    kernel.bind<IWarrior>(TYPES.IWarrior).to(Warrior);
    kernel.bind<IWeapon>(TYPES.IWeapon).to(Katana);
    kernel.bind<string>(TYPES.rank).toConstantValue("master");
    

    根据上下文注入文字

    使用上下文约束,您可以为特定上下文注入常量值:

    let kernel = new Kernel();
    kernel.bind<IWarrior>(TYPES.IWarrior).to(Warrior);
    kernel.bind<IWeapon>(TYPES.IWeapon).to(Katana);
    
    kernel.bind<string>(TYPES.rank)
        .toConstantValue("master")
        .whenTargetTagged("rank", "master");
    
    kernel.bind<string>(TYPES.rank)
          .toConstantValue("student")
          .whenTargetTagged("rank", "student");
    
    let master = kernel.getTagged(TYPES.IWarrior, "rank", "master");
    let student = kernel.getTagged(TYPES.IWarrior, "rank", "student");
    

    注入工厂

    let TYPES = {
        IWarrior: Symbol("IWarrior"),
        IWeapon: Symbol("IWeapon"),
        IFactoryOfIWarrior: Symbol("IFactory<IWarrior>")
    }
    
    interface IWeapon {}
    
    @injectable()
    class Katana implements IWeapon {}
    
    interface IWarrior {
        weapon: IWeapon;
        rank: string;
    }
    
    @injectable()
    class Warrior implements IWarrior {
        public weapon: IWeapon;
        public rank: string;
        public constructor(
            @inject(TYPES.IWeapon) weapon: IWeapon
        ) {
            this.weapon = weapon;
            this.rank = null; // important!
        }
    }
    
    let kernel = new Kernel();
    kernel.bind<IWarrior>(TYPES.IWarrior).to(Warrior);
    kernel.bind<IWeapon>(TYPES.IWeapon).to(Katana);
    
    kernel.bind<inversify.IFactory<IWarrior>>(TYPES.IFactoryOfIWarrior)
        .toFactory<IWarrior>((context) => {
            return (rank: string) => {
                let warrior = context.kernel.get<IWarrior>(TYPES.IWarrior);
                warrior.rank = rank;
                return warrior;
            };
        });
    
    let warriorFactory = kernel.get<inversify.IFactory<IWarrior>>(TYPES.IFactoryOfIWarrior);
    
    let master = warriorFactory("master");
    let student = warriorFactory("student");
    

    您可以将工厂注入其他类:

    @injectable()
    class Army {
        private _warriorFactory: (rank: string) => IWarrior;
        private _soldiers: IWarrior[];
        public constructor(
            @inject(TYPES.IFactoryOfIWarrior) warriorFactory: (rank: string) => IWarrior
        ) {
            this._warriorFactory = warriorFactory;
            this._soldiers = [];
        }
        public newRecruit(rank: string) {
            this._soldiers.push(this._warriorFactory(rank));
        }
    }
    

    我认为最好的解决方案是使用工厂。

    【讨论】:

    • context.kernel.get 在 5.x 版本中不存在。你必须使用context.container
    猜你喜欢
    • 2018-08-06
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多