MCPcopy Index your code
hub / github.com/loopbackio/loopback-next / main

Function main

examples/context/src/dependency-injection.ts:90–133  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

88}
89
90export async function main() {
91 const ctx = new Context('request');
92
93 // Bind greeting service
94 ctx.bind(GREETING_SERVICE).toClass(GreetingService);
95
96 // Set the current date to a factory function
97 ctx.bind(CURRENT_DATE).toDynamicValue(getCurrentDate);
98
99 // Set the current user to `John` (a constant value)
100 ctx.bind('currentUser').to('John');
101
102 // Set the current language to `zh`
103 ctx.bind('currentLanguage').to('zh');
104
105 // Add EnglishGreeter for now
106 ctx.bind('greeters.EnglishGreeter').toClass(EnglishGreeter).tag('greeter');
107
108 // Get an instance of the greeting service
109 const greetingService = await ctx.get(GREETING_SERVICE);
110
111 // Invoke `greet` as a method
112 let greeting = await greetingService.greet('en', 'Jane');
113 console.log(greeting);
114
115 // Use `invokeMethod` to apply method injection
116 greeting = await invokeMethod(greetingService, 'greet', ctx);
117 console.log(greeting);
118
119 // Now add ChineseGreeter
120 ctx.bind('greeters.ChineseGreeter').toClass(ChineseGreeter).tag('greeter');
121
122 greeting = await invokeMethod(greetingService, 'greet', ctx);
123 console.log(greeting);
124
125 // Change the current language to `en`
126 ctx.bind('currentLanguage').to('en');
127
128 // Change the current user to `Jane`
129 ctx.bind('currentUser').to('Jane');
130
131 greeting = await invokeMethod(greetingService, 'greet', ctx);
132 console.log(greeting);
133}
134
135if (require.main === module) {
136 main().catch(err => {

Callers 1

Calls 9

bindMethod · 0.95
getMethod · 0.95
invokeMethodFunction · 0.90
toClassMethod · 0.80
toDynamicValueMethod · 0.80
toMethod · 0.80
tagMethod · 0.80
greetMethod · 0.65
logMethod · 0.65

Tested by

no test coverage detected