export class Services { private static _current: Services = new Services() private _services: Map = new Map() private _factories: Map any> = new Map() private _parent: Services | undefined = undefined public Register(ctor: Function, factory: (services: Services) => T): void { this._factories.set(ctor, factory) } public Get(ctor: Function): T { if (this._services.has(ctor)) { return this._services.get(ctor) } const factory = this._factories.get(ctor) if (factory) { const instance = factory(this) this._services.set(ctor, instance) return instance } if (this._parent !== undefined) { return this._parent.Get(ctor) } throw new Error(`No such factory as ${ctor.name}`) } public CreateChild(): Services { const container = new Services() container._parent = this return container } public static get Current(): typeof this._current { return Services._current } public static set Current(value: Services) { Services._current = value } }