forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefAccessor.cs
61 lines (52 loc) · 2.15 KB
/
RefAccessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// This is an accessor for any object. By definition objects (as opposed to values)
/// are returned by reference in the GetAsync call on the accessor. As such the SetAsync
/// call is never used. The actual act of saving any state to an external store therefore
/// cannot be encapsulated in the Accessor implementation itself. And so to facilitate this
/// the state itself is available as a public property on this class. The reason its here is
/// because the caller of the constructor could pass in null for the state, in which case
/// the factory provided on the GetAsync call will be used.
/// </summary>
/// <typeparam name="T">The type of the object this Accessor Gets.</typeparam>
public class RefAccessor<T> : IStatePropertyAccessor<T>
where T : class
{
public RefAccessor(T value)
{
Value = value;
}
public T Value { get; private set; }
public string Name => nameof(T);
public Task<T> GetAsync(ITurnContext turnContext, Func<T> defaultValueFactory = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (Value == null)
{
if (defaultValueFactory == null)
{
throw new KeyNotFoundException();
}
Value = defaultValueFactory();
}
return Task.FromResult(Value);
}
#region Not Implemented
public Task DeleteAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
public Task SetAsync(ITurnContext turnContext, T value, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
#endregion
}
}