MCPcopy
hub / github.com/loopbackio/loopback-next / toClass

Method toClass

packages/context/src/binding.ts:875–893  ·  view source on GitHub ↗

* Bind the key to an instance of the given class. * * @param ctor - The class constructor to call. Any constructor * arguments must be annotated with `@inject` so that * we can resolve them from the context.

(ctor: Constructor<C>)

Source from the content-addressed store, hash-verified

873 * we can resolve them from the context.
874 */
875 toClass<C extends T & object>(ctor: Constructor<C>): this {
876 /* istanbul ignore if */
877 if (debug.enabled) {
878 debug('Bind %s to class %s', this.key, ctor.name);
879 }
880 this._source = {
881 type: BindingType.CLASS,
882 value: ctor,
883 };
884 this._setValueGetter(resolutionCtx => {
885 const value = instantiateClass(
886 ctor,
887 resolutionCtx.context,
888 resolutionCtx.options.session,
889 );
890 return Binding.valueOrProxy(resolutionCtx, value);
891 });
892 return this;
893 }
894
895 /**
896 * Bind to a class optionally decorated with `@injectable`. Based on the

Calls 3

_setValueGetterMethod · 0.95
instantiateClassFunction · 0.90
valueOrProxyMethod · 0.80

Tested by 15

givenLifeCycleRegistryFunction · 0.64
givenCtxFunction · 0.64
givenProvidersFunction · 0.64
givenProvidersFunction · 0.64
givenProvidersFunction · 0.64
givenProvidersFunction · 0.64
setupBindingsFunction · 0.64
givenControllerFunction · 0.64
givenControllerFunction · 0.64
createContextFunction · 0.64