@djodjonx/wiredi
    Preparing search index...

    Interface ContainerProvider

    DI Container Provider Interface

    Contract that each adapter (tsyringe, awilix, inversify, etc.) must implement to be compatible with WireDI.

    class MyCustomProvider implements ContainerProvider {
    readonly name = 'my-provider'
    // ... implement all methods
    }
    interface ContainerProvider {
        name: string;
        createScope(): ContainerProvider;
        dispose(): void;
        getUnderlyingContainer(): unknown;
        isRegistered(token: ProviderToken): boolean;
        registerClass<T>(
            token: ProviderToken<T>,
            implementation?: Constructor<T>,
            lifecycle?: ProviderLifecycle,
        ): void;
        registerFactory<T>(
            token: symbol,
            factory: (provider: ContainerProvider) => T,
        ): void;
        registerValue<T>(token: symbol, value: T): void;
        resolve<T>(token: ProviderToken<T>): T;
    }

    Implemented by

    Index

    Properties

    name: string

    Provider name for debugging and logging purposes

    Methods

    • Creates a child scope (child container) Useful for HTTP requests, transactions, or isolated contexts

      Returns ContainerProvider

      New scoped provider instance

      const requestScope = provider.createScope()
      requestScope.registerValue(TOKENS.RequestId, generateRequestId())
    • Releases provider resources Called during application cleanup or shutdown

      Returns void

    • Access to the underlying container (for advanced use cases) Return type is generic as it depends on the implementation

      Returns unknown

      The underlying DI container instance

      const tsyringeContainer = provider.getUnderlyingContainer() as DependencyContainer
      
    • Checks if a token is already registered in the container

      Parameters

      Returns boolean

      true if the token is registered, false otherwise

      if (!provider.isRegistered(TOKENS.Logger)) {
      provider.registerClass(TOKENS.Logger, ConsoleLogger)
      }
    • Registers a class in the container

      Type Parameters

      • T

        The type of the class instance

      Parameters

      • token: ProviderToken<T>

        Token (symbol or class) to register under

      • Optionalimplementation: Constructor<T>

        Class to instantiate (optional if token is a class)

      • Optionallifecycle: ProviderLifecycle

        Instance lifecycle (Singleton, Transient, or Scoped)

      Returns void

      // Register class as its own token
      provider.registerClass(UserService)

      // Register implementation for a symbol token
      provider.registerClass(TOKENS.Logger, ConsoleLogger, ProviderLifecycle.Singleton)
    • Registers a factory function in the container

      Type Parameters

      • T

        The type of the value produced by the factory

      Parameters

      • token: symbol

        Symbol token to register the factory under

      • factory: (provider: ContainerProvider) => T

        Function that creates the instance (receives the provider to resolve dependencies)

      Returns void

      provider.registerFactory(TOKENS.HttpClient, (p) => {
      return new HttpClient(p.resolve(TOKENS.ApiUrl))
      })
    • Registers a static value in the container

      Type Parameters

      • T

        The type of the value

      Parameters

      • token: symbol

        Symbol token to register the value under

      • value: T

        The value to register

      Returns void

      provider.registerValue(TOKENS.ApiUrl, 'https://api.example.com')
      
    • Resolves a dependency from the container

      Type Parameters

      • T

        The type of the resolved dependency

      Parameters

      Returns T

      Instance of the dependency

      Error if the token is not registered

      const logger = provider.resolve<LoggerInterface>(TOKENS.Logger)
      const userService = provider.resolve(UserService)