How can an instance of class A
obtain a dependency on an instance of class O
, while remaining a singleton for others?
@injectable()
class O{}
// A must be single instance!
@injectable()
class A{
constructor(o: O){
console.log( 'is A instance' );
}
}
@injectable()
class B{
constructor(a: A){
console.log( 'is B instance' );
}
}
@injectable()
class C{
constructor(a: A){
console.log( 'is C instance' );
}
}
@injectable()
class D{
constructor(b: B, c: C){
console.log( 'is D instance' );
}
}
let container = new Container();
container.bind<B>( O ).to( O );
container.bind<B>( A ).toConstantValue( A ); // ?
container.bind<B>( B ).to( B );
container.bind<C>( C ).to( C );
container.bind<D>( D ).to( D );
container.get<D>(D);