Rem*_*sen 14

toConstructor

如果使用a,toConstructor您将能够将参数传递给构造函数,但是您将无法解析这些参数(除非您也注入它们).

container.bind<interfaces.Newable<Katana>>("Newable<Katana>")
         .toConstructor<Katana>(Katana);
Run Code Online (Sandbox Code Playgroud)

toDynamicValue

如果你使用a,toDynamicValue你将能够将构造函数参数传递给构造函数并使用context.

container.bind<Katana>("Katana")
        .toDynamicValue((context: interfaces.Context) => {
             return new Katana(context.container.get("SomeDependency")); 
        });
Run Code Online (Sandbox Code Playgroud)

toFactory

如果使用a,toFactory您将能够将构造函数参数传递给构造函数并使用它来解析这些参数,context但您也可以根据工厂参数生成不同的输出.

container.bind<interfaces.Factory<Weapon>>("Factory<Weapon>")
         .toFactory<Weapon>((context: interfaces.Context) => {
             return (throwable: boolean) => {
                 if (throwable) {
                     return context.container.getTagged<Weapon>(
                         "Weapon", "throwable", true
                     );
                 } else {
                     return context.container.getTagged<Weapon>(
                         "Weapon", "throwable", false
                     );
                 }
             };
         });
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢您清晰明了的解释。还要感谢您在 inversify 方面所做的所有出色工作。 (2认同)