Lazy evaluated property injection decorators
Installations
npm install inversify-inject-decorators
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
8.1.0
NPM Version
5.5.1
Score
99.5
Supply Chain
100
Quality
80.5
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (79.46%)
JavaScript (20.54%)
Developer
inversify
Download Statistics
Total Downloads
15,217,604
Last Day
4,006
Last Week
50,540
Last Month
296,903
Last Year
3,863,377
GitHub Statistics
145 Stars
300 Commits
14 Forks
12 Watching
72 Branches
15 Contributors
Package Meta Information
Latest Version
3.1.0
Package Id
inversify-inject-decorators@3.1.0
Size
6.07 kB
NPM Version
5.5.1
Node Version
8.1.0
Publised On
07 Jan 2018
Total Downloads
Cumulative downloads
Total Downloads
15,217,604
Last day
-75%
4,006
Compared to previous day
Last week
-29.7%
50,540
Compared to previous week
Last month
-4.6%
296,903
Compared to previous month
Last year
-11.3%
3,863,377
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
inversify-inject-decorators
Lazy evaluated property injection decorators.
Motivation
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.
Installation
You can install inversify-inject-decorators
using npm:
$ npm install inversify inversify-inject-decorators reflect-metadata --save
The inversify-inject-decorators
type definitions are included in the npm module and require TypeScript 2.0.
:warning: 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.
1import { Container } from "inversify"; 2import getDecorators from "inversify-inject-decorators"; 3 4const container: Container = new Container(); 5const { lazyInject } = getDecorators(container, false);
Basic property lazy-injection with @lazyInject
The following example showcases how to inject into a property
using the @lazyInject
decorator:
1import getDecorators from "inversify-inject-decorators"; 2import { Container, injectable, tagged, named } from "inversify"; 3 4let container = new Container(); 5let { lazyInject } = getDecorators(container); 6let TYPES = { Weapon: "Weapon" }; 7 8interface Weapon { 9 name: string; 10 durability: number; 11 use(): void; 12} 13 14@injectable() 15class Sword implements Weapon { 16 public name: string; 17 public durability: number; 18 public constructor() { 19 this.durability = 100; 20 this.name = "Sword"; 21 } 22 public use() { 23 this.durability = this.durability - 10; 24 } 25} 26 27class Warrior { 28 @lazyInject(TYPES.Weapon) 29 public weapon: Weapon; 30} 31 32container.bind<Weapon>(TYPES.Weapon).to(Sword); 33 34let warrior = new Warrior(); 35console.log(warrior.weapon instanceof Sword); // true
Named property injection with @lazyInjectNamed
The following example showcases how to inject into a named property
using the @lazyInjectNamed
decorator:
1import getDecorators from "inversify-inject-decorators"; 2import { Container, injectable, named } from "inversify"; 3 4let container = new Container(); 5let { lazyInjectNamed } = getDecorators(container); 6let TYPES = { Weapon: "Weapon" }; 7 8interface Weapon { 9 name: string; 10 durability: number; 11 use(): void; 12} 13 14@injectable() 15class Sword implements Weapon { 16 public name: string; 17 public durability: number; 18 public constructor() { 19 this.durability = 100; 20 this.name = "Sword"; 21 } 22 public use() { 23 this.durability = this.durability - 10; 24 } 25} 26 27@injectable() 28class Shuriken implements Weapon { 29 public name: string; 30 public durability: number; 31 public constructor() { 32 this.durability = 100; 33 this.name = "Shuriken"; 34 } 35 public use() { 36 this.durability = this.durability - 10; 37 } 38} 39 40class Warrior { 41 42 @lazyInjectNamed(TYPES.Weapon, "not-throwwable") 43 @named("not-throwwable") 44 public primaryWeapon: Weapon; 45 46 @lazyInjectNamed(TYPES.Weapon, "throwwable") 47 @named("throwwable") 48 public secondaryWeapon: Weapon; 49 50} 51 52container.bind<Weapon>(TYPES.Weapon).to(Sword).whenTargetNamed("not-throwwable"); 53container.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetNamed("throwwable"); 54 55let warrior = new Warrior(); 56console.log(warrior.primaryWeapon instanceof Sword); // true 57console.log(warrior.primaryWeapon instanceof Shuriken); // true
Tagged property injection with @lazyInjectTagged
The following example showcases how to inject a tagged property
using the @lazyInjectTagged
decorator:
1import getDecorators from "inversify-inject-decorators"; 2import { Container, injectable, tagged } from "inversify"; 3 4let container = new Container(); 5let { lazyInjectTagged } = getDecorators(container); 6let TYPES = { Weapon: "Weapon" }; 7 8interface Weapon { 9 name: string; 10 durability: number; 11 use(): void; 12} 13 14@injectable() 15class Sword implements Weapon { 16 public name: string; 17 public durability: number; 18 public constructor() { 19 this.durability = 100; 20 this.name = "Sword"; 21 } 22 public use() { 23 this.durability = this.durability - 10; 24 } 25} 26 27@injectable() 28class Shuriken implements Weapon { 29 public name: string; 30 public durability: number; 31 public constructor() { 32 this.durability = 100; 33 this.name = "Shuriken"; 34 } 35 public use() { 36 this.durability = this.durability - 10; 37 } 38} 39 40class Warrior { 41 42 @lazyInjectTagged(TYPES.Weapon, "throwwable", false) 43 @tagged("throwwable", false) 44 public primaryWeapon: Weapon; 45 46 @lazyInjectTagged(TYPES.Weapon, "throwwable", true) 47 @tagged("throwwable", true) 48 public secondaryWeapon: Weapon; 49 50} 51 52container.bind<Weapon>(TYPES.Weapon).to(Sword).whenTargetTagged("throwwable", false); 53container.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetTagged("throwwable", true); 54 55let warrior = new Warrior(); 56console.log(warrior.primaryWeapon instanceof Sword); // true 57console.log(warrior.primaryWeapon instanceof Shuriken); // true
Multi-injection into a property with @lazyMultiInject
The following example showcases how to multi-inject a property
using the @lazyMultiInject
decorator:
1import getDecorators from "inversify-inject-decorators"; 2import { Container, injectable } from "inversify"; 3 4let container = new Container(); 5let { lazyMultiInject } = getDecorators(container); 6let TYPES = { Weapon: "Weapon" }; 7 8interface Weapon { 9 name: string; 10 durability: number; 11 use(): void; 12} 13 14@injectable() 15class Sword implements Weapon { 16 public name: string; 17 public durability: number; 18 public constructor() { 19 this.durability = 100; 20 this.name = "Sword"; 21 } 22 public use() { 23 this.durability = this.durability - 10; 24 } 25} 26 27@injectable() 28class Shuriken implements Weapon { 29 public name: string; 30 public durability: number; 31 public constructor() { 32 this.durability = 100; 33 this.name = "Shuriken"; 34 } 35 public use() { 36 this.durability = this.durability - 10; 37 } 38} 39 40class Warrior { 41 42 @lazyMultiInject(TYPES.Weapon) 43 public weapons: Weapon[]; 44 45} 46 47container.bind<Weapon>(TYPES.Weapon).to(Sword); 48container.bind<Weapon>(TYPES.Weapon).to(Shuriken); 49 50let warrior = new Warrior(); 51console.log(warrior.weapons[0] instanceof Sword); // true 52console.log(warrior.weapons[1] instanceof Shuriken); // true
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/5 approved changesets -- score normalized to 0
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
3.5
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More