@lppedd/di-wise-neo - v0.19.0
    Preparing search index...

    Interface Container

    A Dependency Injection container.

    interface Container {
        isDisposed: boolean;
        options: ContainerOptions;
        parent: Container | undefined;
        addHook(hook: ContainerHook): void;
        clearCache(): unknown[];
        createChild(options?: Partial<ChildContainerOptions>): Container;
        dispose(): void;
        getAllCached<Value>(token: Token<Value>): Value[];
        getCached<Value>(token: Token<Value>): Value | undefined;
        isRegistered<Value>(token: Token<Value>, name?: string): boolean;
        register<Instance extends object>(Class: Constructor<Instance>): Container;
        register<Value>(token: ProviderType<Value>): Container;
        register<Instance extends object, ProviderInstance extends object>(
            token: Token<Instance>,
            provider: ClassProvider<ProviderInstance>,
            options?: RegistrationOptions,
        ): Container;
        register<Value, ProviderValue>(
            token: Token<Value>,
            provider: FactoryProvider<ProviderValue>,
            options?: RegistrationOptions,
        ): Container;
        register<Value, ProviderValue>(
            token: Token<Value>,
            provider: ExistingProvider<ProviderValue>,
        ): Container;
        register<Value, ProviderValue>(
            token: Token<Value>,
            provider: ValueProvider<ProviderValue>,
        ): Container;
        removeHook(hook: ContainerHook): void;
        resetRegistry(): unknown[];
        resolve<Instance extends object>(
            Class: Constructor<Instance>,
            name?: string,
        ): Instance;
        resolve<Value>(token: Token<Value>, name?: string): Value;
        resolveAll<Instance extends object>(
            Class: Constructor<Instance>,
        ): Instance[];
        resolveAll<Value>(token: Token<Value>): Value[];
        tryResolve<Instance extends object>(
            Class: Constructor<Instance>,
            name?: string,
        ): Instance | undefined;
        tryResolve<Value>(token: Token<Value>, name?: string): Value | undefined;
        tryResolveAll<Instance extends object>(
            Class: Constructor<Instance>,
        ): Instance[];
        tryResolveAll<Value>(token: Token<Value>): Value[];
        unregister<Value>(token: Token<Value>, name?: string): Value[];
    }
    Index

    Properties

    isDisposed: boolean

    Whether this container is disposed.

    The options used to create this container.

    parent: Container | undefined

    The parent container, or undefined if this is the root container.

    Methods

    • Adds a hook to observe the lifecycle of container-managed values.

      Does nothing if the hook has already been added.

      Parameters

      Returns void

    • Clears and returns all distinct values that were cached by this container. Values from ValueProvider registrations are not included, as they are never cached.

      Note that only this container is affected. Parent or child containers, if any, remain unchanged.

      Returns unknown[]

    • Disposes this container and all its cached values.

      Token values implementing a Disposable interface (e.g., objects with a dispose() function) are automatically disposed during this process.

      Note that children containers are disposed first, in creation order.

      Returns void

    • Returns all cached values associated with registrations of the token, in the order they were registered, or an empty array if none have been cached.

      If the token has at least one registration in the current container, cached values are taken from those registrations. Otherwise, cached values may be retrieved from parent containers, if any.

      Values are never cached for tokens with Transient or Resolution scope, or for ValueProvider registrations.

      Type Parameters

      • Value

      Parameters

      Returns Value[]

    • Returns the cached value from the most recent registration of the token, or undefined if no value has been cached yet (the token has not been resolved yet).

      If the token has at least one registration in this container, the cached value is taken from the most recent of those registrations. Otherwise, it may be retrieved from parent containers, if any.

      Values are never cached for tokens with Transient or Resolution scope, or for ValueProvider registrations.

      Type Parameters

      • Value

      Parameters

      Returns Value | undefined

    • Returns whether the token is registered in this container or in parent containers, if any.

      Type Parameters

      • Value

      Parameters

      Returns boolean

    • Removes all registrations from this container's internal registry.

      Returns an array of the distinct values that were cached by this container for the removed registrations. Values from ValueProvider registrations are not included, as they are not cached.

      Note that only this container is affected. Parent or child containers, if any, remain unchanged.

      Returns unknown[]

    • Resolves the given class to the instance associated with it.

      If the class is registered in this container or any of its parent containers, an instance is created using the most recent registration.

      If the class is not registered, but it is decorated with AutoRegister, or ContainerOptions.autoRegister is true, the class is registered automatically. Otherwise, the resolution fails.

      The scope for the automatic registration is determined by either the Scoped decorator on the class, or ContainerOptions.defaultScope.

      If the class is not registered in this container or any of its parent containers and could not be auto-registered, an error is thrown.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided instance is returned.
      • For FactoryProvider, the factory function is invoked to create the instance.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the instance is resolved by referring to another registered token.

      If the class is registered with Container scope, the resolved instance is cached in the container's internal registry.

      Type Parameters

      • Instance extends object

      Parameters

      Returns Instance

    • Resolves the given token to the value associated with it.

      If the token is registered in this container or any of its parent containers, its value is resolved using the most recent registration.

      If the token is not registered in this container or any of its parent containers, an error is thrown.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided value is returned.
      • For FactoryProvider, the factory function is invoked to create the value.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the value is resolved by referring to another registered token.

      If the token is registered with Container scope, the resolved value is cached in the container's internal registry.

      Type Parameters

      • Value

      Parameters

      Returns Value

    • Resolves the given class to all instances provided by the registrations associated with it.

      If the class is not registered, but it is decorated with AutoRegister, or ContainerOptions.autoRegister is true, the class is registered automatically. Otherwise, the resolution fails.

      The scope for the automatic registration is determined by either the Scoped decorator on the class, or ContainerOptions.defaultScope.

      If the class is not registered in this container or any of its parent containers and could not be auto-registered, an error is thrown.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided instance is returned.
      • For FactoryProvider, the factory function is invoked to create the instance.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the instance is resolved by referring to another registered token.

      If the class is registered with Container scope, the resolved instances are cached in the container's internal registry.

      A separate instance of the class is created for each provider.

      Type Parameters

      • Instance extends object

      Parameters

      Returns Instance[]

    • Resolves the given token to all values provided by the registrations associated with it.

      If the token is not registered in this container or any of its parent containers, an error is thrown.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided value is returned.
      • For FactoryProvider, the factory function is invoked to create the value.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the value is resolved by referring to another registered token.

      If the token is registered with Container scope, the resolved values are cached in the container's internal registry.

      Type Parameters

      • Value

      Parameters

      Returns Value[]

    • Resolves the given class to the instance associated with it.

      If the class is registered in this container or any of its parent containers, an instance is created using the most recent registration.

      If the class is not registered, but it is decorated with AutoRegister, or ContainerOptions.autoRegister is true, the class is registered automatically. Otherwise, the resolution fails.

      The scope for the automatic registration is determined by either the Scoped decorator on the class, or ContainerOptions.defaultScope.

      If the class is not registered in this container or any of its parent containers and could not be auto-registered, undefined is returned instead.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided instance is returned.
      • For FactoryProvider, the factory function is invoked to create the instance.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the instance is resolved by referring to another registered token.

      If the class is registered with Container scope, the resolved instance is cached in the container's internal registry.

      Type Parameters

      • Instance extends object

      Parameters

      Returns Instance | undefined

    • Tries to resolve the given token to the value associated with it.

      If the token is registered in this container or any of its parent containers, its value is resolved using the most recent registration.

      If the token is not registered in this container or any of its parent containers, undefined is returned instead.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided value is returned.
      • For FactoryProvider, the factory function is invoked to create the value.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the value is resolved by referring to another registered token.

      If the token is registered with Container scope, the resolved value is cached in the container's internal registry.

      Type Parameters

      • Value

      Parameters

      Returns Value | undefined

    • Resolves the given class to all instances provided by the registrations associated with it.

      If the class is not registered, but it is decorated with AutoRegister, or ContainerOptions.autoRegister is true, the class is registered automatically. Otherwise, the resolution fails.

      The scope for the automatic registration is determined by either the Scoped decorator on the class, or ContainerOptions.defaultScope.

      If the class is not registered in this container or any of its parent containers and could not be auto-registered, an empty array is returned instead.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided instance is returned.
      • For FactoryProvider, the factory function is invoked to create the instance.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the instance is resolved by referring to another registered token.

      If the class is registered with Container scope, the resolved instances are cached in the container's internal registry.

      A separate instance of the class is created for each provider.

      Type Parameters

      • Instance extends object

      Parameters

      Returns Instance[]

    • Resolves the given token to all values provided by the registrations associated with it.

      If the token is not registered in this container or any of its parent containers, an empty array is returned instead.

      The resolution behavior depends on the Provider used during registration:

      • For ValueProvider, the explicitly provided value is returned.
      • For FactoryProvider, the factory function is invoked to create the value.
      • For ClassProvider, a new instance of the class is created according to its scope.
      • For ExistingProvider, the value is resolved by referring to another registered token.

      If the token is registered with Container scope, the resolved values are cached in the container's internal registry.

      Type Parameters

      • Value

      Parameters

      Returns Value[]

    • Removes all registrations for the given token from the container's internal registry.

      Returns an array of the distinct values that were cached by this container for the removed registrations. Values from ValueProvider registrations are not included, as they are not cached.

      Note that only this container is affected. Parent or child containers, if any, remain unchanged.

      Type Parameters

      • Value

      Parameters

      Returns Value[]