FactoryProvider

Interface defining a Factory type provider.

See more...


interface FactoryProvider<T = any> {
  provide: InjectionToken
  useFactory: (...args: any[]) => T | Promise<T>
  inject?: Array<InjectionToken | OptionalFactoryDependency>
  scope?: Scope
  durable?: boolean
}

See also

Description

For example:


const connectionFactory = {
  provide: 'CONNECTION',
  useFactory: (optionsProvider: OptionsProvider) => {
    const options = optionsProvider.get();
    return new DatabaseConnection(options);
  },
  inject: [OptionsProvider],
};

Properties

Property Description
provide: InjectionToken

Injection token

useFactory: (...args: any[]) => T | Promise<T>

Factory function that returns an instance of the provider to be injected.

inject?: Array<InjectionToken | OptionalFactoryDependency>

Optional list of providers to be injected into the context of the Factory function.

scope?: Scope

Optional enum defining lifetime of the provider that is returned by the Factory function.

durable?: boolean

Flags provider as durable. This flag can be used in combination with custom context id factory strategy to construct lazy DI subtrees.

This flag can be used only in conjunction with scope = Scope.REQUEST.