Изменения документа Dependency Injection

Редактировал(а) Alexandr Fokin 2021/07/25 01:00

<
От версии < 2.1 >
отредактировано Alexandr Fokin
на 2020/07/10 18:48
К версии < 2.2 >
отредактировано Alexandr Fokin
на 2020/07/10 19:48
>
Изменить комментарий: Добавлен комментарий

Комментарий

Подробности

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