@tdi2/di-core
Version:
TypeScript Dependency Injection 2 - Core DI framework
47 lines (44 loc) • 1.91 kB
TypeScript
type Constructor<T = object> = new (...args: any[]) => T;
interface DIContainer {
register<T>(token: string | symbol, implementation: Constructor<T>, scope?: "singleton" | "transient" | "scoped"): void;
resolve<T>(token: string | symbol): T;
has(token: string | symbol): boolean;
createScope(): DIContainer;
registerByInterface<T>(interfaceName: string, implementation: () => T, scope?: "singleton" | "transient" | "scoped"): void;
resolveByInterface<T>(interfaceName: string): T;
hasInterface(interfaceName: string): boolean;
}
type ServiceFactory<T> = (container?: any) => T;
type ServiceFactoryFactory<T> = (container?: any) => () => T;
interface DIMap {
[token: string]: {
factory: ServiceFactory<any> | ServiceFactoryFactory<any>;
scope: "singleton" | "transient" | "scoped";
dependencies: string[];
interfaceName?: string;
implementationClass: string;
isAutoResolved: boolean;
qualifier?: string;
};
}
declare class CompileTimeDIContainer implements DIContainer {
private services;
private instances;
private factories;
private scopes;
private parent?;
constructor(parent?: DIContainer);
register<T>(token: string | symbol, implementation: any, // This will be a factory function generated at compile time
scope?: "singleton" | "transient" | "scoped"): void;
resolve<T>(token: string | symbol): T;
has(token: string | symbol): boolean;
createScope(): DIContainer;
loadConfiguration(diMap: DIMap): void;
private getTokenKey;
getRegisteredTokens(): string[];
debugContainer(): void;
registerByInterface<T>(interfaceName: string, implementation: () => T, scope?: "singleton" | "transient" | "scoped"): void;
resolveByInterface<T>(interfaceName: string): T;
hasInterface(interfaceName: string): boolean;
}
export { CompileTimeDIContainer };