{"name":"inversify-inject-decorators","version":"3.0.2","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":"4.0.4","@types/mocha":"2.2.44","@types/sinon":"2.3.7","chai":"4.1.2","gulp":"3.9.1","gulp-istanbul":"1.1.2","gulp-mocha":"3.0.1","gulp-tslint":"8.1.2","gulp-typescript":"3.2.3","inversify":"4.5.1","mocha":"4.0.1","publish-please":"2.3.1","reflect-metadata":"0.1.10","run-sequence":"2.2.0","tslint":"5.8.0","typescript":"2.6.1"},"gitHead":"deaf9d2b642835033f9923021e7053f81d9c57ef","_id":"inversify-inject-decorators@3.0.2","_npmVersion":"5.5.1","_nodeVersion":"8.1.0","_npmUser":{"name":"remojansen","email":"remo.jansen@wolksoftware.com"},"dist":{"integrity":"sha512-kM7lR4kdQPWUkKf8CpzgxksZwBnlbAiCyuIF5cCa2tblzGuVUJ27C5byDN5mOyUGUKzFD0W1Abw3R07NL7/zAA==","shasum":"9817e1f0212b9098d476d502449bd807305ca4e6","tarball":"http://registry-npm.ywwl.com/inversify-inject-decorators/download/inversify-inject-decorators-3.0.2.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDE5O/yby0qdnxP/jWRs6wXxeu18OZQA/wII/CXeq0rbAiBhPxcFyDpnGsglplC18YWOsK2Zljl0uVzFyLeEsRY/Qg=="}],"size":4397,"noattachment":false,"key":"/inversify-inject-decorators/-/inversify-inject-decorators-3.0.2.tgz"},"maintainers":[{"name":"remojansen","email":"remo.jansen@wolksoftware.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/inversify-inject-decorators-3.0.2.tgz_1509831515443_0.5188761868048459"},"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":1509831516455,"_cnpm_publish_time":1509831516455,"dist-tags":{"latest":"3.1.0"}}