diff --git a/inversify/inversify-tests.ts b/inversify/inversify-tests.ts new file mode 100644 index 000000000..46b5cab97 --- /dev/null +++ b/inversify/inversify-tests.ts @@ -0,0 +1,75 @@ +/// + +interface FooInterface { + name : string; + greet() : string; +} + +interface BarInterface { + name : string; + greet() : string; +} + +interface FooBarInterface { + foo : FooInterface; + bar : BarInterface; + greet() : string; +} + +class Foo implements FooInterface { + public name : string; + constructor() { + this.name = "foo"; + } + public greet() : string { + return this.name; + } +} + +class Bar implements BarInterface { + public name : string; + constructor() { + this.name = "bar"; + } + public greet() : string { + return this.name; + } +} + +class FooBar implements FooBarInterface { + public foo : FooInterface; + public bar : BarInterface; + constructor(FooInterface : FooInterface, BarInterface : BarInterface) { + this.foo = FooInterface; + this.bar = BarInterface; + } + public greet() : string{ + return this.foo.greet() + this.bar.greet(); + } +} + +// Kernel +var kernel = new inversify.Kernel(); + +// Identifiers +var fooRuntimeIdentifier = "FooInterface"; +var barRuntimeIdentifier = "BarInterface"; +var fooBarRuntimeIdentifier = "FooBarInterface"; + +// Bindings +var fooBinding = new inversify.TypeBinding(fooRuntimeIdentifier, Foo); +var barBinding = new inversify.TypeBinding(barRuntimeIdentifier, Bar); +var fooBarBinding = new inversify.TypeBinding(fooBarRuntimeIdentifier, FooBar); + +kernel.bind(fooBinding); +kernel.bind(barBinding); +kernel.bind(fooBarBinding); + +// Resolve +var foo = kernel.resolve(fooRuntimeIdentifier); +var bar = kernel.resolve(barRuntimeIdentifier); +var fooBar = kernel.resolve(fooBarRuntimeIdentifier); + +// Unbind +kernel.unbind(fooRuntimeIdentifier); +kernel.unbindAll(); diff --git a/inversify/inversify.d.ts b/inversify/inversify.d.ts new file mode 100644 index 000000000..f3c924e82 --- /dev/null +++ b/inversify/inversify.d.ts @@ -0,0 +1,51 @@ +// Type definitions for inversify 1.0.0 +// Project: https://github.com/inversify/InversifyJS +// Definitions by: inversify +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module inversify { + + interface TypeBindingInterface { + runtimeIdentifier : string; + implementationType : { new(): TServiceType ;}; + cache : TServiceType; + scope : number; // TypeBindingScopeEnum + } + + interface KernelInterface { + bind(typeBinding : TypeBindingInterface) : void; + unbind(runtimeIdentifier : string) : void; + unbindAll() : void; + resolve(runtimeIdentifier : string) : TImplementationType; + } + + enum TypeBindingScopeEnum { + Transient = 0, + Singleton = 1, + } + + class TypeBinding implements TypeBindingInterface { + runtimeIdentifier: string; + implementationType: { + new (): TServiceType; + }; + cache: TServiceType; + scope: TypeBindingScopeEnum; + constructor(runtimeIdentifier: string, implementationType: { + new (...args: any[]): TServiceType; + }, scopeType?: TypeBindingScopeEnum); + } + + class Kernel implements KernelInterface { + private _bindings; + bind(typeBinding: TypeBindingInterface): void; + unbind(runtimeIdentifier: string): void; + unbindAll(): void; + resolve(runtimeIdentifier: string): TImplementationType; + private _validateBinding(typeBinding); + private _getConstructorArguments(func); + private _injectDependencies(func); + private _construct(constr, args); + constructor(); + } +}