Изменения документа Dependency Injection
Редактировал(а) Alexandr Fokin 2021/07/25 01:00
отредактировано Alexandr Fokin
на 2020/07/09 09:58
на 2020/07/09 09:58
отредактировано Alexandr Fokin
на 2021/07/24 21:46
на 2021/07/24 21:46
Изменить комментарий:
К данной версии нет комментариев
Комментарий
-
Свойства страницы (3 изменено, 0 добавлено, 0 удалено)
-
Объекты (0 изменено, 1 добавлено, 0 удалено)
Подробности
- Свойства страницы
-
- Название
-
... ... @@ -1,1 +1,1 @@ 1 - javascript-dependency-injection1 +JavaScript DependencyInjection - Теги
-
... ... @@ -1,0 +1,1 @@ 1 +DependencyInjection - Содержимое
-
... ... @@ -1,6 +1,20 @@ 1 1 2 -Попробовать, посмотреть как работает. 3 3 3 +InversifyJS: A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript. 4 +(required TypeScript) 5 +https://inversify.io/ 6 + 7 + 8 +Бибилиотеки для DI в JS 9 + 10 +javascript-dependency-injection-deprecated (Старая версия) 4 4 https://github.com/scaljeri/javascript-dependency-injection-deprecated 5 5 https://www.npmjs.com/package/javascript-dependency-injection 6 6 14 +di-xxl (Новая версия) 15 +https://github.com/scaljeri/di-xxl 16 +https://www.npmjs.com/package/di-xxl 17 + 18 +https://habr.com/ru/post/232851/ 19 +https://github.com/gobwas/dm.js 20 +
- XWiki.XWikiComments[0]
-
- Автор
-
... ... @@ -1,0 +1,1 @@ 1 +XWiki.cccc1808 - Комментарий
-
... ... @@ -1,0 +1,179 @@ 1 +{{code language="JavaScript"}} 2 +import { DI } from 'di-xxl'; 3 + 4 +export class ConstructorParameterInfo 5 +{ 6 + 7 +/** 8 +* @param {string} parameterName Имя параметра 9 +* @param {any} parameterValue Значенеи параметра 10 +*/ 11 + constructor( 12 + parameterName, 13 + parameterValue 14 + ) 15 + { 16 + this.ParameterName = parameterName; 17 + this.ParameterValue = parameterValue; 18 + } 19 +} 20 + 21 +export class IBase { 22 + 23 +/** 24 + * Базовый класс/интерфейс для всех классовых 25 + * Поддерживает: 26 + * имя класса 27 + * проверка, что параметры конструктора не пустые 28 + * 29 + * 30 +* @param {string} className Имя класса реализации 31 +* @param {ConstructorParameterInfo[]} constructorParameters Список параметров конструктора для проверки 32 +*/ 33 + constructor( 34 + className, 35 + constructorParameters = null 36 + ) 37 + { 38 + this.ClassName = className; 39 + 40 + IBase.TestClassName(className); 41 + IBase.TestParameters(className, constructorParameters); 42 + } 43 + 44 + 45 + static TestClassName(className) { 46 + if (className === "") { 47 + throw 'classname is empty'; 48 + } 49 + } 50 + static TestParameters(className, constructorParameters) { 51 + if (constructorParameters !== null && 52 + constructorParameters !== undefined 53 + ) { 54 + constructorParameters.forEach(function (elem, i, arr) { 55 + if (elem.ParameterValue === null 56 + || elem.ParameterValue === undefined) { 57 + throw className + '.' + elem.ParameterName + ' is epmty'; 58 + } 59 + }); 60 + } 61 + } 62 + 63 +} 64 + 65 + 66 +export class ITest extends IBase { 67 + static InterfaceName = 'ITest' 68 + 69 + constructor(className, constructorParameters) { 70 + super(className, constructorParameters); 71 + } 72 +} 73 +export class ITest2 extends ITest { 74 + static InterfaceName = 'ITest2' 75 + 76 + constructor(className, constructorParameters) { 77 + super(className, constructorParameters); 78 + } 79 +} 80 + 81 + 82 +class Test1 extends ITest { 83 + 84 + constructor( 85 + intNumber = -1 86 + ) 87 + { 88 + super('Test1'); 89 + 90 + this.a = intNumber; 91 + } 92 + 93 + hello() { 94 + return "Hello " + this.a; 95 + } 96 + 97 +} 98 + 99 +class Test2 extends ITest { 100 + 101 + //ITestProperty; 102 + 103 + constructor( 104 + ITest 105 + ) { 106 + super( 107 + 'Test2', 108 + [new ConstructorParameterInfo('ITest', ITest)]) 109 + ; 110 + 111 + this.ITest = ITest; 112 + 113 + //debugger; 114 + } 115 + 116 + hello() { 117 + return this.ITest.hello(); 118 + } 119 +} 120 + 121 + 122 +export class DependecyModule { 123 + 124 + 125 + constructor() { 126 + this.Di = null; 127 + } 128 + 129 + 130 +/** 131 + Регистрируем зависимости 132 +*/ 133 + Registry() { 134 + 135 + if (this.Di != null) 136 + { 137 + throw 'DependecyModule dependecy already registered'; 138 + } 139 + 140 + this.Di = new DI(); 141 + 142 + 143 + this.Di.set( 144 + { 145 + name: ITest.InterfaceName, 146 + ref: Test1, 147 + }); 148 + 149 + this.Di.set( 150 + { 151 + name: ITest2.InterfaceName, 152 + ref: Test2, 153 + params: [this.getWithParam(ITest.InterfaceName, 22)] 154 + 155 + //inject: [{ property: 'ITestProperty', name: ITest.InterfaceName }] 156 + }); 157 + } 158 + 159 +/** 160 +* Получить объекты по указанной привязке 161 +* 162 +* @param {string} type Имя привязки. 163 +*/ 164 + get(type) { 165 + return this.Di.get(type); 166 + } 167 + 168 +/** 169 + * Получить объекты по указанной привязке с параметрами конструктора 170 + * 171 + * @param {string} type Имя привязки. 172 + * @param {any} param Параметры конструктора. 173 +*/ 174 + getWithParam(type, param) { 175 + return this.Di.get(type, { params: param }); 176 + } 177 + 178 +} 179 +{{/code}} - Дата
-
... ... @@ -1,0 +1,1 @@ 1 +2020-07-10 19:48:27.900