Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
@Injectable()
class MyCustomService {
constructor(
@Inject('settings') settings: ISettings,
@Inject(UserService) userService: UserService,
@inject(GroupService) groupService: GroupService
) {
...
}
}
Да нет, должно быть всё же как-то так:
export class MyCustomService extends BaseService {
}А не горы бойлерплейта ради не понятно чего.
Ну а я вам порекомендую эту: https://habhub.hyoo.ru/#!author=nin-jin/repo=HabHub/article=40
Вы правы, примерно так это и работает (https://github.com/flancer64/habr_di_demo):
export default class Demo_UserService {
name = 'userService';
}export default class Demo_GroupService {
name = 'groupService';
}export default class Demo_MyCustomService {
/** @type {Demo_GroupService} */
group;
settings;
/** @type {Demo_UserService} */
user;
constructor({settings, Demo_UserService$, Demo_GroupService$}) {
this.settings = settings;
this.group = Demo_GroupService$;
this.user = Demo_UserService$;
}
}Это main.mjs:
import Container from '@teqfw/di';
import {dirname} from 'path';
// compose current path
const url = new URL(import.meta.url);
const current = dirname(url.pathname);
// setup DI container
const container = new Container();
container.addSourceMapping('Demo', current, true);
container.set('settings', {db: {name: 'work'}});
// get instance of custom service from DI container
/** @type {Demo_MyCustomService} */
const custom = await container.get('Demo_MyCustomService$');
// use instance
const dbName = custom.settings.db.name;
const user = custom.user.name;
const group = custom.group.name;
console.log(`${dbName} / ${user} / ${group}`);Только на чистом ES2015+, а не на TypeScript.
@teqfw/di