Initialize the container with optional configuration
Optionaloptions: ContainerOptionsDefine an emitter instance to use
The container instance for chaining
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", or the "bindValue" methods.
True if binding exists, false otherwise
Find if the container has a binding registered using the "bind", the "singleton", or the "bindValue" methods.
True if binding exists, false otherwise
Find if the container has all the bindings registered using the "bind", the "singleton", or the "bindValue" methods.
True if all bindings exist, false otherwise
Find if the container has all the bindings registered using the "bind", the "singleton", or the "bindValue" methods.
True if all bindings exist, false otherwise
Resolves the binding or constructor a class instance as follows.
await container.make('route')
await container.make(Database)
OptionalruntimeValues: any[]OptionalcreateError: ErrorCreatorResolves the binding or constructor a class instance as follows.
await container.make('route')
await container.make(Database)
OptionalruntimeValues: any[]OptionalcreateError: ErrorCreatorCall a method on an object by injecting its dependencies. The method dependencies are resolved in the same manner as a class constructor dependencies.
await container.call(await container.make(UsersController), 'index')
OptionalruntimeValues: any[]OptionalcreateError: ErrorCreatorRegister 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.
An alias must always be defined as a string or a symbol. Classes cannot be aliases
The value should either be the constructor point to the alias value or reference to binding that has the same value as the alias
Sets the alias for the binding
Register a binding inside the container. The method receives a key-value pair.
container.bind('route', () => new Route())
await container.make('route')
container.bind(Route, () => new Route())
await container.make(Route)
const routeSymbol = Symbol('route')
container.bind(routeSymbol, () => new Route())
await container.make(routeSymbol)
Need to narrow down the "Binding" for the case where "KnownBindings" are <any, any>
Register a binding inside the container. The method receives a key-value pair.
container.bind('route', () => new Route())
await container.make('route')
container.bind(Route, () => new Route())
await container.make(Route)
const routeSymbol = Symbol('route')
container.bind(routeSymbol, () => new Route())
await container.make(routeSymbol)
Register a binding as a value
container.bindValue(Route, new Route())
Need to narrow down the "Binding" for the case where "KnownBindings" are <any, any>
Register a binding as a single. The singleton method is same as the bind method, but the factory function is invoked only once.
container.singleton('route', () => new Route())
await container.make('route')
container.singleton(Route, () => new Route())
await container.make(Route)
const routeSymbol = Symbol('route')
container.singleton(routeSymbol, () => new Route())
await container.make(routeSymbol)
Need to narrow down the "Binding" for the case where "KnownBindings" are <any, any>
Register a binding as a single. The singleton method is same as the bind method, but the factory function is invoked only once.
container.singleton('route', () => new Route())
await container.make('route')
container.singleton(Route, () => new Route())
await container.make(Route)
const routeSymbol = Symbol('route')
container.singleton(routeSymbol, () => new Route())
await container.make(routeSymbol)
Define a fake implementation for a binding or a class constructor. Fakes have the highest priority when resolving dependencies from the container.
Restore binding by removing its swap
Restore mentioned or all bindings by removing their swaps
Optionalbindings: AbstractConstructor<any>[]Define hooks to be executed after a binding has been resolved from the container.
The hooks are executed for
In other words, the hooks are not executed for direct values registered with the container
Define hooks to be executed after a binding has been resolved from the container.
The hooks are executed for
In other words, the hooks are not executed for direct values registered with the container
Create a contextual builder to define contextual bindings
Add a contextual binding for a given class constructor. A contextual takes a parent, parent's dependency and a callback to self resolve the dependency.
For example:
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