• Alexandr Fokin
    Alexandr Fokin, 2020/07/10 23:48
    import { DI } from 'di-xxl';

    export class ConstructorParameterInfo
    {

    /**
    * @param {string} parameterName Имя параметра
    * @param {any} parameterValue Значенеи параметра
    */

        constructor(
            parameterName,
            parameterValue
        )
        {
           this.ParameterName = parameterName;
           this.ParameterValue = parameterValue;
        }
    }

    export class IBase {

    /**
     * Базовый класс/интерфейс для всех классовых
     * Поддерживает:
     * имя класса
     * проверка, что параметры конструктора не пустые
     *
     *
    * @param {string} className Имя класса реализации
    * @param {ConstructorParameterInfo[]} constructorParameters Список параметров конструктора для проверки
    */

        constructor(
            className,
            constructorParameters = null
        )
        {
           this.ClassName = className;

            IBase.TestClassName(className);
            IBase.TestParameters(className, constructorParameters);
        }


       static TestClassName(className) {
           if (className === "") {
               throw 'classname is empty';
            }
        }
       static TestParameters(className, constructorParameters) {
           if (constructorParameters !== null &&
                constructorParameters !== undefined
            ) {
                constructorParameters.forEach(function (elem, i, arr) {
                   if (elem.ParameterValue === null
                       || elem.ParameterValue === undefined) {
                       throw className + '.' + elem.ParameterName + ' is epmty';
                    }
                });
            }
        }

    }


    export class ITest extends IBase {
       static InterfaceName = 'ITest'

        constructor(className, constructorParameters) {
           super(className, constructorParameters);
        }
    }
    export class ITest2 extends ITest {
       static InterfaceName = 'ITest2'

        constructor(className, constructorParameters) {
           super(className, constructorParameters);
        }
    }


    class Test1 extends ITest {

        constructor(
            intNumber = -1
        )
        {
           super('Test1');

           this.a = intNumber;
        }

        hello() {
           return "Hello " + this.a;
        }

    }

    class Test2 extends ITest {

       //ITestProperty;

        constructor(
            ITest
        ) {
           super(
               'Test2',
                [new ConstructorParameterInfo('ITest', ITest)])
                ;

           this.ITest = ITest;

           //debugger;
       }

        hello() {
           return this.ITest.hello();
        }
    }


    export class DependecyModule {


        constructor() {
           this.Di = null;
        }


    /**
        Регистрируем зависимости
    */

        Registry() {

           if (this.Di != null)
            {
               throw 'DependecyModule dependecy already registered';
            }

           this.Di = new DI();


           this.Di.set(            
                {
                    name: ITest.InterfaceName,
                    ref: Test1,
                });

           this.Di.set(            
                {
                    name: ITest2.InterfaceName,
                    ref: Test2,
                    params: [this.getWithParam(ITest.InterfaceName, 22)]

                   //inject: [{ property: 'ITestProperty', name: ITest.InterfaceName }]
               });
        }

    /**
    * Получить объекты по указанной привязке
    *
    * @param {string} type Имя привязки.
    */

        get(type) {
           return this.Di.get(type);
        }

    /**
     * Получить объекты по указанной привязке с параметрами конструктора
     *
     * @param {string} type Имя привязки.
     * @param {any} param Параметры конструктора.
    */

        getWithParam(type, param) {
           return this.Di.get(type, { params: param });
        }

    }