Initialize the container with optional configuration
Optionaloptions: ContainerOptions
Optional container configuration including event emitter
Define an emitter instance to use for container events
Event emitter instance that implements emit method
Create a container resolver to resolve bindings, or make classes.
const resolver = container.createResolver()
await resolver.make(CLASS_CONSTRUCTOR)
Bind values with the resolver. Resolver values are isolated from the container.
resolver.bindValue(HttpContext, new HttpContext())
await resolver.make(UsersController)
A new container resolver instance
Find if the container has a binding registered using the "bind", the "singleton", the "bindValue", or the "if" methods.
A registered binding is not always resolvable. When a binding key only has conditional bindings registered for it, this method returns true, but resolving it still fails if none of the conditions match.
The binding key to check for
Find if the container has a binding registered using the "bind", the "singleton", the "bindValue", or the "if" methods.
A registered binding is not always resolvable. When a binding key only has conditional bindings registered for it, this method returns true, but resolving it still fails if none of the conditions match.
The binding key to check for
Find if the container has all the bindings registered using the "bind", the "singleton", the "bindValue", or the "if" methods.
Array of binding keys to check for
Find if the container has all the bindings registered using the "bind", the "singleton", the "bindValue", or the "if" methods.
Resolves the binding or constructor a class instance as follows.
The binding key or class constructor to resolve
OptionalruntimeValues: any[]
Optional array of runtime values for constructor dependencies
OptionalcreateError: ErrorCreator
Optional custom error creator function
Resolves the binding or constructor a class instance as follows.
The binding key or class constructor to resolve
OptionalruntimeValues: any[]
Optional array of runtime values for constructor dependencies
OptionalcreateError: ErrorCreator
Optional custom error creator function
Call a method on an object by injecting its dependencies. The method dependencies are resolved in the same manner as a class constructor dependencies.
The object instance containing the method
The method name to call
OptionalruntimeValues: any[]
Optional array of runtime values for method dependencies
OptionalcreateError: ErrorCreator
Optional custom error creator function
Register an alias for a binding. The value can be a reference to an existing binding or to a class constructor that will instantiate to the same value as the alias.
The alias name (must be string or symbol)
The constructor or binding key this alias points to
Register a binding inside the container. The method receives a key-value pair.
The binding key (string, symbol, or class constructor)
Factory function that resolves the binding value
Register a binding inside the container. The method receives a key-value pair.
The binding key (string, symbol, or class constructor)
Factory function that resolves the binding value
Register a binding as a value. Unlike bind() and singleton(), this method accepts the resolved value directly instead of a factory function.
The binding key (string, symbol, or class constructor)
The pre-resolved value to bind
Register a binding as a value. Unlike bind() and singleton(), this method accepts the resolved value directly instead of a factory function.
Register a binding as a singleton. The singleton method is same as the bind method, but the factory function is invoked only once and the result is cached for subsequent resolutions.
The binding key (string, symbol, or class constructor)
Factory function that resolves the binding value
Register a binding as a singleton. The singleton method is same as the bind method, but the factory function is invoked only once and the result is cached for subsequent resolutions.
The binding key (string, symbol, or class constructor)
Factory function that resolves the binding value
Define a fake implementation for a binding or a class constructor. Swaps have the highest priority when resolving dependencies from the container. Useful for testing.
The class constructor to swap
Factory function that returns the swapped implementation
Restore mentioned or all bindings by removing their swaps
Optionalbindings: AbstractConstructor<any>[]
Optional array of class constructors to restore. If not provided, all swaps are removed
Define hooks to be executed after a binding has been resolved from the container.
The hooks are executed for:
Hooks are not executed for direct values registered with the container.
The binding key or class constructor
Hook function to execute after resolution
Define hooks to be executed after a binding has been resolved from the container.
The hooks are executed for:
Hooks are not executed for direct values registered with the container.
The binding key or class constructor
Hook function to execute after resolution
Create a contextual builder to define contextual bindings. Contextual bindings allow you to specify different implementations for a dependency based on which class is asking for it.
The class constructor that will receive the contextual binding
Create a conditional builder to define bindings that are only used when the condition returns true. The condition is evaluated on every resolution and receives the resolver performing it.
Unlike contextual bindings, which are scoped to a parent class, conditional bindings apply to every resolution of the binding key.
Predicate deciding whether the bindings should be used
Add a contextual binding for a given class constructor. A contextual binding takes a parent, parent's dependency and a callback to resolve the dependency.
For example:
The class constructor that will receive the contextual binding
The dependency class that the parent asks for
Factory function to resolve the contextual binding
The container class exposes the API to register bindings, values and resolve them.
Known bindings types can be defined at the time of the constructing the container.
You can resolve bindings and construct classes as follows