{"name":"inversify-inject-decorators","version":"2.0.0","description":"Lazy evaluated property injection decorators for InversifyJS","main":"lib/index.js","jsnext:main":"es/index.js","typings":"./dts/index.d.ts","directories":{"test":"test"},"scripts":{"test":"gulp","publish-please":"publish-please","prepublish":"publish-please guard"},"repository":{"type":"git","url":"git+https://github.com/inversify/inversify-inject-decorators.git"},"keywords":["inject","lazy","property","decorator","inversify","dependency","injection"],"author":{"name":"Remo H. Jansen","email":"remo.jansen@wolksoftware.com","url":"http://www.remojansen.com"},"license":"MIT","bugs":{"url":"https://github.com/inversify/inversify-inject-decorators/issues"},"homepage":"https://github.com/inversify/inversify-inject-decorators#readme","devDependencies":{"@types/chai":"^3.4.32","@types/mocha":"^2.2.31","@types/sinon":"^1.16.29","chai":"^3.5.0","gulp":"^3.9.1","gulp-istanbul":"^1.0.0","gulp-mocha":"^3.0.0","gulp-tslint":"^6.0.2","gulp-typescript":"^2.13.6","inversify":"^2.0.0","mocha":"^3.0.2","publish-please":"^2.2.0","reflect-metadata":"^0.1.3","run-sequence":"^1.2.2","tslint":"^3.14.0","typescript":"^2.0.2"},"gitHead":"0ef61ac6199edeabbddc6124b4ac4f03e193815e","_id":"inversify-inject-decorators@2.0.0","_shasum":"3ea0cef8d6d03e46da54a5b7dc7683fdc90828f1","_from":".","_npmVersion":"3.10.3","_nodeVersion":"6.5.0","_npmUser":{"name":"remojansen","email":"remo.jansen@wolksoftware.com"},"dist":{"shasum":"3ea0cef8d6d03e46da54a5b7dc7683fdc90828f1","tarball":"http://registry-npm.ywwl.com/inversify-inject-decorators/download/inversify-inject-decorators-2.0.0.tgz","integrity":"sha512-teCIr9Tr2+ndYxpYzjCg1nBAocp8K6Y669rjKfw5Wpgj1/1NI4KNiaETXfs1z4BoBvtB1Y1rEG17XIIr0nSmeg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDx3liohlMfQ5TCppJ+oABQQq7AUAc0uU+XbKlWgodk7AiEAhWDqosrHmg8dwXFoMhdtDQnxjdgH/pacW9+kB42yLTA="}],"size":4192,"noattachment":false,"key":"/inversify-inject-decorators/-/inversify-inject-decorators-2.0.0.tgz"},"maintainers":[{"name":"remojansen","email":"remo.jansen@wolksoftware.com"}],"_npmOperationalInternal":{"host":"packages-16-east.internal.npmjs.com","tmp":"tmp/inversify-inject-decorators-2.0.0.tgz_1473582713055_0.23850190686061978"},"readme":"# inversify-inject-decorators\n\n[![Join the chat at https://gitter.im/inversify/InversifyJS](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/inversify/InversifyJS?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)\n[![Build Status](https://secure.travis-ci.org/inversify/inversify-inject-decorators.svg?branch=master)](https://travis-ci.org/inversify/inversify-inject-decorators)\n[![Test Coverage](https://codeclimate.com/github/inversify/inversify-inject-decorators/badges/coverage.svg)](https://codeclimate.com/github/inversify/inversify-inject-decorators/coverage)\n[![npm version](https://badge.fury.io/js/inversify-inject-decorators.svg)](http://badge.fury.io/js/inversify-inject-decorators)\n[![Dependencies](https://david-dm.org/inversify/inversify-inject-decorators.svg)](https://david-dm.org/inversify/inversify-inject-decorators#info=dependencies)\n[![img](https://david-dm.org/inversify/inversify-inject-decorators/dev-status.svg)](https://david-dm.org/inversify/inversify-inject-decorators/#info=devDependencies)\n[![img](https://david-dm.org/inversify/inversify-inject-decorators/peer-status.svg)](https://david-dm.org/inversify/inversify-inject-decorators/#info=peerDependenciess)\n[![Known Vulnerabilities](https://snyk.io/test/github/inversify/inversify-inject-decorators/badge.svg)](https://snyk.io/test/github/inversify/inversify-inject-decorators)\n\n[![NPM](https://nodei.co/npm/inversify-inject-decorators.png?downloads=true&downloadRank=true)](https://nodei.co/npm/inversify-inject-decorators/)\n[![NPM](https://nodei.co/npm-dl/inversify-inject-decorators.png?months=9&height=3)](https://nodei.co/npm/inversify-inject-decorators/)\n\nLazy evaluated property injection decorators.\n\n## Motivation\nSome frameworks and libraries take control over the creation of instances \nof a given class. For example, React takes control over the creation of \ninstances of a given React component. This kind of frameworks and libraries \n**prevent us from being able to use constructor injection** and as a result \nthey are not easy to integrate with InversifyJS.\n\nInversifyJS also provides support for property injection but it also\nrequires the instances of a class to be created by InversifyJS.\n\nThe decorators included in this library will allow you to lazy-inject \nproperties even when the instances of a class cannot created by InversifyJS.\n\nThis library allows you to integrate InversifyJS with any library or \nframework that takes control over the creation of instances of a \ngiven class.\n\n## Installation\nYou can install `inversify-inject-decorators` using npm:\n\n```\n$ npm install inversify inversify-inject-decorators reflect-metadata --save\n```\n\nThe `inversify-inject-decorators` type definitions are included in the npm module and require TypeScript 2.0.\n\n> :warning: Please note that this library requires support for the ES6 Symbol. You can use the [es6-symbol polyfill](https://www.npmjs.com/package/es6-symbol) as a work arround.\n\nPlease refer to the [InversifyJS documentation](https://github.com/inversify/InversifyJS#installation) to learn more about the installation process.\n\n## Caching vs Non Caching Behaviour\n\nBy default, the lazy injection mechanism implemented by this will cache all requests to the underlying container.\nThis 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.\n\nTo 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.\n\n```ts\nimport { Container } from \"inversify\";\nimport getDecorators from \"inversify-inject-decorators\";\n\nconst container: Container = new Container();\nconst { lazyInject } = getDecorators(container, false);\n```\n\n## Basic property lazy-injection with `@lazyInject`\nThe following example showcases how to inject into a property \nusing the `@lazyInject` decorator:\n\n```ts\nimport getDecorators from \"inversify-inject-decorators\";\nimport { Container, injectable, tagged, named } from \"inversify\";\n\nlet container = new Container();\nlet { lazyInject } = getDecorators(container);\nlet TYPES = { Weapon: \"Weapon\" };\n\ninterface Weapon {\n    name: string;\n    durability: number;\n    use(): void;\n}\n\n@injectable()\nclass Sword implements Weapon {\n    public name: string;\n    public durability: number;\n    public constructor() {\n        this.durability = 100;\n        this.name = \"Sword\";\n    }\n    public use() {\n        this.durability = this.durability - 10;\n    }\n}\n\nclass Warrior {\n    @lazyInject(TYPES.Weapon)\n    public weapon: Weapon;\n}\n\ncontainer.bind<Weapon>(TYPES.Weapon).to(Sword);\n\nlet warrior = new Warrior();\nconsole.log(warrior.weapon instanceof Sword); // true\n```\n\n## Named property injection with `@lazyInjectNamed`\nThe following example showcases how to inject into a named property \nusing the `@lazyInjectNamed` decorator:\n\n```ts\nimport getDecorators from \"inversify-inject-decorators\";\nimport { Container, injectable, named } from \"inversify\";\n\nlet container = new Container();\nlet { lazyInjectNamed } = getDecorators(container);\nlet TYPES = { Weapon: \"Weapon\" };\n\ninterface Weapon {\n    name: string;\n    durability: number;\n    use(): void;\n}\n\n@injectable()\nclass Sword implements Weapon {\n    public name: string;\n    public durability: number;\n    public constructor() {\n        this.durability = 100;\n        this.name = \"Sword\";\n    }\n    public use() {\n        this.durability = this.durability - 10;\n    }\n}\n\n@injectable()\nclass Shuriken implements Weapon {\n    public name: string;\n    public durability: number;\n    public constructor() {\n        this.durability = 100;\n        this.name = \"Shuriken\";\n    }\n    public use() {\n        this.durability = this.durability - 10;\n    }\n}\n\nclass Warrior {\n\n    @lazyInjectNamed(TYPES.Weapon, \"not-throwwable\")\n    @named(\"not-throwwable\")\n    public primaryWeapon: Weapon;\n\n    @lazyInjectNamed(TYPES.Weapon, \"throwwable\")\n    @named(\"throwwable\")\n    public secondaryWeapon: Weapon;\n\n}\n\ncontainer.bind<Weapon>(TYPES.Weapon).to(Sword).whenTargetNamed(\"not-throwwable\");\ncontainer.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetNamed(\"throwwable\");\n\nlet warrior = new Warrior();\nconsole.log(warrior.primaryWeapon instanceof Sword); // true\nconsole.log(warrior.primaryWeapon instanceof Shuriken); // true\n```\n\n## Tagged property injection with `@lazyInjectTagged`\nThe following example showcases how to inject a tagged property \nusing the `@lazyInjectTagged` decorator:\n\n```ts\nimport getDecorators from \"inversify-inject-decorators\";\nimport { Container, injectable, tagged } from \"inversify\";\n\nlet container = new Container();\nlet { lazyInjectTagged } = getDecorators(container);\nlet TYPES = { Weapon: \"Weapon\" };\n\ninterface Weapon {\n    name: string;\n    durability: number;\n    use(): void;\n}\n\n@injectable()\nclass Sword implements Weapon {\n    public name: string;\n    public durability: number;\n    public constructor() {\n        this.durability = 100;\n        this.name = \"Sword\";\n    }\n    public use() {\n        this.durability = this.durability - 10;\n    }\n}\n\n@injectable()\nclass Shuriken implements Weapon {\n    public name: string;\n    public durability: number;\n    public constructor() {\n        this.durability = 100;\n        this.name = \"Shuriken\";\n    }\n    public use() {\n        this.durability = this.durability - 10;\n    }\n}\n\nclass Warrior {\n\n    @lazyInjectTagged(TYPES.Weapon, \"throwwable\", false)\n    @tagged(\"throwwable\", false)\n    public primaryWeapon: Weapon;\n\n    @lazyInjectTagged(TYPES.Weapon, \"throwwable\", true)\n    @tagged(\"throwwable\", true)\n    public secondaryWeapon: Weapon;\n\n}\n\ncontainer.bind<Weapon>(TYPES.Weapon).to(Sword).whenTargetTagged(\"throwwable\", false);\ncontainer.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetTagged(\"throwwable\", true);\n\nlet warrior = new Warrior();\nconsole.log(warrior.primaryWeapon instanceof Sword); // true\nconsole.log(warrior.primaryWeapon instanceof Shuriken); // true\n```\n\n## Multi-injection into a property with `@lazyMultiInject`\nThe following example showcases how to multi-inject a property \nusing the `@lazyMultiInject` decorator:\n\n```ts\nimport getDecorators from \"inversify-inject-decorators\";\nimport { Container, injectable } from \"inversify\";\n\nlet container = new Container();\nlet { lazyMultiInject } = getDecorators(container);\nlet TYPES = { Weapon: \"Weapon\" };\n\ninterface Weapon {\n    name: string;\n    durability: number;\n    use(): void;\n}\n\n@injectable()\nclass Sword implements Weapon {\n    public name: string;\n    public durability: number;\n    public constructor() {\n        this.durability = 100;\n        this.name = \"Sword\";\n    }\n    public use() {\n        this.durability = this.durability - 10;\n    }\n}\n\n@injectable()\nclass Shuriken implements Weapon {\n    public name: string;\n    public durability: number;\n    public constructor() {\n        this.durability = 100;\n        this.name = \"Shuriken\";\n    }\n    public use() {\n        this.durability = this.durability - 10;\n    }\n}\n\nclass Warrior {\n\n    @lazyMultiInject(TYPES.Weapon)\n    public weapons: Weapon[];\n\n}\n\ncontainer.bind<Weapon>(TYPES.Weapon).to(Sword);\ncontainer.bind<Weapon>(TYPES.Weapon).to(Shuriken);\n\nlet warrior = new Warrior();\nconsole.log(warrior.weapons[0] instanceof Sword); // true\nconsole.log(warrior.weapons[1] instanceof Shuriken); // true\n```\n","publish_time":1473582714811,"_cnpm_publish_time":1473582714811,"dist-tags":{"latest":"3.1.0"}}