@absolunet/ioc2.1.0

View on GitHub

Container

Inversion of Control container that allows bindings and factory with dependency injection, exposes tags, aliases and other IoC features.

Extends

Methods

(static) make()

Source:
Make a new Container instance.
Returns:
A container instance.
Type
container.Container

(static) getInstance()

Source:
Get the current Container instance or create a new one.
Returns:
The current instance or a newly created instance if no instance exists.
Type
container.Container

(static) setDefaultInstance(instance)

Source:
Set the current Container instance.
Parameters:
Name Type Description
instance container.Container A Container instance.
Throws:
Indicates that the default instance was not a container instance.
Type
TypeError

setContext(context)

Source:
Set JavaScript module context.
Parameters:
Name Type Description
context module The Node.js module that represents the current context.
Returns:
The current container instance.
Type
container.Container

getContext()

Source:
Get current JavaScript module context.
Returns:
The Node.js module that represents the current context.
Type
module

bind(abstract, concrete, shared)

Source:
Bind abstract to the container.
Parameters:
Name Type Default Description
abstract string Abstract representation of a concrete. Should be treated as an interface.
concrete function | * Concrete reflecting the abstract. Can be either a class, a factory closure, an instance or even a primitive value, such as a string or a boolean.
shared boolean false Indicates that the concrete should be treated as a singleton and be shared through all the other instances when requested.
Returns:
The current container instance.
Type
container.Container

singleton(abstract, concrete)

Source:
Bind abstract as a singleton to the container.
Parameters:
Name Type Description
abstract string Abstract representation of a concrete. Should be treated as an interface.
concrete function | * Concrete reflecting the abstract. Can be either a class, a factory closure, an instance or even a primitive value, such as a string or a boolean.
Returns:
The current container instance.
Type
container.Container

make(abstract, parametersopt)

Source:
Resolve a given argument with either its singleton, a new instance based on bindings or a new instance with resolved dependencies.
Parameters:
Name Type Attributes Default Description
abstract * An abstract that was bound to the container, or a class, closure or instance that can be built by the container.
parameters object.<string, *> <optional>
{} Additional arguments to inject into the concrete when instantiating.
Returns:
The instantiated or the singleton concrete.
Type
*

resolve(abstract, parametersopt)

Source:
Resolve a given abstract to build a new instance.
Parameters:
Name Type Attributes Default Description
abstract * An abstract that was bound to the container, or a class, closure or instance that can be built by the container.
parameters object.<string, *> <optional>
{} Additional arguments to inject into the concrete when instantiating.
Returns:
The instantiated concrete.
Type
*

isBound(abstract)

Source:
Check if the given abstract is bound to the container.
Parameters:
Name Type Description
abstract string The abstract to check.
Returns:
Indicates if the abstract is bound in the container.
Type
boolean

getBounds()

Source:
Get all bindings.
Returns:
List of abstracts bound into the container.
Type
Array.<string>

getSingleton(abstract)

Source:
Get singleton from its abstract.
Parameters:
Name Type Description
abstract string The abstract name that reflects a singleton already instantiated.
Returns:
The singleton instance.
Type
*

isSingleton(abstract)

Source:
Check if a given abstract has a resolved singleton.
Parameters:
Name Type Description
abstract string The abstract name that may reflect an instantiated singleton.
Returns:
Indicates that the singleton exists and was already instantiated.
Type
boolean

instantiate(Concrete, parametersopt)

Source:
Instantiate a given class and resolve its dependencies.
Parameters:
Name Type Attributes Default Description
Concrete function A class that can be instantiated.
parameters object.<string, *> <optional>
{} Additional arguments to inject into the class instance when instantiating.
Returns:
The newly created instance.
Type
*

call(factory, parametersopt)

Source:
Call a given function with the given arguments.
Parameters:
Name Type Attributes Default Description
factory function A closure that factories a concrete.
parameters object.<string, *> <optional>
{} Additional arguments to inject into the factory when calling it.
Returns:
The factoried concrete.
Type
*

assign(object, parametersopt)

Source:
Make a mass assignment to a given object.
Parameters:
Name Type Attributes Default Description
object * An instance that can receive parameters by assignation.
parameters object.<string, *> <optional>
{} Additional arguments to associate to the instance.
Returns:
The instance.
Type
*

build(concrete, parametersopt)

Source:
Build a given concrete, either a factory, a class or an object.
Parameters:
Name Type Attributes Default Description
concrete function | * A concrete that can be either instantiated, called or assigned.
parameters object.<string, *> <optional>
{} Additional arguments to inject into the concrete when instantiating.
Returns:
The built concrete.
Type
*

decorate(abstract, decorator)

Source:
Decorate a given abstract with a callback.
Parameters:
Name Type Description
abstract string The abstract to decorate.
decorator function The decorator function.
Returns:
The current container instance.
Type
container.Container

tag(abstract, tag)

Source:
Tag a given abstract.
Parameters:
Name Type Description
abstract string | Array.<string> The abstract(s) to tag.
tag string The tag to give to the abstract(s).
Returns:
The current container instance.
Type
container.Container

alias(alias, abstract)

Source:
Alias an abstract.
Parameters:
Name Type Description
alias string The alias to given.
abstract string The abstract to associate to the alias.
Returns:
The current container instance.
Type
container.Container

isTag(tag)

Source:
Check if the given string was used as a tag.
Parameters:
Name Type Description
tag string The tag name.
Returns:
Indicates if the tag exists.
Type
boolean

getTagged(tag)

Source:
Get tagged dependencies from the tag name.
Parameters:
Name Type Description
tag string The tag name.
Returns:
The instances associated to the given tag, in a dictionary, mapping the abstract name to the concrete.
Type
object.<string, *>

flush()

Source:
Flush container from attached abstracts and concretes.
Returns:
The current container instance.
Type
container.Container

bindingNotFound(abstract)

Source:
Throw an error announcing that the given abstract was not found.
Parameters:
Name Type Description
abstract string The abstract that was not found.
Throws:
Indicates that the given abstract was not found in the container.
Type
TypeError

getModule(filePath)

Source:
Check if the given file is a valid and existing JavaScript file with a valid extension.
Parameters:
Name Type Description
filePath string The file path to load.
Throws:
Indicates that an error occurred during file loading or parsing.
Type
Error
Returns:
The file parsed value, or null if not found.
Type
*