Class: DI

DI(descriptoropt)

DI-XXL is a very generic Dependency Injection (DI) library, facilitating lazy initialization and loose coupling. It is generic, it can inject everything into anything in multiple ways. Together with support for namespaces, decorators and factory functions it is a powerful tool ready for complex projects.

Constructor

new DI(descriptoropt)

Use an instance if changes should be kept encapsulated and only accessible by that instance. Its best use case is when temporary projection (See DI.setProjection) are needed. In any other case simply use DI directly. The values of the descriptor are used as defaults anywhere in the library where descriptors are required (e.g. DI.get)

Parameters:
Name Type Attributes Description
descriptor object <optional>

Descriptor used as the base for all other descriptors

Properties
Name Type Attributes Description
accept array <optional>

List of roles which are allowed to be injected

action number <optional>

The action to be applied to the entity when requested (see DI.ACTIONS)

inherit string <optional>

Descriptor properties of the referenced entity are used as defaults too

inject array <optional>

List of entity names to be injected

name string <optional>

Entity name, used to access or inject the entity

params array | object <optional>

List of parameters used to initialise/call the entity

reject string <optional>

List of roles which are not allowed to be injected

ref string <optional>

The entity (e.g.: class or function, sting)

role string <optional>

Role of the entity (e.g: Service or Component)

singleton boolean <optional>

Turns the entity into a singleton (Default for object entities)

lookup number <optional>

Lookup direction. See DI.DIRECTIONS (default: PARENT_TO_CHILD)

Source:

Members

(static, readonly) ACTIONS :number

List of actions applicable on the entities when requested (DI.get). For example, if the entity is a class, you probably want to return an instance of that class, so use DI.ACTIONS.CREATE.

Type:
  • number
Properties:
Name Type Description
CREATE number

Create an instance using new (Default if the entity is a function/class)

INVOKE number

Execute the function

NONE number

Do nothing (Default if the entity is an object)

Source:
Example
class Foo {}

DI.set({
    name: 'foo',
    ref: Foo,
    action: DI.ACTIONS.CREATE
});

function Store() { ... }

DI.set({
    name: 'app',
    ref: Store,
    action: DI.ACTIONS.INVOKE
});

const App = {};

DI.set({
    name: 'App',
    ref: App,
    action: DI.ACTIONS.NONE
});

(static, readonly) DIRECTIONS :number

Lookup direction for namespace traversal.

Type:
  • number
Properties:
Name Type Description
PARENT_TO_CHILD number

Upwards (Capturing)

CHILD_TO_PARENT number

Downwards (Bubbling)

Source:
Example
DI.set({
       name: 'a.b.c.d.Foo',
       lookup: DI.DIRECTIONS.CHILD_TO_PARENT
       ...
   });
}

Methods

(static) get(name, configopt) → {*}

Returns the processed entity using of DI.ACTIONS. Use config to overwrite one or more descriptor values. Below is an example in which params is replaced.

Parameters:
Name Type Attributes Description
name string

name of the descriptor

config object <optional>

configuration

Properties
Name Type Attributes Description
lookup number <optional>

Direction of namespace traversal (See DI.DIRECTIONS)

params array <optional>

List of arguments (e.g: used to create an instance)

Source:
Returns:

The processed entity (See DI.ACTIONS)

Type
*
Example
class Foo {
    constructor(base) { this.base = base; }
    addToBase(num) { return this.base + num; }
}

const descriptor = {
    name: 'foo',
    ref: Foo,
    action: DI.ACTIONS.CREATE,
    params: [100]
};
di.set(descriptor);

di.get('foo').addToBase(1); // --> 101
di.get('foo', {params: [1]}).addToBase(1); // --> 2

(static) getDescriptor(name) → {object}

Returns the descriptor identified by the given name. However, it will not traverse the namespace

Parameters:
Name Type Description
name string

Entity name

Source:
Returns:

descriptor

Type
object

(static) getFactory(name, config) → {function}

Returns a factory for the given entity name

Parameters:
Name Type Description
name string

Entity name

config object

Descriptor defaults (Checkout DI#constructor for all descriptor properties)

Source:
Returns:
Type
function
Example
class Foo {
    constructor(val) { this.val = val; }
}

DI.set({name: 'foo', ref: Foo, params: [1]});
const factory = DI.getFactory();
let foo = factory(); // foo.val === 1
foo = factory(2); // foo.val === 2

DI.getFactory(10)().val === 10
DI.getFactory(10)(20).val === 20

(static) getProjection(name, nsopt) → {object}

Returns the projection identified by the given name. However, it will not traverse the namespace

Parameters:
Name Type Attributes Description
name string

entity name (it can include the namespace)

ns string <optional>

namespace

Source:
Returns:

descriptor

Type
object

(static) lookupDescriptor(name, configopt) → {object}

This function is identical to DI.getDescriptor except it will traverse the namespace until it finds it or reaches the end of the namespace.

Parameters:
Name Type Attributes Description
name
config object <optional>

Configuration of the lookup process

Properties
Name Type Attributes Description
lookup object <optional>

Lookup direction (See DI.DIRECTIONS)

Source:
Returns:

Descriptor

Type
object
Example
const descriptor = {
        name: 'a.b.foo',
        ...
    }

    DI.lookupDescriptor('a.b.c.d.e.foo', {lookup: DI.DIRECTIONS.PARENT_TO_CHILD}); // will find `a.b.foo`

(static) removeDescriptor(name) → {function}

Remove a descriptor/entity

Parameters:
Name Type Description
name string

entity name

Source:
Returns:

DI class

Type
function
Example
DI.set({name: 'foo', ref: Foo});
DI.removeDescriptor('foo');
DI.get('foo')

(static) removeProjection(key, projections) → {DI}

Remove one projection

Parameters:
Name Type Description
key
projections
Source:
Returns:
Type
DI
Example
DI.setProjection({foo: 'bar'}); // --> foo is projected to bar
DI.removeProjection('foo');     // --> remove the above projection

(static) set(descriptor) → {function}

Register an entity

Parameters:
Name Type Description
descriptor descriptor

defaults (Checkout DI.constructor for all descriptor properties)

Source:
Returns:

DI class

Type
function

(static) setProjection(list) → {DI}

Define one or projection

Parameters:
Name Type Description
list Object

Projections with the key being the entity name to be replaced by its value

Source:
Returns:
Type
DI
Example
DI.setProjection( { foo: 'bar', baz: 'a.b.mooz'} ); // 'foo' being projected to 'bar'
DI.get('foo') instanceof Bar

get()

SeeDI.get

Source:

getDescriptor()

Source:

getFactory()

Source:

getProjection()

Source:

lookupDescriptor()

Source:

removeDescriptor() → {object}

Source:
Returns:

DI instance

Type
object

removeProjection() → {object}

Source:
Returns:

DI instance

Type
object

set() → {object}

See DI.set

Source:
Returns:

DI instance

Type
object

setProjection() → {object}

See DI.seProjection

Source:
Returns:

DI instance

Type
object