All files / src/libs/DependencyInjection DIContainer.ts

83.33% Statements 15/18
77.77% Branches 7/9
100% Functions 4/4
81.25% Lines 13/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 9718x                                           18x   18x                           337x 18x     337x                                     306x                           192x   192x 1x 191x       191x               191x          
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { IDIContainer, IDIContainer_ } from './interfaces/IDIContainer';
 
/**
 * Dependency Entry Interface
 */
interface IDependency {
    /**
     * The dependency itself
     */
    dependency: unknown;
    /**
     * If true, the dependency is deprecated => a warning
     * is logged when the dependency is resolved
     */
    deprecated?: boolean;
}
 
/**
 * Dependency Injection Container
 */
@ImplementsStatic<IDIContainer_>()
export class DIContainer implements IDIContainer {
    private static _instance: DIContainer;
    private readonly _dependencies = new Map<string, IDependency>();
 
    /**
     * Private constructor to prevent direct instantiation.
     */
    private constructor() {}
 
    //#region IDIContainer_
 
    /**
     * Retrieves the singleton instance of DependencyRegistry.
     * @returns The singleton instance.
     */
    public static getInstance(): IDIContainer {
        if (this._instance == null) {
            this._instance = new DIContainer();
        }
 
        return this._instance;
    }
 
    //#endregion
 
    //#region IDIContainer
 
    /**
     * Register a dependency
     * @param identifier The identifier of the dependency
     * @param dependency The dependency to register
     * @param deprecated If true, the dependency is deprecated => a warning
     * is logged when the dependency is resolved
     */
    public register<T>(
        identifier: string,
        dependency: T,
        deprecated = false,
    ): void {
        this._dependencies.set(identifier, {
            dependency: dependency,
            deprecated: deprecated,
        });
    }
 
    /**
     * Resolve a dependency
     * @param identifier The identifier of the dependency
     * @param necessary If true, throws an error if the dependency is not found
     * @returns The resolved dependency or undefined if the dependency is not found (if necessary is false)
     * @throws Error if the dependency is not found (if necessary is true)
     */
    public resolve<T>(identifier: string, necessary = true): T | undefined {
        const dependency = this._dependencies.get(identifier);
 
        if (necessary && !dependency) {
            throw new Error(`Dependency ${identifier} not found`);
        } else Iif (!dependency) {
            return undefined;
        }
 
        Iif (dependency.deprecated) {
            // eslint-disable-next-line no-console
            console.warn(`Dependency ${identifier} is deprecated`);
 
            // Remove the deprecation warning; it should only be logged once.
            dependency.deprecated = false;
        }
 
        return dependency.dependency as T;
    }
 
    //#endregion
}
 
Zur TypeDoc-Dokumentation