Отсутствует аннотация @Injectable при импорте значения json - PullRequest
0 голосов
/ 01 мая 2020

В настоящее время я пытаюсь внедрить файл json в созданный мной сервис. Этот класс внедряет json через тег @inject() в конструкторе. Ниже вы можете найти код, который я использую.

DiContainer.ts

import { Container, decorate, injectable } from "inversify";
import { IResponseBuilder, InjectionTypes, IUserIdGenerator, ILogger, IResponseRepository, IResponseService, IContextService, IFileWriter, IBoardSessionService, IIntent } from "./src/common/types";
import GoogleResponseBuilder from "./src/common/builders/googleResponseBuilder";
import UserIdGenerator from "./src/common/helpers/userIdGenerator";
import { DialogflowConversation } from "actions-on-google";
import WinstonLogger from "./src/common/winstonLogger";
import FileWriter from "./src/common/helpers/fileWriter";
import TextResponseRepository from "./src/repositories/textResponseRepository";
import SsmlResponseRepository from "./src/repositories/ssmlResponseRepository";
import ResponseSerivce from "./src/services/responseService";
import ContextService from "./src/services/contextService";
import { BoardService } from "./src/services/boardService";
import { BoardSessionService } from "./src/services/boardSessionService";
import WelcomeIntent from "./src/intents/new/welcomeIntent";
import uuid from "uuid/v4";

const sessionJson = require("./src/data/boardSessions.json");



const DIContainer = new Container();

DIContainer.bind<IResponseBuilder<DialogflowConversation>>(InjectionTypes.GoogleResponseBuilder).to(GoogleResponseBuilder);
DIContainer.bind<ILogger>(InjectionTypes.WinstonLogger).to(WinstonLogger);
DIContainer.bind<IFileWriter>(InjectionTypes.FileWriter).to(FileWriter);
DIContainer.bind<IUserIdGenerator>(InjectionTypes.UserIdGenerator).to(UserIdGenerator);

DIContainer.bind<IIntent>(InjectionTypes.WelcomeIntent).to(WelcomeIntent);

DIContainer.bind<IResponseRepository>(InjectionTypes.TextResponseRepository).to(TextResponseRepository);
DIContainer.bind<IResponseRepository>(InjectionTypes.SsmlResponseRepository).to(SsmlResponseRepository);
DIContainer.bind<IResponseService>(InjectionTypes.ResponseService).to(ResponseSerivce);
DIContainer.bind<IBoardSessionService>(InjectionTypes.BoardSessionService).to(BoardSessionService);
DIContainer.bind<IContextService>(InjectionTypes.ContextService).to(ContextService);

DIContainer.bind(InjectionTypes.SessionJSON).to(sessionJson);
DIContainer.bind(InjectionTypes.UUIDv4).toFunction(uuid);


export default DIContainer;

1 Ответ

1 голос
/ 01 мая 2020

Каждая зависимость, которую вы пытаетесь внедрить через @inject, должна быть помечена как @injectable и зарегистрирована через bind. Я действительно не знаю, как работает функция decorate (поэтому я не знаю, можете ли вы использовать ее для пометки функции, а не класса как инъекционной).

Кстати, я думаю, что вы можете добиться того, чего хотите, просто зарегистрировав свою зависимость как значение c dynamici и вернув желаемую функцию, как указано здесь . В вашем случае, что-то вроде этого:

DIContainer.bind(InjectionTypes.UUIDv4).toDynamicValue((context: interfaces.Context) => { return uuid });

В качестве альтернативы, вы можете просто импортировать функцию в своем сервисе без ее внедрения или обернуть функцию в другой сервис, который вы можете пометить как инъекционный (скажем, услуга провайдера uuid).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...