diff --git a/forge-di/forge-di-test.ts b/forge-di/forge-di-test.ts new file mode 100644 index 000000000..33bff4716 --- /dev/null +++ b/forge-di/forge-di-test.ts @@ -0,0 +1,52 @@ +/// + +import Forge = require('forge-di'); + +var forge = new Forge(); + +class Bar {} + +class Foo { + constructor(public bar: Bar) { + } +} + +forge.bind('foo').to.type(Foo); +forge.bind('bar').to.type(Bar); + +var foo: Foo = forge.get('foo'); +var barInst = foo.bar; + +// register functions +var createFoo = (bar: Bar) => new Foo(bar); +forge.bind('foo').to.function(createFoo); + +// Conditional bindings and resolution hints +class RedFoo {} +class BlueFoo {} + +forge.bind('foo').to.type(RedFoo).when('red'); +forge.bind('foo').to.type(BlueFoo).when((hint) => hint === 'blue'); + +// Lifecycles +forge.bind('foo').to.type(Foo).as.singleton(); +forge.bind('bar').to.type(Bar).as.transient(); + +// explicit arguments +var manuallyCreatedBar = new Bar(); +forge.bind('foo').to.type(Foo).with({bar: manuallyCreatedBar}); + +var bindingArgs: Forge.IBindingArguments = { + bar: manuallyCreatedBar +}; +forge.bind('foo').to.type(Foo).with(bindingArgs); + +// Ephemeral Bindings TODO +class DependsOnFoo { + constructor(public foo: Foo){} +} +var dependsOnFoo = forge.create(DependsOnFoo); + +// Unbinding and rebinding +forge.unbind('foo'); +forge.rebind('foo').to.type(Foo); \ No newline at end of file diff --git a/forge-di/forge-di.d.ts b/forge-di/forge-di.d.ts new file mode 100644 index 000000000..e28def38f --- /dev/null +++ b/forge-di/forge-di.d.ts @@ -0,0 +1,206 @@ +// Type definitions for forge-di v0.9.5 +// Project: https://github.com/nkohari/forge +// Definitions by: Adam Carr +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "forge-di" { + /** + * Implementation of the forge dependency injection manager. + */ + class Forge { + /** + * Creates a new instance + * @returns {Forge} a new instance. + */ + new(): Forge; + + /** + * The bindings mapped to this forge instance. + */ + bindings: Forge.IBindingMap; + + /** + * Creates a new binding. + * @param {string} name The binding name. + */ + bind(name: string): Forge.IBinding; + /** + * Unbinds then recreates a binding for this name. + * @param {string} name The binding name. + */ + rebind(name: string): Forge.IBinding; + /** + * Unbinds all bindings for this name. Returns the number of bindings removed. + * @param {string} name The binding name. + */ + unbind(name: string): number; + /** + * Get instance or instances of type registered under the provided name and optional hint. + * @param {string} name The binding name. + * @param {string} hint The binding hint. + * @param {...args} args Additional args. + */ + get(name: string, hint?: string, ...args: any[]): T; + /** + * Get a single instance of type registered under the provided name and optional hint. + * @param {string} name The binding name. + * @param {string} hint The binding hint. + * @param {...args} args Additional args. + */ + getOne(name: string, hint?: string, ...args: any[]): T; + /** + * Gets all instances of the type registered under the provided name. + * @param {string} name The binding name. + * @param {...args} args Additional args. + */ + getAll(name: string, ...args: any[]): T | T[]; + /** + * Creates an instance of the target type attempting to resolve any dependencies. + * @param {T} target The target type. + * @param {...args} args Additional args. + */ + create(target: T, ...args: any[]): T; + /** + * Get all bindings registered under a binding name and optional hint. + * @param {string} name The binding name. + * @param {string} hint The binding hint. + */ + getMatchingBindings(name: string, hint?: string): Forge.IBinding[]; + /** + * Returns a string that represents all bindings within this forge instance. + */ + inspect(): string; + + resolve(name: string, context?: Forge.IContext, hint?: string, all?: boolean, ...args: any[]): T | T[]; + resolveBindings(context: Forge.IContext, bindings: Forge.IBinding[], hint: string, args: any[], unwrap: boolean): Forge.IBinding[]; + } + + module Forge { + interface IContext { + new (): IContext; + bindings: IBinding[]; + has(binding: IBinding): boolean; + push(binding: IBinding): void; + pop(): IBinding; + toString(indent: number): string; + } + + interface IType { + new (...args: any[]): any; + } + + /** + * Represents arguments to help with resolving a binding. + */ + interface IBindingArguments { + [name: string]: any; + } + + /** + * Represents a binding between a name, type/instance/function and optional hint. + */ + interface IBinding { + /** The forge that contains this binding. */ + forge: Forge; + /** The binding name. */ + name: string; + /** Alias mapping to this binding. */ + to: IBinding; + /** Alias mapping to this binding. */ + as: IBinding; + /** Whether or not this binding is currently resolving. */ + isResolving: boolean; + /** The resolver for this binding. */ + resolver: IResolver; + /** The lifecycle associated with this binding. Defaults to singleton. */ + lifecycle: ILifecycle; + /** The predicate associated with this binding. Used to support hints. */ + predicate: IPredicate; + /** The additional binding arguments to help resolve dependencies. */ + arguments: IBindingArguments; + + /** + * Checks whether or not this binding matches the hint by executing the predicate. + * @param {string} hint The hint to check against. + */ + matches(hint: string): boolean; + /** + * Registers a type to a binding. This type must have a constructor. + * @param {T} target The target type. + */ + type(target: T): IBinding; + /** + * Registers a type to a binding. This must be a callable function. + * @param {T} target The target function. + */ + function(target: T): IBinding; + /** + * Registeres an instance to a binding. This instance will always be returned. + * @param {T} target The target instance. + */ + instance(target: T): IBinding; + /** + * Configures this binding lifecycle as a singleton. This is the default lifecycle. + */ + singleton(): IBinding; + /** + * Configures this binding lifecycle as transient. + * New instances will be created, if this is a type based binding, on each get. + */ + transient(): IBinding; + /** + * Registers a predicate for this binding. + * @param {IPredicate} predicate The predicate. + */ + when(predicate:IPredicate): IBinding; + /** + * Registers a hint for this binding. + * @param {string} hint The hint. + */ + when(hint: string): IBinding; + /** + * Registers additional binding arguments to help with resolving. + * @param {IBindingArguments} args The additional binding arguments. + */ + with(args: IBindingArguments): IBinding; + /** + * Returns a string representing this binding. + */ + toString(): string; + } + + /** Represents a binding map. */ + interface IBindingMap { + /** Gets a binding by name. */ + [name: string]: IBinding[]; + } + + /** Represents a predicate. */ + interface IPredicate { + /** + * Returns whether or not the hit satisfies this predicate. + * @param {string} hint The hint to check against. + */ + (hint: string): boolean; + } + + /** Represents a resolver. */ + interface IResolver { + /** + * Resolves a specific type. + */ + resolve(): T; + } + + /** Represents a binding lifecycle. */ + interface ILifecycle { + /** + * Returns the instance from a resolver based on the configured lifecycle. + * @param {IResolver} resolver The type resolver. + */ + getInstance(resolver: IResolver): T; + } + } + + export = Forge; +}