Lazy evaluated property injection decorators.
Some frameworks and libraries take control over the creation of instances of a given class. For example, React takes control over the creation of instances of a given React component. This kind of frameworks and libraries prevent us from being able to use constructor injection and as a result they are not easy to integrate with InversifyJS.
InversifyJS also provides support for property injection but it also requires the instances of a class to be created by InversifyJS.
The decorators included in this library will allow you to lazy-inject properties even when the instances of a class cannot created by InversifyJS.
This library allows you to integrate InversifyJS with any library or framework that takes control over the creation of instances of a given class.
You can install
inversify-inject-decorators using npm:
$ npm install inversify inversify-inject-decorators reflect-metadata --save
inversify-inject-decorators type definitions are included in the npm module and require TypeScript 2.0.
⚠️ Please note that this library requires support for the ES6 Symbol. You can use the es6-symbol polyfill as a work arround.
Please refer to the InversifyJS documentation to learn more about the installation process.
Caching vs Non Caching Behaviour
By default, the lazy injection mechanism implemented by this will cache all requests to the underlying container. This means that rebinding or unbinding services to/from service identifiers will not be reflected in the instances into which these services have been injected into. The same holds true for scenarios where you dynamically load/unload container modules and thus either add or remove bindings from your container.
To overcome this limitation, one can now pass an additional boolean parameter to
getDecorators(container: Container, doCache = true). When set to
false, services resolved from the container will no longer be cached and will always be resolved from the container directly, e.g.
Basic property lazy-injection with
The following example showcases how to inject into a property
;;;;;container.bindTYPES.Weapon.toSword;;console.logwarrior.weapon instanceof Sword; // true
Named property injection with
The following example showcases how to inject into a named property
;;;;;container.bindTYPES.Weapon.toSword.whenTargetNamed"not-throwwable";container.bindTYPES.Weapon.toShuriken.whenTargetNamed"throwwable";;console.logwarrior.primaryWeapon instanceof Sword; // trueconsole.logwarrior.primaryWeapon instanceof Shuriken; // true
Tagged property injection with
The following example showcases how to inject a tagged property
;;;;;container.bindTYPES.Weapon.toSword.whenTargetTagged"throwwable", false;container.bindTYPES.Weapon.toShuriken.whenTargetTagged"throwwable", true;;console.logwarrior.primaryWeapon instanceof Sword; // trueconsole.logwarrior.primaryWeapon instanceof Shuriken; // true
Multi-injection into a property with
The following example showcases how to multi-inject a property
;;;;;container.bindTYPES.Weapon.toSword;container.bindTYPES.Weapon.toShuriken;;console.logwarrior.weapons instanceof Sword; // trueconsole.logwarrior.weapons instanceof Shuriken; // true