Application.ts
1 import { Container, interfaces } from 'inversify'; 2 import { IApplication, IApplicationSymbol } from './IApplication'; 3 import { IModule } from './Modules/IModule'; 4 import { IApplicationRegistration } from './IApplicationRegistration'; 5 6 export class Application implements IApplication, IApplicationRegistration { 7 public constructor(container?: Container) { 8 if (container) { 9 this.container = container; 10 } 11 this.container.bind(IApplicationSymbol).toConstantValue(this); 12 } 13 14 public registerConstant<T>(service: T, identifier: symbol) { 15 if (this.container.isCurrentBound(identifier)) { 16 this.container.unbind(identifier); 17 } 18 this.container.bind<T>(identifier).toConstantValue(service); 19 } 20 21 public registerConstantMultiple<T>(service: T, identifier: symbol) { 22 this.container.bind<T>(identifier).toConstantValue(service); 23 } 24 25 public register<T>(service: interfaces.Newable<T>, identifier: symbol) { 26 if (this.container.isCurrentBound(identifier)) { 27 this.container.unbind(identifier); 28 } 29 this.container.bind<T>(identifier).to(service); 30 } 31 32 public registerMultiple<T>(service: interfaces.Newable<T>, identifier: symbol) { 33 this.container.bind<T>(identifier).to(service); 34 } 35 36 public use(module: IModule): void { 37 module(this); 38 } 39 40 public getService<T>(identifier: symbol): T | undefined { 41 return this.container.get<T>(identifier); 42 } 43 44 public createChildApplication(): Application { 45 return new Application(this.container.createChild()); 46 } 47 48 /** 49 * The IOC {@link Container} of the {@link IApplication}. 50 */ 51 private readonly container = new Container({ 52 defaultScope: 'Singleton', 53 }); 54 }