Factories
A factory produces a value that is not an instance of your own injectable class: a client from a third-party library, a plain object, a function. The container treats the result like any other instance: it is stored by scope, injected, and destroyed.
@Factory
A factory is a class with a create() method. Resolving the class returns what create() returned.
create() runs once per stored instance: once per container for a singleton, once per scope for a request-scoped factory, on every resolution for a transient one. It may be synchronous; then the value can be resolved with getSync().
A produced value has no lifecycle hooks. Initialise it inside create() and register cleanup with ctx.addDestroyListener().
An error thrown by create() reaches the caller as a DIError with code InitializationError and the original in cause. The destroy listeners registered so far run, so what was opened before the error is closed. Nothing is stored, and the next resolution calls create() again.
Providing a token
Most consumers should not know that a factory exists. Give the value a token and let them inject that:
The decorator checks that create() returns the token's type.
When the token has a schema, the validated arguments arrive as the second parameter of create(), and one value is produced per distinct argument value:
FactoryContext
The first parameter of create() describes the resolution in progress.
Listeners run in reverse order of registration. A listener may still use ctx.inject() for what the value depends on, and for singletons from a request-scoped factory. The same context is passed to function providers and to factory tokens.
Injecting dependencies
A factory has two ways to reach other services.
inject()in a field initialiser, as in a service. The factory object is created just beforecreate()is called, so the fields are set.ctx.inject()insidecreate(). It returns a promise, which makes it the one to use for dependencies that initialise asynchronously and for anything requested after anawait.
Both record the dependency, so invalidating REDIS or DatabaseService also destroys the queue built from them.
inject() can also be called directly inside create(), before its first await. If the dependency is still initialising, the container stops create() at that call and runs it again from the top once the dependency is ready. Code before the call therefore runs twice. ctx.inject() has no such effect, which is why it is the better default inside create().
Function providers
When a value is only wired in one place, a function avoids the class. Pass it to provide() with a token:
The function receives the same context and validated arguments as create() and follows the same rules. The difference is reach: @Factory({ token }) sets the token's default for every container, a function provider applies to the one container it is given to. registerProvider() makes a function the default.
A class counts as a factory for types when create takes no parameters, or a FactoryContext first. A service with a create() method that takes its own data is unaffected. A create() without parameters on an ordinary @Injectable() service is taken for a factory: inject() is then typed as its result, although it returns the service. Give such a method another name.
Registering without decorators
registerFactory() is the function form of @Factory, with the same options.