generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCosmosDbQueryBase.cs
236 lines (212 loc) · 13.9 KB
/
CosmosDbQueryBase.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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
using CoreEx.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace CoreEx.Cosmos
{
/// <summary>
/// Enables the common <b>CosmosDb</b> query capabilities.
/// </summary>
/// <typeparam name="T">The resultant <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
/// <typeparam name="TSelf">The <see cref="Type"/> itself.</typeparam>
/// <param name="container">The owning <see cref="CosmosDbContainer"/>.</param>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
public abstract class CosmosDbQueryBase<T, TModel, TSelf>(CosmosDbContainer container, CosmosDbArgs dbArgs) where T : class, new() where TModel : class, new() where TSelf : CosmosDbQueryBase<T, TModel, TSelf>
{
/// <summary>
/// Gets the owning <see cref="CosmosDbContainer"/>.
/// </summary>
public CosmosDbContainer Container { get; } = container.ThrowIfNull(nameof(container));
/// <summary>
/// Gets the <see cref="CosmosDbArgs"/>.
/// </summary>
public CosmosDbArgs QueryArgs = dbArgs;
/// <summary>
/// Gets the <see cref="PagingResult"/>.
/// </summary>
public PagingResult? Paging { get; protected set; }
/// <summary>
/// Adds <see cref="PagingArgs"/> to the query.
/// </summary>
/// <param name="paging">The <see cref="PagingArgs"/>.</param>
/// <returns>The <typeparamref name="TSelf"/> instance to suport fluent-style method-chaining.</returns>
public TSelf WithPaging(PagingArgs? paging)
{
Paging = paging == null ? null : (paging is PagingResult pr ? pr : new PagingResult(paging));
return (TSelf)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 <typeparamref name="TSelf"/> instance to suport fluent-style method-chaining.</returns>
public TSelf WithPaging(long skip, long? take = null) => WithPaging(PagingArgs.CreateSkipAndTake(skip, take));
/// <summary>
/// Selects a single item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item.</returns>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<T> SelectSingleAsync(CancellationToken cancellationToken = default) => await SelectSingleWithResultAsync(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>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<Result<T>> SelectSingleWithResultAsync(CancellationToken cancellationToken = default)
{
var result = await SelectArrayWithResultAsync(nameof(SelectSingleAsync), 2, cancellationToken).ConfigureAwait(false);
return result.ThenAs(coll => coll.Single());
}
/// <summary>
/// Selects a single item or default.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<T?> SelectSingleOrDefaultAsync(CancellationToken cancellationToken = default) => await SelectSingleOrDefaultWithResultAsync(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>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<Result<T?>> SelectSingleOrDefaultWithResultAsync(CancellationToken cancellationToken = default)
{
var result = await SelectArrayWithResultAsync(nameof(SelectSingleOrDefaultAsync), 2, cancellationToken).ConfigureAwait(false);
return result.ThenAs(coll => Result<T?>.Ok(coll.SingleOrDefault()));
}
/// <summary>
/// Selects first item.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The first item.</returns>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<T> SelectFirstAsync(CancellationToken cancellationToken = default) => await SelectFirstWithResultAsync(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>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<Result<T>> SelectFirstWithResultAsync(CancellationToken cancellationToken = default)
{
var result = await SelectArrayWithResultAsync(nameof(SelectFirstAsync), 1, cancellationToken).ConfigureAwait(false);
return result.ThenAs(coll => coll.First());
}
/// <summary>
/// Selects first item or default.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The single item or default.</returns>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<T?> SelectFirstOrDefaultAsync(CancellationToken cancellationToken = default) => await SelectFirstOrDefaultWithResultAsync(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>
/// <remarks><see cref="Paging"/> is not supported for this operation.</remarks>
public async Task<Result<T?>> SelectFirstOrDefaultWithResultAsync(CancellationToken cancellationToken = default)
{
var result = await SelectArrayWithResultAsync(nameof(SelectFirstOrDefaultAsync), 1, cancellationToken).ConfigureAwait(false);
return result.ThenAs(coll => Result<T?>.Ok(coll.FirstOrDefault()));
}
/// <summary>
/// Selects an array by limiting the data retrieved.
/// </summary>
private Task<Result<T[]>> SelectArrayWithResultAsync(string caller, long take, CancellationToken cancellationToken)
{
if (Paging != null)
throw new InvalidOperationException($"The {nameof(Paging)} must be null for a {caller}; internally applied paging is needed to limit unnecessary data retrieval.");
WithPaging(0, take);
return ToArrayWithResultAsync(cancellationToken);
}
/// <summary>
/// Executes the query command creating a resultant collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A resultant collection.</returns>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public async Task<TColl> SelectQueryAsync<TColl>(CancellationToken cancellationToken = default) where TColl : ICollection<T>, new() => await SelectQueryWithResultAsync<TColl>(cancellationToken).ConfigureAwait(false);
/// <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>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A resultant collection.</returns>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public async Task<Result<TColl>> SelectQueryWithResultAsync<TColl>(CancellationToken cancellationToken = default) where TColl : ICollection<T>, new()
{
var coll = new TColl();
var result = await SelectQueryWithResultAsync(coll, cancellationToken).ConfigureAwait(false);
return result.ThenAs(() => coll);
}
/// <summary>
/// Executes the query adding to the passed collection.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public async Task SelectQueryAsync<TColl>(TColl coll, CancellationToken cancellationToken = default) where TColl : ICollection<T> => (await SelectQueryWithResultAsync(coll, cancellationToken).ConfigureAwait(false)).ThrowOnError();
/// <summary>
/// Executes the query adding to the passed collection with a <see cref="Result{T}"/>.
/// </summary>
/// <typeparam name="TColl">The collection <see cref="Type"/>.</typeparam>
/// <param name="coll">The collection to add items to.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public abstract Task<Result> SelectQueryWithResultAsync<TColl>(TColl coll, CancellationToken cancellationToken = default) where TColl : ICollection<T>;
/// <summary>
/// Executes the query command creating a resultant array.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A resultant array.</returns>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public async Task<T[]> ToArrayAsync(CancellationToken cancellationToken = default) => await ToArrayWithResultAsync(cancellationToken).ConfigureAwait(false);
/// <summary>
/// Executes the query command creating a resultant array with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>A resultant array.</returns>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public async Task<Result<T[]>> ToArrayWithResultAsync(CancellationToken cancellationToken = default)
{
var list = new List<T>();
var result = await SelectQueryWithResultAsync(list, cancellationToken).ConfigureAwait(false);
return result.ThenAs(list.ToArray);
}
/// <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>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public async Task<TCollResult> SelectResultAsync<TCollResult, TColl>(CancellationToken cancellationToken = default) where TCollResult : ICollectionResult<TColl, T>, new() where TColl : ICollection<T>, new()
=> await SelectResultWithResultAsync<TCollResult, TColl>(cancellationToken).ConfigureAwait(false);
/// <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>
/// <remarks>The <see cref="Paging"/> is also applied, including <see cref="PagingArgs.IsGetCount"/> where requested.</remarks>
public async Task<Result<TCollResult>> SelectResultWithResultAsync<TCollResult, TColl>(CancellationToken cancellationToken = default) where TCollResult : ICollectionResult<TColl, T>, new() where TColl : ICollection<T>, new()
{
var result = await SelectQueryWithResultAsync<TColl>(cancellationToken).ConfigureAwait(false);
return result.ThenAs(coll => new TCollResult { Items = coll, Paging = Paging ?? new PagingResult() });
}
}
}