Providers
A provider tells a container how to produce the value for a token. @Injectable() and @Factory() attach a default provider to their token. This page covers the other half: what a container can be told on top of the defaults.
provide
provide(target, implementation) creates a binding. Hand a list of them to the container:
The target is a token or a decorated class. A class that is not registered throws ClassNotInjectable. The implementation takes one of five forms:
The implementation has to fit the target: what it produces must have the public members of the target's type. Private members do not count, so a mock is a plain class or object.
The implementation class does not need a decorator of its own. If it is a @Factory class, its create() result is used. The function form is described under function providers.
A provider only affects the container it was given to. The token's default is untouched, so two containers in one process can wire the same class differently.
Aliases
{ existing } gives an instance a second name. Both names resolve to the same object with one lifecycle: it is built once, destroyed once, listed once in instances(), and invalidating it reaches everything that injected it under either name. Arguments are passed on. An alias has no instances of its own, so it cannot be decorated; decorate what it points to. It counts as resolved once something was asked for by its name.
container.provide
container.provide(target, implementation) adds a binding to an existing container. It takes the same arguments as provide().
It must be called before the token is first resolved in that container. Afterwards it throws, because an instance built from the old provider may already be held by other services.
registerProvider
registerProvider(token, implementation, options?) sets the default provider of a token from the same forms. Use it where a decorator does not fit: a token whose default is a value or a function.
A token has one default. A second, different one throws ProviderConflict, whichever way it is registered. options.scope replaces the scope the provider would have.
Outside production (NODE_ENV) a second provider of the same form replaces the first instead: hot reload evaluates a module again, and each time with new functions and values. A warning names the token when the two do not look alike.
Lookup order
For a token, a container uses the first of:
- An instance it already holds, including one stored with
addInstance(). - A provider given to this container, through
providersorcontainer.provide(). - The default provider on the token.
When none exists, resolution fails with a DIError with code ProviderNotFound and the path that led to the token. optional() returns null in that case instead.
A scope looks in its own instances first and then continues with its root container. Providers always belong to the root.
addInstance
addInstance(target, value) stores a ready value. Nothing is constructed and no provider is involved.
How it differs from provide(token, { value }):
Rules for the target:
- A token whose arguments are required cannot be used directly; that throws
TokenSchemaRequiredError. Pass a bound token so the value has an address. - A class must be registered with
@Injectable(). - A request-scoped token or class takes its value in a scope.
addInstance()on the root throwsScopeMismatchErrorfor it. - Adding a second instance for the same token and arguments throws. Invalidate the first one before replacing it.
Decorating instances
container.decorate(target, fn) wraps every instance the container builds for a token. The function receives the finished instance and returns what the container should store. It is not applied to a { value } provider or to a value stored with addInstance().
It runs after construction and onServiceInit, and before the instance is stored, so no other service ever sees the undecorated one. info holds the token and the validated args.
Several decorators for one token run in the order they were added. Like provide(), decorate() throws once the token is resolved. Unlike a provider, a decorator keeps the original implementation and adds to it, which suits logging, tracing and metrics.
Strict containers
new Container({ strict: true }) ignores step 3 of the lookup order. Only instances and providers given to that container resolve; every other token fails with ProviderNotFound, however deep in the graph it is requested.
provide(UserService, UserService) lists a class as itself. Strict mode is meant for unit tests that must not reach real services by accident; UnitTestContainer is built on it.
Inspecting a provider
container.getProvider(target) returns the provider the container would use, or undefined. container.isRegistered(target) answers whether the token has an instance or a provider at all.
Neither call builds anything.