import { Context , inject } from '@loopback/context' ; class Greeter { constructor ( @ inject ( 'prefix' ) private prefix ) { } greet ( name ) { return `[${ this . prefix }] Hello, ${ name }!` ; } } export async function main ( ) { const appCtx = new Context ( 'app' ) ; const requestCtx = new Context ( appCtx , 'request' ) ; const greeterBinding = appCtx . bind ( 'services.Greeter' ) . toClass ( Greeter ) . tag ( 'greeter' ) ; appCtx . bind ( 'prefix' ) . to ( appCtx . name ) ; let greeter = await requestCtx . get < Greeter > ( greeterBinding . key ) ; console . log ( greeter . greet ( 'John' ) ) ; requestCtx . bind ( 'prefix' ) . to ( requestCtx . name ) ; greeter = await requestCtx . get < Greeter > ( greeterBinding . key ) ; console . log ( greeter . greet ( 'John' ) ) ; greeter = await appCtx . get < Greeter > ( greeterBinding . key ) ; console . log ( greeter . greet ( 'John' ) ) ; } if ( require . main === module ) { main ( ) . catch ( err => { console . error ( err ) ; process . exit ( 1 ) ; } ) ; }