从BotFramework V4中的类获取和设置数据

时间:2019-07-16 12:26:16

标签: c# botframework

您好,我正在使用Bot Framework V4和C#。每当我想从某个类中获取或设置某些东西时,我都会使用这种依赖注入方法。我有很多对话框类,并且为每个对话框都做些乏味。这是正确的做法吗?我什至不知道每次作为对话框参数输入用户状态是否好。有没有更简单的方法从类中获取或设置数据?

 public class MainDialog : CancelAndHelpDialog
{
    private readonly IStatePropertyAccessor<UserClass> _userProfileAccessor;
    protected readonly IConfiguration Configuration;
    protected readonly ILogger Logger;

    public MainDialog(UserState userState, IConfiguration configuration, ILogger<MainDialog> logger)
        : base(nameof(MainDialog))
    {
        _userProfileAccessor = userState.CreateProperty<UserClass>("UserProfile");
        Configuration = configuration;
        Logger = logger;

        AddDialog(new TextPrompt(nameof(TextPrompt)));
        AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
        {
            AskNameStepAsync,
        }));
        InitialDialogId = nameof(WaterfallDialog);
    }

    private async Task<DialogTurnResult> AskNameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        return await stepContext.PromptAsync(
            nameof(TextPrompt),
            new PromptOptions
            {
                Prompt = MessageFactory.Text("Enter your name"),
            });
    }

   private async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
   {
        var userProfile = await _userProfileAccessor.GetAsync(stepContext.Context, () => new UserClass(), cancellationToken);

        userProfile.Name = (string)stepContext.Result;
        return await stepContext.EndDialogAsync();
      }

0 个答案:

没有答案