generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEfDbQuery.cs
250 lines (219 loc) · 13.6 KB
/
EfDbQuery.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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
using CoreEx.Mapping;
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"/> automatically mapping to the resultant <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <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></remarks>
public class EfDbQuery<T, TModel> where T : class, new() where TModel : class, new()
{
private readonly EfDbQuery<TModel> _query;
/// <summary>
/// Initializes a new instance of the <see cref="EfDbQuery{T, TModel}"/> struct.
/// </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) => _query = new EfDbQuery<TModel>(efdb, args, query);
/// <summary>
/// Gets the <see cref="IEfDb"/>.
/// </summary>
public IEfDb EfDb => _query.EfDb;
/// <summary>
/// Gets the <see cref="EfDbArgs"/>.
/// </summary>
public EfDbArgs Args => _query.Args;
/// <summary>
/// Gets the <see cref="IMapper"/>.
/// </summary>
public IMapper Mapper => EfDb.Mapper;
/// <summary>
/// Gets the <see cref="PagingResult"/>.
/// </summary>
public PagingResult? Paging => _query.Paging;
/// <summary>
/// Adds <see cref="PagingArgs"/> to the query.
/// </summary>
/// <param name="paging">The <see cref="PagingArgs"/>.</param>
/// <returns>The <see cref="EfDbQuery{T, TModel}"/> to suport fluent-style method-chaining.</returns>
public EfDbQuery<T, TModel> WithPaging(PagingArgs? paging)
{
_query.WithPaging(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{T, TModel}"/> to suport fluent-style method-chaining.</returns>
public EfDbQuery<T, TModel> WithPaging(long skip, long? take = null) => WithPaging(PagingArgs.CreateSkipAndTake(skip, take));
/// <summary>
/// Executes the query and maps.
/// </summary>
private async Task<Result<TResult>> ExecuteQueryAndMapAsync<TResult, TModelResult>(Func<CancellationToken, Task<Result<TModelResult>>> executeAsync, CancellationToken cancellationToken)
{
var result = await executeAsync(cancellationToken).ConfigureAwait(false);
if (result.IsFailure)
return Result<TResult>.Fail(result.Error);
var val = result.Value == null ? default! : Mapper.Map<TResult>(result.Value, Mapping.OperationTypes.Get);
return Args.CleanUpResult ? Cleaner.Clean(val) : val;
}
/// <summary>
/// Selects a single item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item.</returns>
public async Task<T> SelectSingleAsync(CancellationToken cancellationToken = default)
=> (await SelectSingleWithResultAsync(cancellationToken).ConfigureAwait(false)).Value;
/// <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<T>> SelectSingleWithResultAsync(CancellationToken cancellationToken = default)
{
var q = _query;
return await ExecuteQueryAndMapAsync<T, TModel>(q.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<T?> SelectSingleOrDefaultAsync(CancellationToken cancellationToken = default)
=> (await SelectSingleOrDefaultWithResultAsync(cancellationToken).ConfigureAwait(false)).Value;
/// <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 async Task<Result<T?>> SelectSingleOrDefaultWithResultAsync(CancellationToken cancellationToken = default)
{
var q = _query;
return await ExecuteQueryAndMapAsync<T?, TModel?>(q.SelectSingleOrDefaultWithResultAsync, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Selects first item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The first item.</returns>
public async Task<T> SelectFirstAsync(CancellationToken cancellationToken = default)
=> (await SelectFirstWithResultAsync(cancellationToken).ConfigureAwait(false)).Value;
/// <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<T>> SelectFirstWithResultAsync(CancellationToken cancellationToken = default)
{
var q = _query;
return await ExecuteQueryAndMapAsync<T, TModel>(q.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<T?> SelectFirstOrDefaultAsync(CancellationToken cancellationToken = default)
=> (await SelectFirstOrDefaultWithResultAsync(cancellationToken).ConfigureAwait(false)).Value;
/// <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 async Task<Result<T?>> SelectFirstOrDefaultWithResultAsync(CancellationToken cancellationToken = default)
{
var q = _query;
return await ExecuteQueryAndMapAsync<T?, TModel?>(q.SelectFirstOrDefaultWithResultAsync, cancellationToken).ConfigureAwait(false);
}
/// <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, T>, new() where TColl : ICollection<T>, new() => new TCollResult
{
Paging = _query.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, T>, new() where TColl : ICollection<T>, new() => new TCollResult
{
Paging = _query.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<T>, 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<T>, 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<T>, 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<T>
=> (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<T>
=> 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<T>
{
collection.ThrowIfNull(nameof(collection));
var mapper = Mapper;
var result = await _query.SelectQueryWithResultAsync(item =>
{
var val = mapper.Map<TModel, T>(item, OperationTypes.Get);
if (val is null)
return new InvalidOperationException("Mapping from the EF model must not result in a null value.");
collection.Add(val);
return true;
}, memberName, cancellationToken).ConfigureAwait(false);
return result.ThenAs(() => collection);
}
}
}