Scope for binding values
Type of the binding source
Binding represents an entry in the Context. Each binding has a key and a
corresponding value getter.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| isLocked |
boolean
|
constructorProperty exported public |
Binding represents an entry in the |
|
| key |
string
|
exported public |
Key of the binding |
|
| scope |
BindingScope
|
BindingScope.TRANSIENT | exported public |
Scope of the binding to control how the value is cached/shared |
| tagMap |
TagMap
|
exported public |
Map for tag name/value pairs |
|
| type |
BindingType
|
exported public |
Type of the binding value getter |
|
| valueConstructor |
Constructor<T>
|
exported public |
For bindings bound via toClass, this property contains the constructor function |
Get an array of tag names
This is an internal function optimized for performance.
Users should use @inject(key) or ctx.get(key) instead.
Get the value bound to this key. Depending on isSync, this
function returns either:
Consumers wishing to consume sync values directly should use isPromise
to check the type of the returned value to decide how to handle it.
const result = binding.getValue(ctx);
if (isPromise(result)) {
result.then(doSomething)
} else {
doSomething(result);
}
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| ctx |
Context
|
Context for the resolution |
||
| session |
ResolutionSession
|
optional |
Optional session for binding and dependency resolution |
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| scope |
BindingScope
|
Binding represents an entry in the |
Tag the binding with names or name/value objects. A tag has a name and an optional value. If not supplied, the tag name is used as the value.
Bind the key to a constant value. The value must be already available at binding time, it is not allowed to pass a Promise instance.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| value |
T
|
The bound value. |
Bind the key to an instance of the given class.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| ctor |
Constructor<T>
|
The class constructor to call. Any constructor
arguments must be annotated with |
Bind the key to a computed (dynamic) value.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| factoryFn |
anonymous
|
The factory function creating the value. Both sync and async functions are supported. |
Bind the key to a value computed by a Provider.
export class DateProvider implements Provider<Date> {
constructor(@inject('stringDate') private param: String){}
value(): Date {
return new Date(param);
}
}
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| providerClass |
Constructor<Provider<T>>
|
Binding represents an entry in the |
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| PROPERTY_SEPARATOR |
|
"#" | static exported |
Get a binding address for retrieving a deep property of the object bound to the current binding key.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| propertyPath |
string
|
A dot-separated path to a (deep) property, e.g. "server.port". |
Create a new key for a binding bound to a value of type ValueType.
Example
BindingKey.create<string>('application.name');
BindingKey.create<number>('config', 'rest.port);
BindingKey.create<number>('config#rest.port');
Parse a string containing both the binding key and the path to the deeply nested property to retrieve.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| keyWithPath |
BindingAddress<T>
|
The key with an optional path, e.g. "application.instance" or "config#rest.port". |
Validate the binding key format. Please note that # is reserved.
Returns a string representation of the binding key.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<T>
|
Binding key, such as |
Context provides an implementation of Inversion of Control (IoC) container
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| name |
string
|
exported |
Name of the context |
Create a new context
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| _parent |
Context | string
|
optional |
The optional parent context |
|
| name |
undefined | string
|
optional |
Create a new context |
Create a binding with the given key in the context. If a locked binding already exists with the same key, an error will be thrown.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<ValueType>
|
Binding key |
Check if a binding exists with the given key in the local context without delegating to the parent context
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<ValueType>
|
Binding key |
Find bindings using the key pattern
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| pattern |
string | RegExp
|
optional |
A regexp or wildcard pattern with optional
|
Find bindings using a filter function
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| filter |
anonymous
|
A function to test on the binding. It returns `true` to include the binding or `false` to exclude the binding. |
Find bindings using the tag filter. If the filter matches one of the binding tags, the binding is included.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| tagFilter |
string | RegExp | TagMap
|
A filter for tags. It can be in one of the following forms:
|
Get the value bound to the given key, throw an error when no value was bound for the given key.
A promise of the bound value.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| keyWithPath |
BindingAddress<ValueType>
|
The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve. |
Get the value bound to the given key, optionally return a (deep) property of the bound value.
A promise of the bound value, or a promise of undefined when the optional binding was not found.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| keyWithPath |
BindingAddress<ValueType>
|
The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve. |
||
| optionsOrSession |
ResolutionOptions | ResolutionSession
|
optional |
Options or session for resolution. An instance of `ResolutionSession` is accepted for backward compatibility. |
Look up a binding by key in the context and its ancestors. If no matching binding is found, an error will be thrown.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<ValueType>
|
Binding key |
Look up a binding by key in the context and its ancestors. If no matching
binding is found and options.optional is not set to true, an error will
be thrown.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<ValueType>
|
Binding key |
||
| options |
undefined | anonymous
|
optional |
Options to control if the binding is optional. If `options.optional` is set to true, the method will return `undefined` instead of throwing an error if the binding key is not found. |
Get the owning context for a binding key
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<ValueType>
|
Binding key |
Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value.
This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.
A promise of the bound value.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| keyWithPath |
BindingAddress<ValueType>
|
The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.
|
Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value.
This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.
The bound value, or undefined when an optional binding was not found.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| keyWithPath |
BindingAddress<ValueType>
|
The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve. * @param optionsOrSession Options or session for resolution. An instance of `ResolutionSession` is accepted for backward compatibility. |
||
| optionsOrSession |
ResolutionOptions | ResolutionSession
|
optional |
Context provides an implementation of Inversion of Control (IoC) container |
Get the value bound to the given key.
This is an internal version that preserves the dual sync/async result
of Binding#getValue(). Users should use get() or getSync() instead.
The bound value or a promise of the bound value, depending on how the binding was configured.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| keyWithPath |
BindingAddress<ValueType>
|
The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve. |
||
| optionsOrSession |
ResolutionOptions | ResolutionSession
|
optional |
Options for resolution or a session |
Check if a key is bound in the context or its ancestors
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<ValueType>
|
Binding key |
Create a plain JSON object for the context
Unbind a binding from the context. No parent contexts will be checked. If you need to unbind a binding owned by a parent context, use the code below:
const ownerCtx = ctx.getOwnerContext(key);
return ownerCtx != null && ownerCtx.unbind(key);
true if the binding key is found and removed from this context
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| key |
BindingAddress<ValueType>
|
Binding key |
Inject the context object.
Inject a function for getting the actual bound value.
This is useful when implementing Actions, where the action is instantiated for Sequence constructor, but some of action's dependencies become bound only after other actions have been executed by the sequence.
See also Getter<T>.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| bindingKey |
BindingAddress<BoundValue>
|
The key of the value we want to eventually get. |
||
| metadata |
InjectionMetadata
|
optional |
Optional metadata to help the injection |
Inject a function for setting (binding) the given key to a given value. (Only static/constant values are supported, it's not possible to bind a key to a class or a provider.)
This is useful e.g. when implementing Actions that are contributing new Elements.
See also Setter<T>.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| bindingKey |
BindingAddress<BoundValue>
|
The key of the value we want to set. |
||
| metadata |
InjectionMetadata
|
optional |
Optional metadata to help the injection |
Inject an array of values by a tag pattern string or regexp
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| bindingTag |
string | RegExp
|
Tag name or regex |
||
| metadata |
InjectionMetadata
|
optional |
Optional metadata to help the injection |
Descriptor for an injection point
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| bindingKey |
BindingAddress<ValueType>
|
exported |
Descriptor for an injection point |
|
| member |
string | symbol
|
exported optional |
Descriptor for an injection point |
|
| metadata |
InjectionMetadata
|
exported optional |
Descriptor for an injection point |
|
| methodDescriptorOrParameterIndex |
TypedPropertyDescriptor<ValueType> | number
|
exported optional |
Descriptor for an injection point |
|
| resolve |
ResolverFunction
|
exported optional |
Descriptor for an injection point |
|
| target |
Object
|
exported |
Descriptor for an injection point |
An object to provide metadata for @inject
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| decorator |
undefined | string
|
exported optional |
Name of the decorator function, such as |
|
| optional |
undefined | true | false
|
exported optional |
Control if the dependency is optional, default to false |
A function to provide resolution of injected values
The function injected by `@inject.getter(key)`.
The function injected by `@inject.setter(key)`.
| Name | Type | Description |
|---|---|---|
| value |
T
|
Return an array of injection objects for parameters
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| target |
Object
|
The target class for constructor or static methods, or the prototype for instance methods |
||
| method |
string | symbol
|
optional |
Method name, undefined for constructor |
Return a map of injection objects for properties
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| target |
Object
|
The target class for static properties or prototype for instance properties. |
Providers allow developers to compute injected values dynamically, with any dependencies required by the value getter injected automatically from the Context.
The value to inject to dependents. This method can return a promise too, in which case the IoC framework will resolve this promise to obtain the value to inject.
Object to keep states for a session to resolve bindings and their dependencies within a context
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| stack |
ResolutionElement[]
|
[] | exported |
A stack of bindings for the current resolution session. It's used to track the path of dependency resolution and detect circular dependencies. |
Getter for bindings on the stack
Getter for the current binding
Getter for the current injection
Getter for injections on the stack
Get the binding path as bindingA --> bindingB --> bindingC.
Get the injection path as injectionA --> injectionB --> injectionC.
Get the resolution path including bindings and injections, for example:
bindingA --> @ClassA[0] --> bindingB --> @ClassB.prototype.prop1
--> bindingC.
Exit the resolution of a binding
Pop the last injection
Enter the resolution of the given binding. If
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| binding |
Readonly<Binding>
|
Binding |
Push the injection onto the session
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| injection |
Readonly<Injection>
|
Injection The current injection |
Describe the injection for debugging purpose
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| injection |
Readonly<Injection>
|
optional |
Injection object |
Fork the current session so that a new one with the same stack can be used in parallel or future resolutions, such as multiple method arguments, multiple properties, or a getter function
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| session |
ResolutionSession
|
optional |
The current session |
Run the given action with the given binding and session
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| action |
ResolutionAction
|
A function to do some work with the resolution session |
||
| binding |
Readonly<Binding>
|
The current binding |
||
| session |
ResolutionSession
|
optional |
The current resolution session |
Run the given action with the given injection and session
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| action |
ResolutionAction
|
A function to do some work with the resolution session |
||
| injection |
Readonly<Injection>
|
Object to keep states for a session to resolve bindings and their dependencies within a context |
||
| session |
ResolutionSession
|
optional |
The current resolution session |
Wrapper for bindings tracked by resolution sessions
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| type |
|
exported |
Wrapper for bindings tracked by resolution sessions |
|
| value |
Readonly<Binding>
|
exported |
Wrapper for bindings tracked by resolution sessions |
Wrapper for injections tracked by resolution sessions
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| type |
|
exported |
Wrapper for injections tracked by resolution sessions |
|
| value |
Readonly<Injection>
|
exported |
Wrapper for injections tracked by resolution sessions |
Options for binding/dependency resolution
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| optional |
undefined | true | false
|
exported optional |
A boolean flag to indicate if the dependency is optional. If it's set to
|
|
| session |
ResolutionSession
|
exported optional |
A session to track bindings and injections |
A function to be executed with the resolution session
| Name | Type | Description |
|---|---|---|
| session |
ResolutionSession
|
Create an instance of a class which constructor has arguments
decorated with @inject.
The function returns a class when all dependencies were resolved synchronously, or a Promise otherwise.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| ctor |
Constructor<T>
|
The class constructor to call. |
||
| ctx |
Context
|
The context containing values for |
||
| session |
ResolutionSession
|
optional |
Optional session for binding and dependency resolution |
|
| nonInjectedArgs |
any[]
|
optional |
Optional array of args for non-injected parameters |
Invoke an instance method with dependency injection
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| target |
Object
|
Target of the method, it will be the class for a static method, and instance or class prototype for a prototype method |
||
| method |
string
|
Name of the method |
||
| ctx |
Context
|
Context |
||
| nonInjectedArgs |
any[]
|
optional |
Optional array of args for non-injected parameters |
Given a function with arguments decorated with @inject,
return the list of arguments resolved using the values
bound in ctx.
The function returns an argument array when all dependencies were resolved synchronously, or a Promise otherwise.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| target |
Object
|
The class for constructor injection or prototype for method injection |
||
| method |
string
|
The method name. If set to '', the constructor will be used. |
||
| ctx |
Context
|
The context containing values for |
||
| session |
ResolutionSession
|
optional |
Optional session for binding and dependency resolution |
|
| nonInjectedArgs |
any[]
|
optional |
Optional array of args for non-injected parameters |
Given a class with properties decorated with @inject,
return the map of properties resolved using the values
bound in ctx.
The function returns an argument array when all dependencies were resolved synchronously, or a Promise otherwise.
| Name | Type | Default Value | Flags | Description |
|---|---|---|---|---|
| constructor |
Function
|
The class for which properties should be resolved. |
||
| ctx |
Context
|
The context containing values for |
||
| session |
ResolutionSession
|
optional |
Optional session for binding and dependency resolution |