InjectorService Service

Module
import { InjectorService } from "@tsed/di"
Source/packages/di/src/services/InjectorService.ts

Overview

class InjectorService extends Map<;RegistryKey, Provider<;any>;>; {
    constructor();
    scopes: {
        [key: string]: ProviderScope;
    };
    scopeOf(providerType: ProviderType): ProviderScope;
    settings: IDISettings;
    get<;T>;(target: Type<;T>; | symbol | any): T | undefined;
    has(key: RegistryKey): boolean;
    getProvider(key: RegistryKey): Provider<;any>; | undefined;
    forkProvider(key: RegistryKey, instance?: any): Provider<;any>;;
    getProviders(type?: ProviderType | string): Provider<;any>;[];
    invoke<;T>;(target: any, locals?: Map<;string | Function, any>;, designParamTypes?: any[], requiredScope?: boolean): T;
    invokeMethod(handler: any, options: IInjectableMethod<;any>;): any;
    load(): Promise<;any>;;
    emit(eventName: string, ...args: any[]): Promise<;void>;;
}

Description

This service contain all services collected by @Service or services declared manually with InjectorService.factory() or InjectorService.service().

Example:

import {InjectorService} from "@tsed/common";

// Import the services (all services are decorated with @Service()";
import MyService1 from "./services/service1";
import MyService2 from "./services/service2";
import MyService3 from "./services/service3";

// When all services is imported you can load InjectorService.
const injector = new InjectorService()
injector.load();

const myService1 = injector.get<MyService1>(MyServcice1);

Note: ServerLoader make this automatically when you use ServerLoader.mount() method (or settings attributes) and load services and controllers during the starting server.

Members

scopes: {
     [key: string]: ProviderScope;
 };

scopeOf(providerType: ProviderType): ProviderScope

settings: IDISettings

get<;T>;(target: Type<;T>; | symbol | any): T | undefined
Param Type Description
target <a href="/api/core/interfaces/Type.html"><span class="token">Type</span></a><T> | symbol | any The class or symbol registered in InjectorService.

Get a service or factory already constructed from his symbol or class.

Example

import {InjectorService} from "@tsed/common";
import MyService from "./services";

class OtherService {
     constructor(injectorService: InjectorService) {
         const myService = injectorService.get<MyService>(MyService);
     }
}

has(key: RegistryKey): boolean

The has() method returns a boolean indicating whether an element with the specified key exists or not.


getProvider(key: RegistryKey): Provider<;any>; | undefined
Param Type Description
key <a href="/api/core/class/RegistryKey.html"><span class="token">RegistryKey</span></a> Required. The key of the element to return from the Map object.

The getProvider() method returns a specified element from a Map object.


forkProvider(key: RegistryKey, instance?: any): Provider<;any>;

getProviders(type?: ProviderType | string): Provider<;any>;[]

invoke<;T>;(target: any, locals?: Map<;string | Function, any>;, designParamTypes?: any[], requiredScope?: boolean): T
Param Type Description
target any The injectable class to invoke. Class parameters are injected according constructor signature. locals

Invoke the class and inject all services that required by the class constructor.

Example

import {InjectorService} from "@tsed/common";
import MyService from "./services";

class OtherService {
    constructor(injectorService: InjectorService) {
         const myService = injectorService.invoke<MyService>(MyService);
     }
 }

invokeMethod(handler: any, options: IInjectableMethod<;any>;): any
Param Type Description
handler any The injectable method to invoke. Method parameters are injected according method signature. options

Invoke a class method and inject service.

IInjectableMethod options

  • target: Optional. The class instance.
  • methodName: string Optional. The method name.
  • designParamTypes: any[] Optional. List of injectable types.
  • locals: Map<Function, any> Optional. If preset then any argument Class are read from this object first, before the InjectorService is consulted.

Example

import {InjectorService} from "@tsed/common";

class MyService {
     constructor(injectorService: InjectorService) {
         injectorService.invokeMethod(this.method, {
             this,
             methodName: 'method'
         });
     }

  method(otherService: OtherService) {}
}

load(): Promise<;any>;

Initialize injectorService and load all services/factories.


emit(eventName: string, ...args: any[]): Promise<;void>;
Param Type Description
eventName string The event name to emit at all services. args

Emit an event to all service. See service lifecycle hooks.