import { Container } from 'inversify' ; import type { IServiceCollection , IServiceConstructor , IServiceInjector } from './IApp' ; import type { InjectionToken } from './InjectionToken' ; import { isConstructor } from './isConstructor' ; export class DIContainer implements IServiceInjector , IServiceCollection { protected container = new Container ( { defaultScope : 'Singleton' , skipBaseClassChecks : true , } ) ; private parent : DIContainer | null = null ; constructor ( parent ? ) { if ( parent ) { this . bindWithParent ( parent ) ; } } bindWithParent ( parent ) { this . container . parent = parent . container ; this . parent = parent ; } unbindParent ( ) { this . container . parent = null ; this . parent = null ; } getParent ( ) { return this . parent ; } getServiceByClass < T > ( ctor ) { return this . container . get < T > ( ctor ) ; } getServiceByToken < T > ( token ) { return this . container . get < T > ( token ) ; } resolveServiceByClass < T > ( ctor ) { return this . container . resolve ( ctor ) ; } addServiceByClass ( Ctor ) { this . container . bind ( Ctor ) . toSelf ( ) ; } addServiceByToken < T extends Record < string , any > > ( token , value ) { if ( isConstructor ( value ) ) { this . container . bind ( token ) . to ( value as IServiceConstructor < T > ) ; } else { this . container . bind ( token ) . toConstantValue ( value ) ; } } }