The UFService implements a dependency injection mechanism.

Services are registered with a name using registerFactory, registerConstructor, registerSingletonFactory, registerSingletonConstructor, registerStatic.

A service may depend on other services. When a service is requested, the dependent services are injected into the service provider function (either a factory or constructor function).

Use getInstance to get a service.

To inject services into a call use call.

To construct a class (via new) injecting services into the constructor function use construct.

Add '()' to a service name to get a factory function (no parameters) that can be used to create instances for that service. The factory function will call getInstance to get the service instance.

Example

// some model and controller class
class MainModel {
// ...
}
class MainController {
// ...
}
// a view for an enemy
class EnemyView {
// ...
}
// register services
UFService.registerConstructor('game.models.MainModel', MainModel);
UFService.registerConstructor('game.controllers.MainController', MainController);
UFService.registerConstructor('game.views.Enemy', EnemyView);

Example

// some factory function to create a view
function viewFactory(aMainData, aMainController) {
// create view
return {
// some public methods
};
}

const viewInstance = UF.callWithServices(
viewFactory, ['game.models.MainModel', 'game.controllers.MainController']
);

Example

// Creates a view with factory function
function viewFactory(aMainData, aMainController, anEnemyFactory) {
const enemies = [];
// ...
function createEnemies(aCount) {
for(let index = 0; index < aCount; index++) {
enemies.push(anEnemyFactory());
}
}
return {
// some public methods
};
}
// note the () with last service name
const viewInstance = UF.callWithServices(
viewFactory, ['game.models.MainModel', 'game.controllers.MainController', 'game.views.Enemy()']
);

Constructors

Methods

  • Calls a function injecting service instances / factories as arguments.

    Type Parameters

    • T

    Parameters

    • aFunction: ((...args) => T)

      Function to call

        • (...args): T
        • Parameters

          • Rest ...args: any[]

          Returns T

    • aServices: string[]

      Services to inject.

    Returns T

    Result from function call

  • Constructs a class (via new) by injecting service instances / factories into the constructor function.

    Type Parameters

    • T extends object

    Parameters

    • aConstructor: (new (...args) => T)

      Constructor function

        • new (...args): T
        • Parameters

          • Rest ...args: any[]

          Returns T

    • aServices: string[]

      Services to inject.

    Returns T

    instance of class defined by aConstructor

  • Gets a service instance for a certain service.

    Type Parameters

    • T extends object

    Parameters

    • aName: string

      Service name

    Returns T

    an instance implementing the service.

  • Checks if a service of certain name exists.

    Parameters

    • aName: string

      Name of service

    Returns boolean

    True if there is a service.

  • Registers a service that is an instance of some class. The service is created by using new on the constructor function injecting the dependent types.

    Parameters

    • aName: string

      Name of service

    • aConstructor: (new (...args) => object)

      Constructor function that creates the service.

        • new (...args): object
        • Parameters

          • Rest ...args: any[]

          Returns object

    • aServices: string[] = []

      Name of other services that should be resolved an injected into the constructor function. The order of the service names is the order in which they are injected.

    Returns void

  • Registers a service that is created using a factory function. The service is created by invoking the factory function injecting the dependent types.

    Parameters

    • aName: string

      Name of service

    • aFactory: ((...args) => object)

      Factory function that creates the service.

        • (...args): object
        • Parameters

          • Rest ...args: any[]

          Returns object

    • aServices: string[] = []

      Name of other services that should be resolved an injected into the factory function. The order of the service names is the order in which they are injected.

    Returns void

  • Registers a service that is an instance of some class. The first time the service is requested, it is created by using new on the constructor function injecting the dependent types.

    Subsequent requests for the service will return the same instance.

    Parameters

    • aName: string

      Name of service

    • aConstructor: (new (...args) => object)

      Constructor function that creates the service.

        • new (...args): object
        • Parameters

          • Rest ...args: any[]

          Returns object

    • aServices: string[] = []

      Name of other services that should be resolved an injected into the constructor function. The order of the service names is the order in which they are injected.

    Returns void

  • Registers a service that is created using a factory function. The first time the service is requested, it is created by invoking the factory function injecting the dependent types.

    Subsequent requests for the service will return the same instance.

    Parameters

    • aName: string

      Name of service

    • aFactory: ((...args) => object)

      Factory function that creates the service.

        • (...args): object
        • Parameters

          • Rest ...args: any[]

          Returns object

    • aServices: string[] = []

      Name of other services that should be resolved an injected into the factory function. The order of the service names is the order in which they are injected.

    Returns void

  • Registers a service that is already created.

    Parameters

    • aName: string

      Name of service

    • anObject: object

      Object that wil be returned when the service is requested.

    Returns void