コード例 #1
0
            /// <summary>
            /// Get the property value. The semantics are intended to be lazy, note the use of LoadAsync at the start.
            /// </summary>
            /// <param name="turnContext">The context object for this turn.</param>
            /// <param name="defaultValueFactory">Defines the default value. Invoked when no value been set for the requested state property.  If defaultValueFactory is defined as null, the MissingMemberException will be thrown if the underlying property is not set.</param>
            /// <param name="cancellationToken">The cancellation token.</param>
            /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
            public async Task <T> GetAsync(ITurnContext turnContext, Func <T> defaultValueFactory, CancellationToken cancellationToken)
            {
                T result = default(T);

                await _botState.LoadAsync(turnContext, false, cancellationToken).ConfigureAwait(false);

                try
                {
                    // if T is a value type, lookup up will throw key not found if not found, but as perf
                    // optimization it will return null if not found for types which are not value types (string and object).
                    result = await _botState.GetPropertyValueAsync <T>(turnContext, Name, cancellationToken).ConfigureAwait(false);

                    if (result == null && defaultValueFactory != null)
                    {
                        // use default Value Factory and save default value for any further calls
                        result = defaultValueFactory();
                        await SetAsync(turnContext, result, cancellationToken).ConfigureAwait(false);
                    }
                }
                catch (KeyNotFoundException)
                {
                    if (defaultValueFactory != null)
                    {
                        // use default Value Factory and save default value for any further calls
                        result = defaultValueFactory();
                        await SetAsync(turnContext, result, cancellationToken).ConfigureAwait(false);
                    }
                }

                return(result);
            }
コード例 #2
0
            /// <summary>
            /// Get the property value.
            /// </summary>
            /// <param name="turnContext">turn context</param>
            /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
            public async Task <T> GetAsync(ITurnContext turnContext)
            {
                try
                {
                    return(await _botState.GetPropertyValueAsync <T>(turnContext, Name).ConfigureAwait(false));
                }
                catch (KeyNotFoundException)
                {
                    // ask for default value from factory
                    var result = _defaultValueFactory();

                    // save default value for any further calls
                    await SetAsync(turnContext, result).ConfigureAwait(false);

                    return(result);
                }
            }
コード例 #3
0
            /// <summary>
            /// Get the property value.
            /// </summary>
            /// <param name="turnContext">The context object for this turn.</param>
            /// <param name="defaultValueFactory">Defines the default value. Invoked when no value been set for the requested state property.  If defaultValueFactory is defined as null, the MissingMemberException will be thrown if the underlying property is not set.</param>
            /// <param name="cancellationToken">The cancellation token.</param>
            /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
            public async Task <T> GetAsync(ITurnContext turnContext, Func <T> defaultValueFactory, CancellationToken cancellationToken)
            {
                await _botState.LoadAsync(turnContext, false, cancellationToken).ConfigureAwait(false);

                try
                {
                    return(await _botState.GetPropertyValueAsync <T>(turnContext, Name, cancellationToken).ConfigureAwait(false));
                }
                catch (KeyNotFoundException)
                {
                    // ask for default value from factory
                    if (defaultValueFactory == null)
                    {
                        throw new MissingMemberException("Property not set and no default provided.");
                    }

                    var result = defaultValueFactory();

                    // save default value for any further calls
                    await SetAsync(turnContext, result, cancellationToken).ConfigureAwait(false);

                    return(result);
                }
            }