Libraries
    Preparing search index...

    Class ScopedServiceProvider

    A service provider scoped to a ServiceScope. Resolves scoped services from the scope's cache and singletons from the root cache.

    This class is not instantiated directly — obtain it via scope.serviceProvider.

    Implements

    Index

    Methods

    • Resolves a service within this scope.

      • Singleton services are resolved from the root provider's cache.
      • Scoped services are created once per scope and cached.
      • Transient services are created fresh on every call.

      Type Parameters

      • TSchema extends SchemaBuilder<any, any, any, any, any>

      Parameters

      • schema: TSchema

        The schema instance used as the service identifier.

      Returns InferType<TSchema>

      The resolved service, typed as InferType<typeof schema>.

      If the schema is not registered.

      If a circular dependency is detected.

      using scope = provider.createScope();
      const db = scope.serviceProvider.get(IDbContext);
    • Resolves a service within this scope, or returns undefined if not registered.

      Type Parameters

      • TSchema extends SchemaBuilder<any, any, any, any, any>

      Parameters

      • schema: TSchema

        The schema instance used as the service identifier.

      Returns InferType<TSchema> | undefined

      The resolved service or undefined.

    • Resolves the dependencies described by a FunctionSchemaBuilder and calls implementation with the resolved values. Scoped services are resolved within this scope.

      Type Parameters

      • TFuncSchema extends FunctionSchemaBuilder<any, any, any, any, any, any, undefined, any>

      Parameters

      • funcSchema: TFuncSchema

        A FunctionSchemaBuilder whose parameters describe the services to inject.

      • implementation: InferType<TFuncSchema>

        A function whose parameters match the funcSchema parameter types.

      Returns ReturnType<InferType<TFuncSchema>>

      The return value of implementation.

      using scope = provider.createScope();
      const result = scope.serviceProvider.invoke(handler, (logger, db) => {
      logger.info('Handling request');
      return db.query('SELECT 1');
      });