How resolution works
The container has a small model: a token names a dependency, a provider says how to build it, and a container stores the instance. This page follows one get() call through that model, so the rest of the docs can refer to it instead of repeating it.
Token, provider, instance
Passing a class to get() or inject() is shorthand for passing its token. Tokens are compared by identity, never by name. Tokens and Providers cover the two pieces you write yourself.
What happens during a call
Three things follow from this pipeline:
- Dependencies are discovered by running the constructor. Nothing is declared ahead of time. An
inject()call re-enters the same pipeline for its own token, which is why conditionalinject()calls work: a dependency that this build does not ask for is not built. - The scope decides where the instance is stored. A singleton goes to the root container, a request-scoped instance to the scope that asked, and a transient is not stored at all. Scopes has the rules.
- Every failed resolution is a
DIErrorwith acode. An error thrown by your constructor, factory oronServiceInitis wrapped asInitializationError, with the original incause. ADIErrorraised while building a dependency passes through unchanged, so the error names the innermost service that failed. The API reference lists the codes.
While an instance is being built, the container records which stored instances it injected. Invalidation uses those links to destroy dependents together with the thing they depend on.
Synchronous first
The whole pipeline runs synchronously. When UserService is constructed, inject(DatabaseService) constructs DatabaseService, runs its onServiceInit and returns the real, finished instance before the next field initialiser runs.
get() returns a promise so that one call works for every service. getSync() returns the instance directly, and succeeds whenever nothing on the way was asynchronous:
peek() never builds anything. It returns an instance that already exists, or null.
When resolution becomes asynchronous
Resolution has to wait in these cases:
get() waits for all of them. getSync() throws a DIError with code AsyncInitRequired, both for the service itself and for anything it depends on:
Once the asynchronous work has finished, the instance is stored like any other and every later resolution of it is synchronous again. Once disposal has started there is nothing left to wait for, and both calls fail with ContainerDisposed.
A getSync() that throws AsyncInitRequired does not cancel the build. It keeps running in the background, so the same call succeeds after await container.ready(). A transient is the exception: nobody can receive it later, so it is destroyed once it is ready.
An instance must not have a then method. get() returns a promise, and a promise unwraps anything that has one.
Constructor replay
A constructor cannot pause. If it calls inject() for a dependency whose onServiceInit is still running, the container cannot hand over a finished instance, and it cannot suspend the constructor either. So it does this:
inject()returns a placeholder and the constructor runs to the end.- The container discards the object that was built.
- It waits for the dependency, then runs the constructor again. This time
inject()returns the real instance.
Dependencies are built because the constructor asks for them, never ahead of it. A class therefore runs its constructor a second time in every build that meets a dependency that is not ready: for a singleton with an asynchronous initialiser only until it is initialised, for a request-scoped one in each scope that has not built it yet.
A dependency that fails while the container is waiting for it makes inject() throw in the next pass, so a try around the call works the same for synchronous and asynchronous failures.
Two rules follow, and they are the only rules a service has to obey:
- Keep constructors and field initialisers free of side effects. They may run twice. Open connections, start timers and subscribe in
onServiceInit, which runs once per instance. - Do not use an injected service inside the constructor. Store it, and use it from
onServiceInitor a method. Touching a placeholder throws, which ends that pass early: the container discards the pass, error included, and runs the constructor again once the dependency is ready. The code works, but everything before it in the constructor has run twice. An error thrown by a pass in which every dependency was ready fails the build.
Factories follow a variation of the same idea, described in Factories. Transients injected during a discarded pass are reused by the replay when it asks for them with equal arguments, so they are not built twice. An argument that is never equal, such as a function created in the constructor, makes a transient with an asynchronous initialiser impossible to settle; the build fails after 32 passes that got no further and names it. An asyncInject() of a transient starts only once the pass that called it is known to be kept.