| Name | Type | Description |
|---|---|---|
| isLocked |
boolean
|
|
| key |
string
|
|
| scope |
BindingScope
|
|
| tags |
Set
|
|
| type |
BindingType
|
|
| valueConstructor |
Constructor
|
| Name | Type | Description |
|---|---|---|
| key |
string
|
|
| isLocked |
boolean
|
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 | Description |
|---|---|---|
| ctx |
Context
|
Context for the resolution |
| session |
ResolutionSession
|
Optional session for binding and dependency resolution |
| Name | Type | Description |
|---|---|---|
| scope |
BindingScope
|
| Name | Type | Description |
|---|---|---|
| tagName |
|
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 | Description |
|---|---|---|
| value |
T
|
The bound value. |
Bind the key to an instance of the given class.
| Name | Type | Description |
|---|---|---|
| ctor |
Constructor
|
The class constructor to call. Any constructor
arguments must be annotated with |
Bind the key to a computed (dynamic) value.
| Name | Type | Description |
|---|---|---|
| factoryFn |
Function
|
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 | Description |
|---|---|---|
| providerClass |
Constructor
|
| Name | Type | Description |
|---|---|---|
| PROPERTY_SEPARATOR |
|
Get a binding address for retrieving a deep property of the object bound to the current binding key.
| Name | Type | 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');
| Name | Type | Description |
|---|---|---|
| key |
string
|
The binding key. When propertyPath is not provided, the key
is allowed to contain propertyPath as encoded via |
| propertyPath |
|
Optional path to a deep property of the bound value. |
Parse a string containing both the binding key and the path to the deeply nested property to retrieve.
| Name | Type | Description |
|---|---|---|
| keyWithPath |
BindingAddress
|
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 | Description |
|---|---|---|
| key |
BindingAddress
|
Binding key, such as |
Context provides an implementation of Inversion of Control (IoC) container
| Name | Type | Description |
|---|---|---|
| name |
string
|
Name of the context |
Create a new context
| Name | Type | Description |
|---|---|---|
| _parent |
|
The optional parent context |
| name |
|
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 | Description |
|---|---|---|
| key |
BindingAddress
|
Binding key |
Check if a binding exists with the given key in the local context without delegating to the parent context
| Name | Type | Description |
|---|---|---|
| key |
BindingAddress
|
Binding key |
Find bindings using the key pattern
| Name | Type | Description |
|---|---|---|
| pattern |
|
A regexp or wildcard pattern with optional
|
Find bindings using a filter function
| Name | Type | Description |
|---|---|---|
| filter |
Function
|
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 pattern
| Name | Type | Description |
|---|---|---|
| pattern |
|
A regexp or wildcard pattern with optional
|
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 | Description |
|---|---|---|
| keyWithPath |
BindingAddress
|
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 | Description |
|---|---|---|
| keyWithPath |
BindingAddress
|
The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve. |
| optionsOrSession |
|
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 | Description |
|---|---|---|
| key |
BindingAddress
|
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 | Description |
|---|---|---|
| key |
BindingAddress
|
Binding key |
| options |
|
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 | Description |
|---|---|---|
| key |
BindingAddress
|
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 | Description |
|---|---|---|
| keyWithPath |
BindingAddress
|
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 | Description |
|---|---|---|
| keyWithPath |
BindingAddress
|
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 |
|
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 | Description |
|---|---|---|
| keyWithPath |
BindingAddress
|
The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve. |
| optionsOrSession |
|
Options for resolution or a session |
Check if a key is bound in the context or its ancestors
| Name | Type | Description |
|---|---|---|
| key |
BindingAddress
|
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 | Description |
|---|---|---|
| key |
BindingAddress
|
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 | Description |
|---|---|---|
| bindingKey |
BindingAddress
|
The key of the value we want to eventually get. |
| metadata |
InjectionMetadata
|
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 | Description |
|---|---|---|
| bindingKey |
BindingAddress
|
The key of the value we want to set. |
| metadata |
InjectionMetadata
|
Optional metadata to help the injection |
Inject an array of values by a tag pattern string or regexp
| Name | Type | Description |
|---|---|---|
| bindingTag |
|
Tag name or regex |
| metadata |
InjectionMetadata
|
Optional metadata to help the injection |
Descriptor for an injection point
| Name | Type | Description |
|---|---|---|
| bindingKey |
BindingAddress
|
Descriptor for an injection point |
| member |
|
Descriptor for an injection point |
| metadata |
InjectionMetadata
|
Descriptor for an injection point |
| methodDescriptorOrParameterIndex |
|
Descriptor for an injection point |
| resolve |
ResolverFunction
|
Descriptor for an injection point |
| target |
Object
|
Descriptor for an injection point |
An object to provide metadata for @inject
| Name | Type | Description |
|---|---|---|
| decorator |
|
Name of the decorator function, such as |
| 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 | Description |
|---|---|---|
| target |
Object
|
The target class for constructor or static methods, or the prototype for instance methods |
| method |
|
Method name, undefined for constructor |
Return a map of injection objects for properties
| Name | Type | 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 | Description |
|---|---|---|
| stack |
|
A stack of bindings for the current resolution session. It's used to track the path of dependency resolution and detect circular dependencies. |
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 | Description |
|---|---|---|
| binding |
Readonly
|
Binding |
Push the injection onto the session
| Name | Type | Description |
|---|---|---|
| injection |
Readonly
|
Injection The current injection |
Describe the injection for debugging purpose
| Name | Type | Description |
|---|---|---|
| injection |
Readonly
|
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 | Description |
|---|---|---|
| session |
ResolutionSession
|
The current session |
Run the given action with the given binding and session
| Name | Type | Description |
|---|---|---|
| action |
ResolutionAction
|
A function to do some work with the resolution session |
| binding |
Readonly
|
The current binding |
| session |
ResolutionSession
|
The current resolution session |
Run the given action with the given injection and session
| Name | Type | Description |
|---|---|---|
| action |
ResolutionAction
|
A function to do some work with the resolution session |
| injection |
Readonly
|
Object to keep states for a session to resolve bindings and their dependencies within a context |
| session |
ResolutionSession
|
The current resolution session |
Wrapper for bindings tracked by resolution sessions
| Name | Type | Description |
|---|---|---|
| type |
|
Wrapper for bindings tracked by resolution sessions |
| value |
Readonly
|
Wrapper for bindings tracked by resolution sessions |
Wrapper for injections tracked by resolution sessions
| Name | Type | Description |
|---|---|---|
| type |
|
Wrapper for injections tracked by resolution sessions |
| value |
Readonly
|
Wrapper for injections tracked by resolution sessions |
Options for binding/dependency resolution
| Name | Type | Description |
|---|---|---|
| optional |
|
A boolean flag to indicate if the dependency is optional. If it's set to
|
| session |
ResolutionSession
|
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 | Description |
|---|---|---|
| ctor |
Constructor
|
The class constructor to call. |
| ctx |
Context
|
The context containing values for |
| session |
ResolutionSession
|
Optional session for binding and dependency resolution |
| nonInjectedArgs |
|
Optional array of args for non-injected parameters |
Invoke an instance method with dependency injection
| Name | Type | 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 |
|
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 | 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 session for binding and dependency resolution |
| nonInjectedArgs |
|
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 | Description |
|---|---|---|
| constructor |
Function
|
The class for which properties should be resolved. |
| ctx |
Context
|
The context containing values for |
| session |
ResolutionSession
|
Optional session for binding and dependency resolution |