Как внедрить зависимость от водопада в бот-каркас - PullRequest
0 голосов
/ 04 апреля 2019

Мне нужно написать в CosmosDB из waterfallStep в botframework, как внедрить зависимость, waterfallStep является статическим делегатом.

Спасибо

1 Ответ

1 голос
/ 05 апреля 2019

Вы можете внедрить DbContext с помощью инжектора конструктора.

  1. Регистрация DbContext

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(option =>
            option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        //your rest code
    }
    
  2. Ввод DbContext

    public class MultiTurnPromptsBot : IBot
    {
        private readonly ApplicationDbContext _context;
        private const string WelcomeText = "Welcome to MultiTurnPromptBot. This bot will introduce multiple turns using prompts.  Type anything to get started.";
    
        private readonly MultiTurnPromptsBotAccessors _accessors;
    
        private DialogSet _dialogs;
    
        public MultiTurnPromptsBot(
            MultiTurnPromptsBotAccessors accessors
            , ApplicationDbContext context)
        {
            _accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));
            _context = context;
            // The DialogSet needs a DialogState accessor, it will call it when it has a turn context.
            _dialogs = new DialogSet(accessors.ConversationDialogState);
    
            // This array defines how the Waterfall will execute.
            var waterfallSteps = new WaterfallStep[]
            {
                NameStepAsync,
                NameConfirmStepAsync,
            };
    
            // Add named dialogs to the DialogSet. These names are saved in the dialog state.
            _dialogs.Add(new WaterfallDialog("details", waterfallSteps));
            _dialogs.Add(new TextPrompt("name"));
        }        
    
    
        private async Task<DialogTurnResult> NameConfirmStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // Get the current profile object from user state.
            var userProfile = await _accessors.UserProfile.GetAsync(stepContext.Context, () => new UserProfile(), cancellationToken);
    
            // Update the profile.
            userProfile.Name = (string)stepContext.Result;
            _context.Add(new User { Name = userProfile.Name });
            _context.SaveChanges();
            // We can send messages to the user at any point in the WaterfallStep.
            await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Thanks {stepContext.Result}."), cancellationToken);
    
            // WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
            return await stepContext.PromptAsync("confirm", new PromptOptions { Prompt = MessageFactory.Text("Would you like to give your age?") }, cancellationToken);
        }        
    }
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...