import 'reflect-metadata'; import { DependencyCreator } from './dependency-creator'; import { AbstractDependency } from './abstract-dependency'; import { DependencyProvider } from './dependency-provider'; import { DependencyQuery } from './collections/dependency-query'; import { TypeReference } from './type-reference'; import { AsyncDisposable } from '@dvolper/ts-system'; export declare enum ServingBehaviour { Lazy = 0, Greedy = 1 } /** * Resembles the entry point for any dependency injection. * Each instance has its own dependency and instance cache. * Only dependencies marked with `@Singleton` are globally unique. */ export declare class DependencyContainer implements AsyncDisposable { private static readonly _globalInstance; private readonly _registeredDependencies; private readonly _abstractionMapping; private readonly _singletonInstances; private readonly _scopedInstances; private _currentScope?; servingBehaviour: ServingBehaviour; /** * Do not use the global instance to add dependencies! * Every dependency added to any DependencyContainer instance will also be cached globally. * This means whenever you do not have access to a specific DependencyContainer instance you can try resolving dependencies via * `DependencyContainer.global.create` or `DependencyContainer.global.resolve`. * * @returns The global DependencyContainer instance */ static get global(): DependencyContainer; /** * @param scope - If specified the instance will directly use the specific scope. (See `DependencyContainer::useScope`) */ constructor(scope?: string); /** * Adds a dependency to the DependencyContainer cache and marks it as `@Injectable`. * * @param dependency - The dependency to be injected * @param provider - Optional dependency provider callback (Can be used to override the dependency creation behaviour) * @returns The DependencyContainer instance itself (for chaining .add calls) */ add(dependency: DependencyCreator, provider?: DependencyProvider): DependencyContainer; /** * Adds a dependency as an implementation of an abstraction to the DependencyContainer cache and marks it as `@Injectable`. * When mapping a dependency to an abstraction it is possible to query it based on the abstraction later on. (See `DependencyContainer::abstract`) * * @param abstraction - The abstraction of the dependency * @param implementation - The implementation of the specified abstraction * @param provider - Optional dependency provider callback (Can be used to override the dependency creation behaviour) * @returns The DependencyContainer instance itself (for chaining .add calls) */ add(abstraction: AbstractDependency, implementation: DependencyCreator, provider?: DependencyProvider): DependencyContainer; /** * Queries the DependencyContainer cache based on a specified abstraction. * * @param abstraction - The abstraction which will be used to query the dependency cache * @param args - Optional arguments used to create dependency instances later on (See `DependencyContainer::serve`) * @returns The DependencyQuery containing all cached implementations of the specified abstraction (See `DependencyQuery`) */ abstract(abstraction: AbstractDependency, ...args: any[]): DependencyQuery; /** * Serves a resolved instance of the specified dependency. (See `DependencyContainer::resolve`) * * @param dependency - The specific dependency for which an instance is required * @param args - Optional arguments used to create the dependency instance * @returns The resolved instance * @remarks * The constructor arguments of a required dependency will be resolved in the following manner when creating an instance: * * If no reflection metadata is emitted for the required dependency (No decorator used) * * => Only the optional arguments passed to `DependencyContainer::serve` will be used (No type checking possible) * * Else, the reflection metadata is checked first... * * If the reflection of an argument is a known dependency (in the current DependencyContainer cache) * * => The known dependency will be served * * If the argument for the same index in the optional arguments passed to `DependencyContainer::serve` matches the type of the reflection * * => Use the optional argument * * If the reflection of an argument is a known abstraction of a dependency (in the current DependencyContainer cache) * * => The first found implementation of the known abstraction will be served * * Else * * => `null` will be served (Different to when no reflection metadata is emitted and no optional arguments are passed => `undefined`) */ serve(dependency: DependencyCreator, ...args: any[]): TDependency; /** * Resolves an instance of a dependency. * * @param dependency - The instance to be resolved * @returns The resolved instance * @remarks * Only properties of an instance marked with `@Resolve` will be resolved. * You can extend the default resolve behaviour of any DependencyContainer by adding a derivation of `ResolveExtension` to it: * (See `ResolveExtension`) * ```ts * import {DependencyContainer, ResolveExtension} from '@dvolper/tsdi' * * class CustomResolveExtension extends ResolveExtension { * public resolve ( dc: DependencyContainer, dependency: TDependency ): TDependency * { * // Custom resolve logic... * return dependency * } * } * * const dc = new DependencyContainer * dc.add( CustomResolveExtension ) * * // Whenever .serve or .resolve is called on the extended DependencyContainer the CustomResolveExtension will be triggered. * const instance = dc.serve( ... ) * ``` */ resolve(dependency: TDependency): TDependency; /** * Queries the DependencyContainer cache. * * @param args - Optional arguments used to create dependency instances later on (See `DependencyContainer::serve`) * @returns The DependencyQuery containing all cached implementations (See `DependencyQuery`) */ query(...args: any[]): DependencyQuery; useScope(scope: string): void; exitScope(scope: string): Promise; dispose(): Promise; private verifyMetadata; private resolveCreationArguments; private resolveCreationArgument; private disposeCurrentScope; private static resolveTargetByContext; static isPrototypeAssignableFrom(prototype: any, type: TypeReference): boolean; } //# sourceMappingURL=dependency-container.d.ts.map