forked from Avanade/Beef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CosmosDbContainerExtensions.cs
176 lines (157 loc) · 12 KB
/
CosmosDbContainerExtensions.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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef
using Beef.RefData;
using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace Beef.Data.Cosmos
{
/// <summary>
/// Provides <b>Cosmos</b> <see cref="Container"/> extensions.
/// </summary>
public static class CosmosDbContainerExtensions
{
/// <summary>
/// Imports a batch (creates) of <paramref name="items"/> into the <see cref="Container"/>.
/// </summary>
/// <typeparam name="TModel">The model <see cref="Type"/>.</typeparam>
/// <param name="container">The <see cref="Container"/>.</param>
/// <param name="items">The items to import.</param>
/// <param name="partitionKey">The optional partition key; where not specified <see cref="PartitionKey.None"/> is used.</param>
/// <param name="itemRequestOptions">The optional <see cref="ItemRequestOptions"/>.</param>
/// <param name="setIdentifier">Indicates whether to override the <c>Id</c> where entity implements <see cref="Beef.Entities.IIdentifier"/>.</param>
/// <returns>The <see cref="Task"/>.</returns>
/// <remarks>Each item is added individually and is not transactional.</remarks>
public static async Task ImportBatchAsync<TModel>(this Container container, IEnumerable<TModel> items, Func<TModel, PartitionKey?>? partitionKey = null, ItemRequestOptions? itemRequestOptions = null, bool setIdentifier = false) where TModel : class, new()
{
if (container == null)
throw new ArgumentNullException(nameof(container));
if (items == null)
return;
foreach (var item in items)
{
CosmosDbBase.PrepareEntityForCreate(item, setIdentifier);
await container.CreateItemAsync(item, partitionKey?.Invoke(item), itemRequestOptions).ConfigureAwait(false);
}
}
/// <summary>
/// Imports a batch (creates) of <see cref="CosmosDbValue{TModel}"/> <paramref name="items"/> into the <see cref="Container"/>.
/// </summary>
/// <typeparam name="TModel">The model <see cref="Type"/>.</typeparam>
/// <param name="container">The <see cref="Container"/>.</param>
/// <param name="items">The items to import.</param>
/// <param name="partitionKey">The optional partition key; where not specified <see cref="PartitionKey.None"/> is used.</param>
/// <param name="itemRequestOptions">The optional <see cref="ItemRequestOptions"/>.</param>
/// <param name="setIdentifier">Indicates whether to override the <c>Id</c> where entity implements <see cref="Beef.Entities.IIdentifier"/>.</param>
/// <returns>The <see cref="Task"/>.</returns>
/// <remarks>Each item is added individually and is not transactional.</remarks>
public static async Task ImportValueBatchAsync<TModel>(this Container container, IEnumerable<TModel> items, Func<TModel, PartitionKey?>? partitionKey = null, ItemRequestOptions? itemRequestOptions = null, bool setIdentifier = false) where TModel : class, new()
{
if (container == null)
throw new ArgumentNullException(nameof(container));
if (items == null)
return;
foreach (var item in items)
{
var cdv = new CosmosDbValue<TModel>(item);
CosmosDbBase.PrepareEntityForCreate(cdv.Value, setIdentifier);
((ICosmosDbValue)cdv).PrepareBefore();
await container.CreateItemAsync(cdv, partitionKey?.Invoke(item), itemRequestOptions).ConfigureAwait(false);
}
}
/// <summary>
/// Imports a batch (creates) of items specified within a <b>YAML</b> resource (see <see cref="Beef.Yaml.YamlConverter"/>) into the <see cref="Container"/>.
/// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to infer the <see cref="Assembly"/> to find manifest resources (see <see cref="Assembly.GetManifestResourceStream(string)"/>).</typeparam>
/// <typeparam name="TModel">The model <see cref="Type"/>.</typeparam>
/// <param name="container">The <see cref="Container"/>.</param>
/// <param name="yamlResourceName">The YAML resource name (must reside in <c>Cosmos</c> folder within the <typeparamref name="TResource"/> <see cref="Assembly"/>).</param>
/// <param name="name">The YAML node name to load; where <c>null</c> will infer the name from the <typeparamref name="TModel"/>.</param>
/// <param name="partitionKey">The optional partition key; where not specified <see cref="PartitionKey.None"/> is used.</param>
/// <param name="itemRequestOptions">The optional <see cref="ItemRequestOptions"/>.</param>
/// <param name="setIdentifier">Indicates whether to override the <c>Id</c> where entity implements <see cref="Beef.Entities.IIdentifier"/>.</param>
/// <returns>The <see cref="Task"/>.</returns>
/// <remarks>Each item is added individually and is not transactional.</remarks>
public static async Task ImportBatchAsync<TResource, TModel>(this Container container, string yamlResourceName, string? name = null, Func<TModel, PartitionKey?>? partitionKey = null, ItemRequestOptions? itemRequestOptions = null, bool setIdentifier = false) where TModel : class, new()
{
if (container == null)
throw new ArgumentNullException(nameof(container));
using var rs = GetResourceStream<TResource>(Check.NotEmpty(yamlResourceName, nameof(yamlResourceName)));
var yc = Beef.Yaml.YamlConverter.ReadYaml(rs);
await ImportBatchAsync(container, yc.Convert<TModel>(name ?? typeof(TModel).Name), partitionKey, itemRequestOptions, setIdentifier).ConfigureAwait(false);
}
/// <summary>
/// Imports a batch (creates) of <see cref="CosmosDbValue{TModel}"/> items specified within a <b>YAML</b> resource (see <see cref="Beef.Yaml.YamlConverter"/>) into the <see cref="Container"/>.
/// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to infer the <see cref="Assembly"/> to find manifest resources (see <see cref="Assembly.GetManifestResourceStream(string)"/>).</typeparam>
/// <typeparam name="TModel">The item <see cref="Type"/>.</typeparam>
/// <param name="container">The <see cref="Container"/>.</param>
/// <param name="yamlResourceName">The YAML resource name (must reside in <c>Cosmos</c> folder within the <typeparamref name="TResource"/> <see cref="Assembly"/>).</param>
/// <param name="name">The YAML node name to load; where <c>null</c> will infer the name from the <typeparamref name="TModel"/>.</param>
/// <param name="partitionKey">The optional partition key; where not specified <see cref="PartitionKey.None"/> is used.</param>
/// <param name="itemRequestOptions">The optional <see cref="ItemRequestOptions"/>.</param>
/// <param name="setIdentifier">Indicates whether to override the <c>Id</c> where entity implements <see cref="Beef.Entities.IIdentifier"/>.</param>
/// <returns>The <see cref="Task"/>.</returns>
/// <remarks>Each item is added individually and is not transactional.</remarks>
public static async Task ImportValueBatchAsync<TResource, TModel>(this Container container, string yamlResourceName, string? name = null, Func<TModel, PartitionKey?>? partitionKey = null, ItemRequestOptions? itemRequestOptions = null, bool setIdentifier = false) where TModel : class, new()
{
if (container == null)
throw new ArgumentNullException(nameof(container));
using var rs = GetResourceStream<TResource>(Check.NotEmpty(yamlResourceName, nameof(yamlResourceName)));
var yc = Beef.Yaml.YamlConverter.ReadYaml(rs);
await ImportValueBatchAsync(container, yc.Convert<TModel>(name ?? typeof(TModel).Name), partitionKey, itemRequestOptions, setIdentifier).ConfigureAwait(false);
}
/// <summary>
/// Gets the resource stream.
/// </summary>
private static System.IO.Stream GetResourceStream<TResource>(string yamlResourceName)
{
if (string.IsNullOrEmpty(yamlResourceName))
throw new ArgumentNullException(nameof(yamlResourceName));
var ass = typeof(TResource).Assembly;
var rn = ass.GetManifestResourceNames().Where(x => x.EndsWith($"Cosmos.{Check.NotNull(yamlResourceName, nameof(yamlResourceName))}", StringComparison.InvariantCultureIgnoreCase));
if (rn == null || rn.Count() != 1)
throw new ArgumentException($"A single Resource with name ending in 'Cosmos.{yamlResourceName}' not found in Assembly '{ass.FullName}'.", nameof(yamlResourceName));
return ass.GetManifestResourceStream(rn.First()) ?? throw new ArgumentException($"A single Resource with name ending in 'Cosmos.{yamlResourceName}' not found in Assembly '{ass.FullName}'.", nameof(yamlResourceName));
}
/// <summary>
/// Imports a batch (creates) of <see cref="CosmosDbValue{TModel}"/> <b>reference data</b> items specified within a <b>YAML</b> resource (see <see cref="Beef.Yaml.YamlConverter"/>) into the <see cref="Container"/>.
/// </summary>
/// <typeparam name="TResource">The <see cref="Type"/> to infer the <see cref="Assembly"/> to find manifest resources (see <see cref="Assembly.GetManifestResourceStream(string)"/>).</typeparam>
/// <param name="container">The <see cref="Container"/>.</param>
/// <param name="refData">The <see cref="IReferenceDataProvider"/> to infer the underlying reference data types.</param>
/// <param name="yamlResourceName">The YAML resource name (must reside in <c>Cosmos</c> folder within the <typeparamref name="TResource"/> <see cref="Assembly"/>).</param>
/// <param name="partitionKey">The optional partition key; where not specified <see cref="PartitionKey.None"/> is used.</param>
/// <param name="itemRequestOptions">The optional <see cref="ItemRequestOptions"/>.</param>
/// <returns>The <see cref="Task"/>.</returns>
/// <remarks>Each item is added individually and is not transactional. Also, the data is created using a <see cref="CosmosDbValue{ReferenceDataBase}"/> so that multiple reference data types
/// can co-exist within the same collection.</remarks>
public static async Task ImportValueRefDataBatchAsync<TResource>(this Container container, IReferenceDataProvider refData, string yamlResourceName, Func<ReferenceDataBase, PartitionKey?>? partitionKey = null, ItemRequestOptions? itemRequestOptions = null)
{
if (container == null)
throw new ArgumentNullException(nameof(container));
if (refData == null)
throw new ArgumentNullException(nameof(refData));
if (string.IsNullOrEmpty(yamlResourceName))
throw new ArgumentNullException(nameof(yamlResourceName));
using var rs = GetResourceStream<TResource>(yamlResourceName);
var yc = Beef.Yaml.YamlConverter.ReadYaml(rs);
var yt = typeof(Beef.Yaml.YamlConverter);
var ct = typeof(Container);
var emi = typeof(CosmosDbContainerExtensions)
.GetMethods().Where(x => x.Name == nameof(CosmosDbContainerExtensions.ImportValueBatchAsync) && x.GetGenericArguments().Length == 1).Single();
foreach (var rdt in refData.GetAllTypes())
{
var vals = (System.Collections.IEnumerable?)yt.GetMethod("Convert")?.MakeGenericMethod(rdt).Invoke(yc, new object[] { rdt.Name, true, true, true, null! });
if (vals != null)
{
var r = emi.MakeGenericMethod(rdt).Invoke(null, new object[] { container, vals!, partitionKey!, itemRequestOptions!, false });
if (r != null)
await ((Task)r).ConfigureAwait(false);
}
}
}
}
}