@djodjonx/wiredi
    Preparing search index...

    Class InversifyProvider

    InversifyJS adapter implementing the ContainerProvider interface

    Provides integration between WireDI and InversifyJS, allowing you to use InversifyJS as your DI container while benefiting from WireDI's configuration and validation features.

    Implements

    Index

    Constructors

    Properties

    name: "inversify" = 'inversify'

    Provider name for debugging and logging purposes

    Methods

    • Initializes the provider asynchronously

      Returns Promise<void>

      Required before using the provider if not using createSync().

      const provider = new InversifyProvider()
      await provider.init()
      useContainerProvider(provider)
    • Checks if a token is already registered in the container

      Parameters

      • token: symbol | Constructor<unknown>

        Token to check

      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: symbol | Constructor<T>

        Token (symbol or class) to register under

      • Optionalimpl: 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

      • token: symbol | Constructor<T>

        Token of the dependency to resolve

      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)
    • Creates a pre-initialized provider synchronously

      Parameters

      Returns InversifyProvider

      Fully initialized InversifyProvider instance

      This is the recommended way to create an InversifyProvider as it avoids async initialization and ensures the provider is ready immediately.

      import * as inversify from 'inversify'

      const provider = InversifyProvider.createSync(inversify, {
      defaultScope: 'Transient'
      })