Is it possible to implement only with DI?
export const app = new Container();
function provider( context: interfaces.Context ): any {
let { name } = context.container.get<any>( Symbol.for( 'config' ) );
return { name };
}
app.bind<any>( Symbol.for('config') ).toConstantValue( {name: "config"} );
app.bind<any>( Symbol.for('provider') ).toDyn( provider );
let a = app.get<any>( Symbol.for( 'provider' ) );
let b = app.get<any>( Symbol.for( 'provider' ) );
console.log( a === b ); // to be true!
You can use toDynamicValue + inSingletonScope:
import { interfaces, Container } from "inversify";
interface Config {
name: string;
}
let container = new Container();
function provider( context: interfaces.Context ): any {
let { name } = context.container.get<any>( Symbol.for( 'config' ) );
return { name };
}
container.bind<Config>(Symbol.for('config')).toConstantValue({ name: "config" });
container.bind<Config>(Symbol.for('provider')).toDynamicValue(provider).inSingletonScope(); // !!!
let a = container.get<Config>(Symbol.for('provider'));
let b = container.get<Config>(Symbol.for('provider'));
console.log(a === b); // to be true!
If so, then I apologize! This is the first thing that occurred to me, but typescript auto-completion in phpstorm said that there is no such method...