From 935e0c7a782959f990479f7bc2ded1259a1fcbde Mon Sep 17 00:00:00 2001 From: "Remo H. Jansen" Date: Thu, 11 Aug 2016 18:34:05 +0100 Subject: [PATCH 1/3] Create inversify-inject-decorators.d.ts --- .../inversify-inject-decorators.d.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 inversify-inject-decorators/inversify-inject-decorators.d.ts diff --git a/inversify-inject-decorators/inversify-inject-decorators.d.ts b/inversify-inject-decorators/inversify-inject-decorators.d.ts new file mode 100644 index 0000000000..da567815e9 --- /dev/null +++ b/inversify-inject-decorators/inversify-inject-decorators.d.ts @@ -0,0 +1,32 @@ +// Type definitions for inversify-inject-decorators 1.0.0-beta.1 +// Project: https://github.com/inversify/inversify-inject-decorators +// Definitions by: inversify +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare namespace inversifyInjectDecorators { + + interface InjectDecorators { + + lazyInject: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable)) => + (proto: any, key: string) => void; + + lazyInjectNamed: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable), named: string) => + (proto: any, key: string) => void; + + lazyInjectTagged: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable), key: string, value: any) => + (proto: any, propertyName: string) => void; + + lazyMultiInject: (serviceIdentifier: (string|Symbol|inversify.interfaces.Newable)) => + (proto: any, key: string) => void; + + } + + export default function getDecorators(kernel: inversify.interfaces.Kernel): InjectDecorators; + +} + +declare module "inversify-inject-decorators" { + export = inversifyInjectDecorators; +} From 4ee6064b2a017bff9d522e30c4b33b9f02d9fa8c Mon Sep 17 00:00:00 2001 From: "Remo H. Jansen" Date: Thu, 11 Aug 2016 18:34:56 +0100 Subject: [PATCH 2/3] Create inversify-inject-decorators-tests.ts --- .../inversify-inject-decorators-tests.ts | 214 ++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 inversify-inject-decorators/inversify-inject-decorators-tests.ts diff --git a/inversify-inject-decorators/inversify-inject-decorators-tests.ts b/inversify-inject-decorators/inversify-inject-decorators-tests.ts new file mode 100644 index 0000000000..468bbaf763 --- /dev/null +++ b/inversify-inject-decorators/inversify-inject-decorators-tests.ts @@ -0,0 +1,214 @@ +/// +/// + +import getDecorators from "inversify-inject-decorators"; +import { Kernel, injectable, tagged, named } from "inversify"; + +module lazyInject { + + let kernel = new Kernel(); + let { lazyInject } = getDecorators(kernel); + let TYPES = { Weapon: "Weapon" }; + + interface Weapon { + name: string; + durability: number; + use(): void; + } + + @injectable() + class Sword implements Weapon { + public name: string; + public durability: number; + public constructor() { + this.durability = 100; + this.name = "Sword"; + } + public use() { + this.durability = this.durability - 10; + } + } + + class Warrior { + @lazyInject(TYPES.Weapon) + public weapon: Weapon; + } + + kernel.bind(TYPES.Weapon).to(Sword); + + let warrior = new Warrior(); + console.log(warrior.weapon instanceof Sword); // true + +} + +module lazyInjectNamed { + + let kernel = new Kernel(); + let { lazyInjectNamed } = getDecorators(kernel); + let TYPES = { Weapon: "Weapon" }; + + interface Weapon { + name: string; + durability: number; + use(): void; + } + + @injectable() + class Sword implements Weapon { + public name: string; + public durability: number; + public constructor() { + this.durability = 100; + this.name = "Sword"; + } + public use() { + this.durability = this.durability - 10; + } + } + + @injectable() + class Shuriken implements Weapon { + public name: string; + public durability: number; + public constructor() { + this.durability = 100; + this.name = "Shuriken"; + } + public use() { + this.durability = this.durability - 10; + } + } + + class Warrior { + + @lazyInjectNamed(TYPES.Weapon, "not-throwwable") + @named("not-throwwable") + public primaryWeapon: Weapon; + + @lazyInjectNamed(TYPES.Weapon, "throwwable") + @named("throwwable") + public secondaryWeapon: Weapon; + + } + + kernel.bind(TYPES.Weapon).to(Sword).whenTargetNamed("not-throwwable"); + kernel.bind(TYPES.Weapon).to(Shuriken).whenTargetNamed("throwwable"); + + let warrior = new Warrior(); + console.log(warrior.primaryWeapon instanceof Sword); // true + console.log(warrior.primaryWeapon instanceof Shuriken); // true + +} + +module lazyInjectTagged { + + let kernel = new Kernel(); + let { lazyInjectTagged } = getDecorators(kernel); + let TYPES = { Weapon: "Weapon" }; + + interface Weapon { + name: string; + durability: number; + use(): void; + } + + @injectable() + class Sword implements Weapon { + public name: string; + public durability: number; + public constructor() { + this.durability = 100; + this.name = "Sword"; + } + public use() { + this.durability = this.durability - 10; + } + } + + @injectable() + class Shuriken implements Weapon { + public name: string; + public durability: number; + public constructor() { + this.durability = 100; + this.name = "Shuriken"; + } + public use() { + this.durability = this.durability - 10; + } + } + + class Warrior { + + @lazyInjectTagged(TYPES.Weapon, "throwwable", false) + @tagged("throwwable", false) + public primaryWeapon: Weapon; + + @lazyInjectTagged(TYPES.Weapon, "throwwable", true) + @tagged("throwwable", true) + public secondaryWeapon: Weapon; + + } + + kernel.bind(TYPES.Weapon).to(Sword).whenTargetTagged("throwwable", false); + kernel.bind(TYPES.Weapon).to(Shuriken).whenTargetTagged("throwwable", true); + + let warrior = new Warrior(); + console.log(warrior.primaryWeapon instanceof Sword); // true + console.log(warrior.primaryWeapon instanceof Shuriken); // true + +} + +module lazyMultiInject { + + let kernel = new Kernel(); + let { lazyMultiInject } = getDecorators(kernel); + let TYPES = { Weapon: "Weapon" }; + + interface Weapon { + name: string; + durability: number; + use(): void; + } + + @injectable() + class Sword implements Weapon { + public name: string; + public durability: number; + public constructor() { + this.durability = 100; + this.name = "Sword"; + } + public use() { + this.durability = this.durability - 10; + } + } + + @injectable() + class Shuriken implements Weapon { + public name: string; + public durability: number; + public constructor() { + this.durability = 100; + this.name = "Shuriken"; + } + public use() { + this.durability = this.durability - 10; + } + } + + class Warrior { + + @lazyMultiInject(TYPES.Weapon) + public weapons: Weapon[]; + + } + + kernel.bind(TYPES.Weapon).to(Sword); + kernel.bind(TYPES.Weapon).to(Shuriken); + + let warrior = new Warrior(); + console.log(warrior.weapons[0] instanceof Sword); // true + console.log(warrior.weapons[1] instanceof Shuriken); // true + +} From c27a53475311ffc7fa463f4124c7f11237a0997b Mon Sep 17 00:00:00 2001 From: "Remo H. Jansen" Date: Thu, 11 Aug 2016 18:47:12 +0100 Subject: [PATCH 3/3] Update inversify-inject-decorators.d.ts --- inversify-inject-decorators/inversify-inject-decorators.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inversify-inject-decorators/inversify-inject-decorators.d.ts b/inversify-inject-decorators/inversify-inject-decorators.d.ts index da567815e9..032ed40cdd 100644 --- a/inversify-inject-decorators/inversify-inject-decorators.d.ts +++ b/inversify-inject-decorators/inversify-inject-decorators.d.ts @@ -23,10 +23,10 @@ declare namespace inversifyInjectDecorators { } - export default function getDecorators(kernel: inversify.interfaces.Kernel): InjectDecorators; + export function getDecorators(kernel: inversify.interfaces.Kernel): InjectDecorators; } declare module "inversify-inject-decorators" { - export = inversifyInjectDecorators; + export default inversifyInjectDecorators.getDecorators; }