generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCosmosDbContainerT.cs
257 lines (218 loc) · 14.7 KB
/
CosmosDbContainerT.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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Cosmos.Model;
using CoreEx.Entities;
using CoreEx.Results;
using Microsoft.Azure.Cosmos;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace CoreEx.Cosmos
{
/// <summary>
/// Provides a typed interface for the primary <see cref="CosmosDbContainer"/> operations.
/// </summary>
/// <typeparam name="T">The entity <see cref="Type"/>.</typeparam>
/// <typeparam name="TModel">The cosmos model <see cref="Type"/>.</typeparam>
public sealed class CosmosDbContainer<T, TModel> where T : class, IEntityKey, new() where TModel : class, IEntityKey, new()
{
private CosmosDbModelContainer<TModel>? _model;
/// <summary>
/// Initializes a new instance of the <see cref="CosmosDbContainer{T, TModel}"/> class.
/// </summary>
/// <param name="owner">The owning <see cref="CosmosDbContainer"/>.</param>
internal CosmosDbContainer(CosmosDbContainer owner)
{
Container = owner.ThrowIfNull(nameof(owner));
CosmosContainer = Container.CosmosContainer;
}
/// <summary>
/// Gets the owning <see cref="CosmosDbContainer"/>.
/// </summary>
public CosmosDbContainer Container { get; }
/// <summary>
/// Gets the <see cref="Microsoft.Azure.Cosmos.Container"/>.
/// </summary>
public Container CosmosContainer { get; }
/// <summary>
/// Gets the typed <see cref="CosmosDbModelContainer{TModel}"/>.
/// </summary>
public CosmosDbModelContainer<TModel> Model => _model ??= new(Container);
#region Query
/// <summary>
/// Gets (creates) a <see cref="CosmosDbQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbQuery{T, TModel}"/>.</returns>
public CosmosDbQuery<T, TModel> Query(Func<IQueryable<TModel>, IQueryable<TModel>>? query) => Container.Query<T, TModel>(query);
/// <summary>
/// Gets (creates) a <see cref="CosmosDbQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <param name="partitionKey">The <see cref="PartitionKey"/>.</param>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbQuery{T, TModel}"/>.</returns>
public CosmosDbQuery<T, TModel> Query(PartitionKey? partitionKey = null, Func<IQueryable<TModel>, IQueryable<TModel>>? query = null) => Container.Query<T, TModel>(partitionKey, query);
/// <summary>
/// Gets (creates) a <see cref="CosmosDbQuery{T, TModel}"/> to enable LINQ-style queries.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="query">The function to perform additional query execution.</param>
/// <returns>The <see cref="CosmosDbQuery{T, TModel}"/>.</returns>
public CosmosDbQuery<T, TModel> Query(CosmosDbArgs dbArgs, Func<IQueryable<TModel>, IQueryable<TModel>>? query = null) => Container.Query<T, TModel>(dbArgs, query);
#endregion
#region Get
/// <summary>
/// Gets the entity for the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<T?> GetAsync(CompositeKey key, CancellationToken cancellationToken = default) => Container.GetAsync<T, TModel>(key, cancellationToken);
/// <summary>
/// Gets the entity for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<Result<T?>> GetWithResultAsync(CompositeKey key, CancellationToken cancellationToken = default) => Container.GetWithResultAsync<T, TModel>(key, cancellationToken);
/// <summary>
/// Gets the entity for the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="CosmosDbContainer.DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<T?> GetAsync(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) => Container.GetAsync<T, TModel>(key, partitionKey, cancellationToken);
/// <summary>
/// Gets the entity for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="CosmosDbContainer.DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<Result<T?>> GetWithResultAsync(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) => Container.GetWithResultAsync<T, TModel>(key, partitionKey, cancellationToken);
/// <summary>
/// Gets the entity for the specified <paramref name="key"/>.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<T?> GetAsync(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) => Container.GetAsync<T, TModel>(dbArgs, key, cancellationToken);
/// <summary>
/// Gets the entity for the specified <paramref name="key"/> with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The entity value where found; otherwise, <c>null</c> (see <see cref="CosmosDbArgs.NullOnNotFound"/>).</returns>
public Task<Result<T?>> GetWithResultAsync(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) => Container.GetWithResultAsync<T, TModel>(dbArgs, key, cancellationToken);
#endregion
#region Create
/// <summary>
/// Creates the entity.
/// </summary>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public Task<T> CreateAsync(T value, CancellationToken cancellationToken = default) => Container.CreateAsync<T, TModel>(value, cancellationToken);
/// <summary>
/// Creates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public Task<Result<T>> CreateWithResultAsync(T value, CancellationToken cancellationToken = default) => Container.CreateWithResultAsync<T, TModel>(value, cancellationToken);
/// <summary>
/// Creates the entity.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public Task<T> CreateAsync(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) => Container.CreateAsync<T, TModel>(dbArgs, value, cancellationToken);
/// <summary>
/// Creates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The created value.</returns>
public Task<Result<T>> CreateWithResultAsync(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) => Container.CreateWithResultAsync<T, TModel>(dbArgs, value, cancellationToken);
#endregion
#region Update
/// <summary>
/// Updates the entity.
/// </summary>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public Task<T> UpdateAsync(T value, CancellationToken cancellationToken = default) => Container.UpdateAsync<T, TModel>(value, cancellationToken);
/// <summary>
/// Updates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public Task<Result<T>> UpdateWithResultAsync(T value, CancellationToken cancellationToken = default) => Container.UpdateWithResultAsync<T, TModel>(value, cancellationToken);
/// <summary>
/// Updates the entity.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public Task<T> UpdateAsync(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) => Container.UpdateAsync<T, TModel>(dbArgs, value, cancellationToken);
/// <summary>
/// Updates the entity with a <see cref="Result{T}"/>.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>.</param>
/// <param name="value">The value to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
/// <returns>The updated value.</returns>
public Task<Result<T>> UpdateWithResultAsync(CosmosDbArgs dbArgs, T value, CancellationToken cancellationToken = default) => Container.UpdateWithResultAsync<T, TModel>(dbArgs, value, cancellationToken);
#endregion
#region Delete
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task DeleteAsync(CompositeKey key, CancellationToken cancellationToken = default) => Container.DeleteAsync<T, TModel>(key, cancellationToken);
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteWithResultAsync(CompositeKey key, CancellationToken cancellationToken = default) => Container.DeleteWithResultAsync<T, TModel>(key, cancellationToken);
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="CosmosDbContainer.DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task DeleteAsync(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) => Container.DeleteAsync<T, TModel>(key, partitionKey, cancellationToken);
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="partitionKey">The <see cref="PartitionKey"/>. Defaults to <see cref="CosmosDbContainer.DbArgs"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteWithResultAsync(CompositeKey key, PartitionKey? partitionKey, CancellationToken cancellationToken = default) => Container.DeleteWithResultAsync<T, TModel>(key, partitionKey, cancellationToken);
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/>.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>..</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task DeleteAsync(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) => Container.DeleteAsync<T, TModel>(dbArgs, key, cancellationToken);
/// <summary>
/// Deletes the entity for the specified <paramref name="key"/> with a <see cref="Result"/>.
/// </summary>
/// <param name="dbArgs">The <see cref="CosmosDbArgs"/>..</param>
/// <param name="key">The <see cref="CompositeKey"/>.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
public Task<Result> DeleteWithResultAsync(CosmosDbArgs dbArgs, CompositeKey key, CancellationToken cancellationToken = default) => Container.DeleteWithResultAsync<T, TModel>(dbArgs, key, cancellationToken);
#endregion
}
}