-
Notifications
You must be signed in to change notification settings - Fork 177
/
EntityFrameworkStorage.cs
214 lines (188 loc) · 8.83 KB
/
EntityFrameworkStorage.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Bot.Builder.Community.Storage.EntityFramework
{
/// <summary>
/// Implements an EntityFramework based storage provider for a bot.
/// </summary>
public class EntityFrameworkStorage : IStorage
{
private static readonly JsonSerializer _jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
private bool _checkedConnection;
private readonly EntityFrameworkStorageOptions _storageOptions;
/// <summary>
/// Initializes a new instance of the <see cref="EntityFrameworkStorage"/> class.
/// using the provided connection string.
/// </summary>
/// <param name="connectionString">Entity Framework database connection string.</param>
public EntityFrameworkStorage(string connectionString)
: this(new EntityFrameworkStorageOptions() { ConnectionString = connectionString })
{
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException(nameof(connectionString));
}
}
/// <summary>
/// Initializes a new instance of the <see cref="EntityFrameworkStorage"/> class.
/// </summary>
/// <param name="options">Entity Framework options <see cref="EntityFrameworkStorageOptions"/> class.</param>
public EntityFrameworkStorage(EntityFrameworkStorageOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (string.IsNullOrEmpty(options.ConnectionString))
{
throw new ArgumentNullException(nameof(options.ConnectionString));
}
_storageOptions = options;
}
/// <summary>
/// Get a BotDataContext will by default use the connection string provided during EntityFrameworkStorage construction.
/// </summary>
public virtual BotDataContext GetBotDataContext => new BotDataContext(_storageOptions.ConnectionString);
/// <summary>
/// Deletes storage items from storage.
/// </summary>
/// <param name="keys">keys of the <see cref="IStoreItem"/> objects to remove from the store.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the work queued to execute.</returns>
/// <seealso cref="ReadAsync(string[], CancellationToken)"/>
/// <seealso cref="WriteAsync(IDictionary{string, object}, CancellationToken)"/>
public async Task DeleteAsync(string[] keys, CancellationToken cancellationToken)
{
if (keys == null)
{
throw new ArgumentNullException(nameof(keys));
}
if (keys.Length == 0)
{
return;
}
// Ensure Initialization has been run
await EnsureConnection().ConfigureAwait(false);
using (var context = GetBotDataContext)
{
context.RemoveRange(context.BotDataEntity.Where(item => keys.Contains(item.RealId)));
await context.SaveChangesAsync();
}
}
/// <summary>
/// Reads storage items from storage.
/// </summary>
/// <param name="keys">keys of the <see cref="IStoreItem"/> objects to read from the store.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the work queued to execute.</returns>
/// <remarks>If the activities are successfully sent, the task result contains
/// the items read, indexed by key.</remarks>
/// <seealso cref="DeleteAsync(string[], CancellationToken)"/>
/// <seealso cref="WriteAsync(IDictionary{string, object}, CancellationToken)"/>
public async Task<IDictionary<string, object>> ReadAsync(string[] keys, CancellationToken cancellationToken)
{
if (keys == null)
{
throw new ArgumentNullException(nameof(keys));
}
if (keys.Length == 0)
{
// No keys passed in, no result to return.
return new Dictionary<string, object>();
}
// Ensure we have checked for possible connection issues
await EnsureConnection().ConfigureAwait(false);
var storeItems = new Dictionary<string, object>(keys.Length);
using (var database = GetBotDataContext)
{
var query = (from item in database.BotDataEntity
where keys.Any(k => k == item.RealId)
select new { item.RealId, item.Document });
foreach (var item in query)
{
var jObject = JObject.Parse(item.Document).ToObject(typeof(object), _jsonSerializer);
storeItems.Add(item.RealId, jObject);
}
return storeItems;
}
}
/// <summary>
/// Writes storage items to storage.
/// </summary>
/// <param name="changes">The items to write to storage, indexed by key.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects
/// or threads to receive notice of cancellation.</param>
/// <returns>A task that represents the work queued to execute.</returns>
/// <seealso cref="DeleteAsync(string[], CancellationToken)"/>
/// <seealso cref="ReadAsync(string[], CancellationToken)"/>
public async Task WriteAsync(IDictionary<string, object> changes, CancellationToken cancellationToken)
{
if (changes == null)
{
throw new ArgumentNullException(nameof(changes));
}
if (changes.Count == 0)
{
return;
}
// Ensure Initialization has been run
await EnsureConnection().ConfigureAwait(false);
using (var context = GetBotDataContext)
{
// Begin a transaction using the isolation level provided in Storage Options
var transaction = context.Database.BeginTransaction(_storageOptions.TransactionIsolationLevel);
var existingItems = context.BotDataEntity.Where(item => changes.Keys.Contains(item.RealId)).ToDictionary(d => (d as BotDataEntity).RealId);
foreach (var change in changes)
{
var json = JObject.FromObject(change.Value, _jsonSerializer);
var existingItem = existingItems.FirstOrDefault(i => i.Key == change.Key).Value;
if (existingItem != null)
{
existingItem.Document = json.ToString(Formatting.None);
existingItem.Timestamp = DateTimeOffset.UtcNow;
}
else
{
var newItem = new BotDataEntity
{
RealId = change.Key,
Document = json.ToString(Formatting.None),
Timestamp = DateTimeOffset.UtcNow,
};
await context.BotDataEntity.AddAsync(newItem);
}
}
context.SaveChanges();
transaction.Commit();
}
}
/// <summary>
/// Ensures a database connection is possible.
/// </summary>
/// <remarks>
/// Will throw ArgumentException if the database cannot be created to.
/// </remarks>
private async Task EnsureConnection()
{
// In the steady-state case, we'll already have verified the database is setup.
if (!_checkedConnection)
{
using (var context = GetBotDataContext)
{
if (!await context.Database.CanConnectAsync())
throw new ArgumentException("The sql database defined in the connection has not been created. See https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/tree/master/libraries/Bot.Builder.Community.Storage.EntityFramework");
}
_checkedConnection = true;
}
}
}
}