Home Reference Source
public class | source

ServiceContainer

Manages services in the application.

Constructor Summary

Public Constructor
public

Constructor.

Member Summary

Public Members
public get

mainBindings: Iterator<string>

Returns an iterator containing the main service bindings that have been registered.

Private Members
private

aliases: StringMap<string>

The identifier aliases in this container.

private

services: StringMap<Service>

The services stored in this container.

Method Summary

Public Methods
public

bindProviders(providers: ...serviceProvider): Dispatcher

Binds services to the service container via service providers.

public

construct(identifier: string | string[], builder: Function): ServiceContainer

Binds a service to the container which is rebuilt each time it is fetched.

public

get(identifier: string): *

Fetches a service from the container, constructing it if necessary.

public

has(identifier: string): boolean

Determines if an identifier has been bound in the container.

public

instance(identifier: string | string[], value: *): ServiceContainer

Binds a service to the container which is returned as a value each time it is fetched.

public

singleton(identifier: string | string[], builder: Function): ServiceContainer

Binds a service to the container which is built once and cached for subsequent fetches.

public

Removes a binding from the container, along with its aliases.

Private Methods
private

addService(type: string, identifier: string | string[], service: *): ServiceContainer

Adds a service to the container.

private

Returns the main bound identifier for an alias.

Public Constructors

public constructor() source

Constructor.

Public Members

public get mainBindings: Iterator<string> source

Returns an iterator containing the main service bindings that have been registered.

Private Members

private aliases: StringMap<string> source

The identifier aliases in this container.

private services: StringMap<Service> source

The services stored in this container.

Public Methods

public bindProviders(providers: ...serviceProvider): Dispatcher source

Binds services to the service container via service providers.

Params:

NameTypeAttributeDescription
providers ...serviceProvider

The service providers.

Return:

Dispatcher

The instance this method was called on.

public construct(identifier: string | string[], builder: Function): ServiceContainer source

Binds a service to the container which is rebuilt each time it is fetched.

Params:

NameTypeAttributeDescription
identifier string | string[]

The identifier of the service. If given an array, additional identifiers are added as service aliases.

builder Function

The function which will build the service.

Return:

ServiceContainer

The instance this method was called on.

Throw:

TypeError

Thrown if the builder is not a function.

public get(identifier: string): * source

Fetches a service from the container, constructing it if necessary.

Params:

NameTypeAttributeDescription
identifier string

The identifier of the service to fetch.

Return:

* (nullable: true)

The service requested, or null if no service was bound under that identifier.

public has(identifier: string): boolean source

Determines if an identifier has been bound in the container.

Params:

NameTypeAttributeDescription
identifier string

The identifier.

Return:

boolean

True if the identifier was bound.

public instance(identifier: string | string[], value: *): ServiceContainer source

Binds a service to the container which is returned as a value each time it is fetched.

Params:

NameTypeAttributeDescription
identifier string | string[]

The identifier of the service. If given an array, additional identifiers are added as service aliases.

value *

The value to store as an instance.

Return:

ServiceContainer

The instance this method was called on.

public singleton(identifier: string | string[], builder: Function): ServiceContainer source

Binds a service to the container which is built once and cached for subsequent fetches.

Params:

NameTypeAttributeDescription
identifier string | string[]

The identifier of the service. If given an array, additional identifiers are added as service aliases.

builder Function

The function which will build the service.

Return:

ServiceContainer

The instance this method was called on.

Throw:

TypeError

Thrown if the builder is not a function.

public unbind(identifier: string): ServiceContainer source

Removes a binding from the container, along with its aliases.

Params:

NameTypeAttributeDescription
identifier string

The identifier or an alias for the service.

Return:

ServiceContainer

The instance this method was called on.

Private Methods

private addService(type: string, identifier: string | string[], service: *): ServiceContainer source

Adds a service to the container.

Params:

NameTypeAttributeDescription
type string

The type of service being added.

identifier string | string[]

The identifier of the service. If given an array, additional identifiers are added as service aliases.

service *

The service being added.

Return:

ServiceContainer

The instance this method was called on.

Throw:

Error

Thrown if the service type is invalid.

private getMainBinding(alias: string): string | undefined source

Returns the main bound identifier for an alias. If the alias is already a main bound identifier, that identifier is returned directly.

Params:

NameTypeAttributeDescription
alias string

The alias.

Return:

string | undefined

The main bound identifier for the given alias, or undefined if this alias was not previously bound.