DependecyModule.js
Home
/
Src /
ReactDependecyInjection /
ClientApp /
src /
Model /
DI /
DependecyModule.js
import { DI } from 'di-xxl';
import { ErrorEntity } from '../Entities/Error/ErrorEntity';
import { ApiQueryExecuter } from '../DAL/WebApi/ApiQueryExecuter'
import { IWeatherApiClient } from '../DAL.Contract/WebApi/IWeatherApiClient'
import { WeatherApiClient } from '../DAL/WebApi/WeatherApiClient'
import { StubWeatherApiClient } from '../DAL/WebApi/StubWeatherApiClient'
/**
* Модуль, региструющий зависимости
* для DI использует библиотеку di-xxl
* */
export class DependecyModule {
static ClassName = 'DependecyModule';
constructor() {
this.Di = null;
}
/**
Регистрируем зависимости
*/
Registry() {
if (this.Di !== null)
{
throw new ErrorEntity(ErrorEntity.ArgumentException, 'Dependecy already registered')
.SetClassName(DependecyModule.ClassName)
.SetMethodName('Registry');
}
this.Di = new DI();
this.BindingNames = [];
this.RegistryDAL();
}
RegistryDAL() {
this._Registry(
{
name: ApiQueryExecuter.InterfaceName,
ref: ApiQueryExecuter
}
);
this._Registry(
{
name: IWeatherApiClient.InterfaceName,
ref: WeatherApiClient
}
);
//this._Registry(
// {
// name: IWeatherApiClient.InterfaceName,
// ref: StubWeatherApiClient,
// }
//);
}
_Registry(bindingInfo) {
if (bindingInfo.name === null
|| bindingInfo.name === undefined
) {
throw new ErrorEntity(ErrorEntity.ArgumentException, 'bindingInfo.name is empty')
.SetClassName(DependecyModule.ClassName)
.SetMethodName('_Registry');
}
//Проверка, что данная привязка не ипользовалась выше
if (this.BindingNames.indexOf(bindingInfo.name) !== -1) {
throw new ErrorEntity(ErrorEntity.ArgumentException, 'bindingInfo.name=' + bindingInfo.name + ' already use')
.SetClassName(DependecyModule.ClassName)
.SetMethodName('_Registry');
}
this.Di.set(bindingInfo);
this.BindingNames.push(bindingInfo.name);
}
/**
* Получить объекты по указанной привязке
*
* @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 });
}
}