generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEfDbModelQuery.cs
308 lines (269 loc) · 18.1 KB
/
EfDbModelQuery.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
using CoreEx.Results;
using Microsoft.EntityFrameworkCore;
namespace CoreEx.EntityFrameworkCore
{
/// <summary>
/// Encapsulates an Entity Framework query enabling all select-like capabilities on a specified <typeparamref name="TModel"/>.
/// </summary>
/// <typeparam name="TModel">The entity framework model <see cref="Type"/>.</typeparam>
/// <remarks>Queried entities by default are <see cref="EntityFrameworkQueryableExtensions.AsNoTracking{TEntity}(IQueryable{TEntity})">not tracked</see>; this behavior can be overridden using <see cref="EfDbArgs.QueryNoTracking"/>.
/// <para>Reminder: leverage <see cref="EntityFrameworkQueryableExtensions.IgnoreAutoIncludes{TEntity}(IQueryable{TEntity})"/> and then explictly include to improve performance where applicable.</para>
/// <para>Automatic filtering is applied using the <see cref="EfDbArgs.WhereModelValid"/>.</para></remarks>
public class EfDbQuery<TModel> where TModel : class, new()
{
private readonly Func<IQueryable<TModel>, IQueryable<TModel>>? _query;
/// <summary>
/// Initializes a new instance of the <see cref="EfDbQuery{T, TModel}"/> class.
/// </summary>
/// <param name="efdb">The <see cref="IEfDb"/>.</param>
/// <param name="args">The <see cref="EfDbArgs"/>.</param>
/// <param name="query">A function to modify the underlying <see cref="IQueryable{TModel}"/>.</param>
internal EfDbQuery(IEfDb efdb, EfDbArgs args, Func<IQueryable<TModel>, IQueryable<TModel>>? query = null)
{
EfDb = efdb.ThrowIfNull(nameof(efdb));
Args = args;
_query = query;
Paging = null;
}
/// <summary>
/// Gets the <see cref="IEfDb"/>.
/// </summary>
public IEfDb EfDb { get; }
/// <summary>
/// Gets the <see cref="EfDbArgs"/>.
/// </summary>
public EfDbArgs Args { get; }
/// <summary>
/// Gets the <see cref="PagingResult"/>.
/// </summary>
public PagingResult? Paging { get; private set; }
/// <summary>
/// Adds <see cref="PagingArgs"/> to the query.
/// </summary>
/// <param name="paging">The <see cref="PagingArgs"/>.</param>
/// <returns>The <see cref="EfDbQuery{TModel}"/> to suport fluent-style method-chaining.</returns>
public EfDbQuery<TModel> WithPaging(PagingArgs? paging)
{
Paging = paging == null ? null : (paging is PagingResult pr ? pr : new PagingResult(paging));
return this;
}
/// <summary>
/// Adds <see cref="PagingArgs"/> to the query.
/// </summary>
/// <param name="skip">The specified number of elements in a sequence to bypass.</param>
/// <param name="take">The specified number of contiguous elements from the start of a sequence.</param>
/// <returns>The <see cref="EfDbQuery{TModel}"/> to suport fluent-style method-chaining.</returns>
public EfDbQuery<TModel> WithPaging(long skip, long? take = null) => WithPaging(PagingArgs.CreateSkipAndTake(skip, take));
/// <summary>
/// Manages the DbContext and underlying query construction and lifetime.
/// </summary>
private async Task<Result<TResult?>> ExecuteQueryAsync<TResult>(Func<IQueryable<TModel>, CancellationToken, Task<TResult?>> executeAsync, string memberName, CancellationToken cancellationToken) => await EfDb.Invoker.InvokeAsync(EfDb, EfDb, _query, Args, async (_, efdb, query, args, ct) =>
{
var dbSet = args.WhereModelValid(args.QueryNoTracking ? efdb.DbContext.Set<TModel>().AsNoTracking() : efdb.DbContext.Set<TModel>());
return Result<TResult?>.Ok(await executeAsync((query == null) ? dbSet : query(dbSet), ct).ConfigureAwait(false));
}, cancellationToken, memberName).ConfigureAwait(false);
/// <summary>
/// Executes the query and cleans.
/// </summary>
private async Task<Result<TModel?>> ExecuteQueryInternalAsync<TResult>(Func<IQueryable<TModel>, CancellationToken, Task<TResult?>> executeAsync, string memberName, CancellationToken cancellationToken)
{
var result = await ExecuteQueryAsync(executeAsync, memberName, cancellationToken).ConfigureAwait(false);
if (result.IsFailure)
return Result<TModel?>.Fail(result.Error);
return result.Value is TModel model ? model : default;
}
/// <summary>
/// Sets the paging from the <see cref="PagingArgs"/>.
/// </summary>
private static IQueryable<TModel> SetPaging(IQueryable<TModel> query, PagingArgs? paging)
{
if (paging == null)
return query;
var q = query;
if (paging.Skip > 0)
q = q.Skip((int)paging.Skip);
return q.Take((int)(paging == null ? PagingArgs.DefaultTake : paging.Take));
}
/// <summary>
/// Selects a single item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item.</returns>
public async Task<TModel> SelectSingleAsync(CancellationToken cancellationToken = default)
=> (await ExecuteQueryInternalAsync(async (q, ct) => await q.SingleAsync(ct).ConfigureAwait(false), nameof(SelectSingleAsync), cancellationToken).ConfigureAwait(false))!;
/// <summary>
/// Selects a single item with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item.</returns>
public async Task<Result<TModel>> SelectSingleWithResultAsync(CancellationToken cancellationToken = default)
=> (await ExecuteQueryInternalAsync(async (q, ct) => await q.SingleAsync(ct).ConfigureAwait(false), nameof(SelectSingleWithResultAsync), cancellationToken).ConfigureAwait(false))!;
/// <summary>
/// Selects a single item or default.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public async Task<TModel?> SelectSingleOrDefaultAsync(CancellationToken cancellationToken = default)
=> await ExecuteQueryInternalAsync((q, ct) => q.SingleOrDefaultAsync(ct), nameof(SelectSingleOrDefaultAsync), cancellationToken).ConfigureAwait(false);
/// <summary>
/// Selects a single item or default with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public Task<Result<TModel?>> SelectSingleOrDefaultWithResultAsync(CancellationToken cancellationToken = default)
=> ExecuteQueryInternalAsync((q, ct) => q.SingleOrDefaultAsync(ct), nameof(SelectSingleOrDefaultWithResultAsync), cancellationToken);
/// <summary>
/// Selects first item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The first item.</returns>
public async Task<TModel> SelectFirstAsync(CancellationToken cancellationToken = default)
=> (await ExecuteQueryInternalAsync(async (q, ct) => await q.FirstAsync(ct).ConfigureAwait(false), nameof(SelectFirstAsync), cancellationToken).ConfigureAwait(false))!;
/// <summary>
/// Selects first item with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The first item.</returns>
public async Task<Result<TModel>> SelectFirstWithResultAsync(CancellationToken cancellationToken = default)
=> (await ExecuteQueryInternalAsync(async (q, ct) => await q.FirstAsync(ct).ConfigureAwait(false), nameof(SelectFirstWithResultAsync), cancellationToken).ConfigureAwait(false))!;
/// <summary>
/// Selects first item or default.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public async Task<TModel?> SelectFirstOrDefaultAsync(CancellationToken cancellationToken = default)
=> await ExecuteQueryInternalAsync((q, ct) => q.FirstOrDefaultAsync(ct), nameof(SelectFirstOrDefaultAsync), cancellationToken).ConfigureAwait(false);
/// <summary>
/// Selects first item or default with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
public Task<Result<TModel?>> SelectFirstOrDefaultWithResultAsync(CancellationToken cancellationToken = default)
=> ExecuteQueryInternalAsync((q, ct) => q.FirstOrDefaultAsync(ct), nameof(SelectFirstOrDefaultWithResultAsync), cancellationToken);
/// <summary>
/// Executes the query command creating a <typeparamref name="TCollResult"/>.
/// </summary>
/// <typeparam name="TCollResult">The <see cref="ICollectionResult{TColl, TItem}"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TColl">The <see cref="ICollection{T}"/> <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The resulting <typeparamref name="TCollResult"/>.</returns>
public async Task<TCollResult> SelectResultAsync<TCollResult, TColl>(CancellationToken cancellationToken = default) where TCollResult : ICollectionResult<TColl, TModel>, new() where TColl : ICollection<TModel>, new() => new TCollResult
{
Paging = Paging,
Items = (await SelectQueryWithResultInternalAsync<TColl>(nameof(SelectResultAsync), cancellationToken).ConfigureAwait(false)).Value
};
/// <summary>
/// Executes the query command creating a <typeparamref name="TCollResult"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="TCollResult">The <see cref="ICollectionResult{TColl, TItem}"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TColl">The <see cref="ICollection{T}"/> <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The resulting <typeparamref name="TCollResult"/>.</returns>
public async Task<Result<TCollResult>> SelectResultWithResultAsync<TCollResult, TColl>(CancellationToken cancellationToken = default) where TCollResult : ICollectionResult<TColl, TModel>, new() where TColl : ICollection<TModel>, new() => new TCollResult
{
Paging = Paging,
Items = (await SelectQueryWithResultInternalAsync<TColl>(nameof(SelectResultWithResultAsync), cancellationToken).ConfigureAwait(false)).Value
};
/// <summary>
/// Executes the query command creating a resultant collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <returns>A resultant collection.</returns>
public async Task<TColl> SelectQueryAsync<TColl>(CancellationToken cancellationToken = default) where TColl : ICollection<TModel>, new()
=> (await SelectQueryWithResultInternalAsync<TColl>(nameof(SelectQueryAsync), cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Executes the query command creating a resultant collection with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <returns>A resultant collection.</returns>
public Task<Result<TColl>> SelectQueryWithResultAsync<TColl>(CancellationToken cancellationToken = default) where TColl : ICollection<TModel>, new()
=> SelectQueryWithResultInternalAsync<TColl>(nameof(SelectQueryWithResultAsync), cancellationToken);
/// <summary>
/// Executes the query command creating a resultant collection with a <see cref="Result{T}"/> internal.
/// </summary>
private async Task<Result<TColl>> SelectQueryWithResultInternalAsync<TColl>(string memberName, CancellationToken cancellationToken) where TColl : ICollection<TModel>, new()
{
var coll = new TColl();
return await SelectQueryWithResultInternalAsync(coll, memberName, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Executes a query adding to the passed collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <param name="collection">The collection to add items to.</param>
/// <returns>The <paramref name="collection"/>.</returns>
public async Task<TColl> SelectQueryAsync<TColl>(TColl collection, CancellationToken cancellationToken = default) where TColl : ICollection<TModel>
=> (await SelectQueryWithResultInternalAsync(collection, nameof(SelectQueryAsync), cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Executes a query adding to the passed collection with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <param name="collection">The collection to add items to.</param>
/// <returns>The <paramref name="collection"/>.</returns>
public Task<Result<TColl>> SelectQueryWithResultAsync<TColl>(TColl collection, CancellationToken cancellationToken = default) where TColl : ICollection<TModel>
=> SelectQueryWithResultInternalAsync(collection, nameof(SelectQueryWithResultAsync), cancellationToken);
/// <summary>
/// Executes a query adding to the passed collection with a <see cref="Result{T}"/> internal.
/// </summary>
private async Task<Result<TColl>> SelectQueryWithResultInternalAsync<TColl>(TColl collection, string memberName, CancellationToken cancellationToken) where TColl : ICollection<TModel>
{
collection.ThrowIfNull(nameof(collection));
var result = await SelectQueryWithResultAsync(item =>
{
collection.Add(item);
return true;
}, memberName, cancellationToken).ConfigureAwait(false);
return result.ThenAs(() => collection);
}
/// <summary>
/// Executes a query with per <paramref name="item"/> processing
/// </summary>
/// <param name="item">The item function.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <remarks>The <paramref name="item"/> returns a <see cref="bool"/> which indicates whether to continue enumerating. A <c>true</c> indicates
/// to continue, whilst a <c>false</c> indicates a stop. Where <see cref="Result{T}.IsFailure"/> then the error will be returned as-is.</remarks>
public async Task SelectQueryAsync(Func<TModel, bool> item, CancellationToken cancellationToken = default)
=> (await SelectQueryWithResultAsync(model => item.ThrowIfNull(nameof(item))(model), nameof(SelectQueryAsync), cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Executes a query with per <paramref name="item"/> processing with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="item">The item function.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The <see cref="Result"/>.</returns>
/// <remarks>The <paramref name="item"/> returns a <see cref="Result{T}"/> where the corresponding <see cref="Result{T}.Value"/> <see cref="bool"/> indicates whether to continue enumerating. A <c>true</c> indicates
/// to continue, whilst a <c>false</c> indicates a stop. Where <see cref="Result{T}.IsFailure"/> then the error will be returned as-is.</remarks>
public Task<Result> SelectQueryWithResultAsync(Func<TModel, Result<bool>> item, CancellationToken cancellationToken = default)
=> SelectQueryWithResultAsync(item.ThrowIfNull(nameof(item)), nameof(SelectQueryWithResultAsync), cancellationToken);
/// <summary>
/// Executes a query with per <paramref name="item"/> processing.
/// </summary>
/// <param name="item">The item function.</param>
/// <param name="memberName">The member name.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The <see cref="Result"/>.</returns>
/// <remarks>The <paramref name="item"/> returns a <see cref="Result{T}"/> where the corresponding <see cref="Result{T}.Value"/> <see cref="bool"/> indicates whether to continue enumerating. A <c>true</c> indicates
/// to continue, whilst a <c>false</c> indicates a stop. Where <see cref="Result{T}.IsFailure"/> then the error will be returned as-is.</remarks>
internal async Task<Result> SelectQueryWithResultAsync(Func<TModel, Result<bool>> item, string memberName, CancellationToken cancellationToken)
{
var result = await ExecuteQueryAsync<bool>(async (query, ct) =>
{
var q = SetPaging(query, Paging);
Result<bool> r = Result<bool>.None;
await foreach (var model in q.AsAsyncEnumerable().WithCancellation(ct))
{
r = item(model);
if (r.IsFailure || !r.Value)
break;
}
if (r.IsSuccess && Paging != null && Paging.IsGetCount)
Paging.TotalCount = query.LongCount();
return r;
}, memberName, cancellationToken);
return result.AsResult();
}
}
}