Container

flitter-di/src/Container~ Container

Manages service definitions, instances, and deferred injection.


Constructor

new Container(definitions)

Instantiates the container.

Parameters:
Name Type Description
definitions object

mapping of service name to static service CLASS definition

Members

deferred_classes :Array.<*>

Array of static class definitions with deferred services. These static definitions are waiting for a service to be registered with this container so it can be injected into the prototype and instances.

Type:
  • Array.<*>

definitions :object

Static IoC item definitions from which instances are created or singleton values are returned when the items are requested. Should be mapping of item_name -> {type: Symbol, ref: *}. definition pairs.

Type:
  • object

di :boolean|module:flitter-di/src/DependencyInjector~DependencyInjector

Instance of the dependency injector this container is associated with. If this is specified, services will be injected with other services when they are instantiated.

instances :object

Instantiated services. If a service has already been requested, it is stored here so that the single instance can be reused.

Type:
  • object

statics :object

Already injected static service definitions. These are used to resolve circular dependencies.

Type:
  • object

Methods

(private) __is_deferrable(Class) → {boolean}

Checks if a class is deferrable. That is, does it have the requirements for functioning with the defer logic. In almost all cases, these should be satisfied by having the Class extend from Injectable.

Parameters:
Name Type Description
Class *

the static class to check

Returns:
  • true if the class is deferrable
Type
boolean

(private) _process_deferral(item_name, item)

Process deferred classes that need the provided service name and instance.

Parameters:
Name Type Description
item_name string

the referential name of the IoC item

item module:flitter-di/src/Service~Service | *

the Service or item to be injected

defer(Class)

Defer a static class to have its missing IoC items filled in as they become available in the service container. The class should extend from Injectable.

Parameters:
Name Type Description
Class *

the static class to be deferred

get(name) → {module:flitter-di/src/Service~Service|*}

Fetch a container item by name. It it is an injectable item, it will be injected and instantiated before return.

Parameters:
Name Type Description
name string

the name of the IoC item

Returns:
  • the service instance or singleton item
Type
module:flitter-di/src/Service~Service | *

has(service) → {boolean}

Check if a service definition exists in this container.

Parameters:
Name Type Description
service string

the name of the service

Returns:
  • true if the service definition exists in this container
Type
boolean

proxy() → {Object}

Get the container proxy. Allows accessing IoC items by name.

Returns:
Type
Object

register_service(service_name, service_class)

Register a service class with the container. Allows the service to be requested and it will be instantiated and injected by the container.

Parameters:
Name Type Description
service_name string
service_class

register_singleton(singleton_name, value)

Register an item as a singleton with the container.

Parameters:
Name Type Description
singleton_name string
value *

the value tobe returned by the container

service(name) → {module:flitter-di/src/Service~Service|*}

Fetch a container item by name.

Parameters:
Name Type Description
name string
Deprecated:
  • Please use Container.get from now on. This will be removed in the future.