-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
AnnotatableBase.cs
475 lines (409 loc) · 17.9 KB
/
AnnotatableBase.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using Microsoft.EntityFrameworkCore.Internal;
namespace Microsoft.EntityFrameworkCore.Infrastructure;
/// <summary>
/// <para>
/// Base class for types that support reading and writing annotations.
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-providers">Implementation of database providers and extensions</see>
/// for more information and examples.
/// </remarks>
public class AnnotatableBase : IAnnotatable
{
private SortedDictionary<string, Annotation>? _annotations;
private ConcurrentDictionary<string, Annotation>? _runtimeAnnotations;
/// <summary>
/// Indicates whether the current object is read-only.
/// </summary>
/// <remarks>
/// Annotations cannot be changed when the object is read-only.
/// Runtime annotations cannot be changed when the object is not read-only.
/// </remarks>
public virtual bool IsReadOnly
=> false;
/// <summary>
/// Throws if the model is not read-only.
/// </summary>
protected virtual void EnsureReadOnly()
{
}
/// <summary>
/// Throws if the model is read-only.
/// </summary>
protected virtual void EnsureMutable()
{
}
/// <summary>
/// Gets all annotations on the current object.
/// </summary>
public virtual IEnumerable<Annotation> GetAnnotations()
=> _annotations?.Values ?? Enumerable.Empty<Annotation>();
/// <summary>
/// Adds an annotation to this object. Throws if an annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="value">The value to be stored in the annotation.</param>
/// <returns>The newly added annotation.</returns>
public virtual Annotation AddAnnotation(string name, object? value)
{
Check.NotEmpty(name, nameof(name));
var annotation = CreateAnnotation(name, value);
return AddAnnotation(name, annotation);
}
/// <summary>
/// Adds an annotation to this object. Throws if an annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="annotation">The annotation to be added.</param>
/// <returns>The added annotation.</returns>
protected virtual Annotation AddAnnotation(string name, Annotation annotation)
{
if (FindAnnotation(name) != null)
{
throw new InvalidOperationException(CoreStrings.DuplicateAnnotation(name, ToString()));
}
SetAnnotation(name, annotation, oldAnnotation: null);
return annotation;
}
/// <summary>
/// Adds annotations to this object.
/// </summary>
/// <param name="annotations">The annotations to be added.</param>
public virtual void AddAnnotations(IEnumerable<IAnnotation> annotations)
=> AddAnnotations(this, annotations);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
internal static void AddAnnotations(AnnotatableBase annotatable, IEnumerable<IAnnotation> annotations)
{
foreach (var annotation in annotations)
{
annotatable.AddAnnotation(annotation.Name, annotation.Value);
}
}
/// <summary>
/// Adds annotations to this object.
/// </summary>
/// <param name="annotations">The annotations to be added.</param>
public virtual void AddAnnotations(IReadOnlyDictionary<string, object?> annotations)
=> AddAnnotations(this, annotations.Select(a => CreateAnnotation(a.Key, a.Value)));
/// <summary>
/// Sets the annotation stored under the given key. Overwrites the existing annotation if an
/// annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="value">The value to be stored in the annotation.</param>
public virtual void SetAnnotation(string name, object? value)
{
var oldAnnotation = FindAnnotation(name);
if (oldAnnotation != null
&& Equals(oldAnnotation.Value, value))
{
return;
}
SetAnnotation(name, CreateAnnotation(name, value), oldAnnotation);
}
/// <summary>
/// Sets the annotation stored under the given key. Overwrites the existing annotation if an
/// annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="annotation">The annotation to be set.</param>
/// <param name="oldAnnotation">The annotation being replaced.</param>
/// <returns>The annotation that was set.</returns>
protected virtual Annotation? SetAnnotation(
string name,
Annotation annotation,
Annotation? oldAnnotation)
{
EnsureMutable();
_annotations ??= new SortedDictionary<string, Annotation>(StringComparer.Ordinal);
_annotations[name] = annotation;
return OnAnnotationSet(name, annotation, oldAnnotation);
}
/// <summary>
/// Called when an annotation was set or removed.
/// </summary>
/// <param name="name">The key of the set annotation.</param>
/// <param name="annotation">The annotation set.</param>
/// <param name="oldAnnotation">The old annotation.</param>
/// <returns>The annotation that was set.</returns>
protected virtual Annotation? OnAnnotationSet(
string name,
Annotation? annotation,
Annotation? oldAnnotation)
=> annotation;
/// <summary>
/// Gets the annotation with the given name, returning <see langword="null" /> if it does not exist.
/// </summary>
/// <param name="name">The key of the annotation to find.</param>
/// <returns>
/// The existing annotation if an annotation with the specified name already exists. Otherwise, <see langword="null" />.
/// </returns>
public virtual Annotation? FindAnnotation(string name)
{
Check.NotEmpty(name, nameof(name));
return _annotations == null
? null
: _annotations.TryGetValue(name, out var annotation)
? annotation
: null;
}
/// <summary>
/// Gets the annotation with the given name, throwing if it does not exist.
/// </summary>
/// <param name="annotationName">The key of the annotation to find.</param>
/// <returns>The annotation with the specified name.</returns>
public virtual Annotation GetAnnotation(string annotationName)
=> (Annotation)GetAnnotation(this, annotationName);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
internal static IAnnotation GetAnnotation(IReadOnlyAnnotatable annotatable, string annotationName)
{
Check.NotEmpty(annotationName, nameof(annotationName));
var annotation = annotatable.FindAnnotation(annotationName);
if (annotation == null)
{
throw new InvalidOperationException(CoreStrings.AnnotationNotFound(annotationName, annotatable.ToString()));
}
return annotation;
}
/// <summary>
/// Removes the given annotation from this object.
/// </summary>
/// <param name="name">The annotation to remove.</param>
/// <returns>The annotation that was removed.</returns>
public virtual Annotation? RemoveAnnotation(string name)
{
Check.NotNull(name, nameof(name));
EnsureMutable();
var annotation = FindAnnotation(name);
if (annotation == null)
{
return null;
}
_annotations!.Remove(name);
if (_annotations.Count == 0)
{
_annotations = null;
}
OnAnnotationSet(name, null, annotation);
return annotation;
}
/// <summary>
/// Gets the value annotation with the given name, returning <see langword="null" /> if it does not exist.
/// </summary>
/// <param name="name">The key of the annotation to find.</param>
/// <returns>
/// The value of the existing annotation if an annotation with the specified name already exists.
/// Otherwise, <see langword="null" />.
/// </returns>
public virtual object? this[string name]
{
get => FindAnnotation(name)?.Value;
set
{
Check.NotEmpty(name, nameof(name));
if (value == null)
{
RemoveAnnotation(name);
}
else
{
SetAnnotation(name, value);
}
}
}
/// <summary>
/// Creates a new annotation.
/// </summary>
/// <param name="name">The key of the annotation.</param>
/// <param name="value">The value to be stored in the annotation.</param>
/// <returns>The newly created annotation.</returns>
protected virtual Annotation CreateAnnotation(string name, object? value)
=> new(name, value);
/// <summary>
/// Gets all runtime annotations on the current object.
/// </summary>
public virtual IEnumerable<Annotation> GetRuntimeAnnotations()
=> _runtimeAnnotations == null
? Enumerable.Empty<Annotation>()
: _runtimeAnnotations.OrderBy(p => p.Key).Select(p => p.Value);
/// <summary>
/// Adds a runtime annotation to this object. Throws if an annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="value">The value to be stored in the annotation.</param>
/// <returns>The newly added annotation.</returns>
public virtual Annotation AddRuntimeAnnotation(string name, object? value)
{
Check.NotEmpty(name, nameof(name));
var annotation = CreateRuntimeAnnotation(name, value);
return AddRuntimeAnnotation(name, annotation);
}
/// <summary>
/// Adds a runtime annotation to this object. Throws if an annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="annotation">The annotation to be added.</param>
/// <returns>The added annotation.</returns>
protected virtual Annotation AddRuntimeAnnotation(string name, Annotation annotation)
=> GetOrCreateRuntimeAnnotations().TryAdd(name, annotation)
? annotation
: throw new InvalidOperationException(CoreStrings.DuplicateAnnotation(name, ToString()));
/// <summary>
/// Adds runtime annotations to this object.
/// </summary>
/// <param name="annotations">The annotations to be added.</param>
public virtual void AddRuntimeAnnotations(IEnumerable<Annotation> annotations)
{
foreach (var annotation in annotations)
{
AddRuntimeAnnotation(annotation.Name, annotation);
}
}
/// <summary>
/// Adds runtime annotations to this object.
/// </summary>
/// <param name="annotations">The annotations to be added.</param>
public virtual void AddRuntimeAnnotations(IReadOnlyDictionary<string, object?> annotations)
=> AddRuntimeAnnotations(annotations.Select(a => CreateRuntimeAnnotation(a.Key, a.Value)));
/// <summary>
/// Sets the runtime annotation stored under the given key. Overwrites the existing annotation if an
/// annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="value">The value to be stored in the annotation.</param>
public virtual Annotation SetRuntimeAnnotation(string name, object? value)
=> GetOrCreateRuntimeAnnotations().AddOrUpdate(
name,
static (n, a) => a.Annotatable.CreateRuntimeAnnotation(n, a.Value),
static (n, oldAnnotation, a) =>
!Equals(oldAnnotation.Value, a.Value) ? a.Annotatable.CreateRuntimeAnnotation(n, a.Value) : oldAnnotation,
(Value: value, Annotatable: this));
/// <summary>
/// Sets the runtime annotation stored under the given key. Overwrites the existing annotation if an
/// annotation with the specified name already exists.
/// </summary>
/// <param name="name">The key of the annotation to be added.</param>
/// <param name="annotation">The annotation to be set.</param>
/// <param name="oldAnnotation">The annotation being replaced.</param>
/// <returns>The annotation that was set.</returns>
protected virtual Annotation SetRuntimeAnnotation(
string name,
Annotation annotation,
Annotation? oldAnnotation)
{
GetOrCreateRuntimeAnnotations()[name] = annotation;
return annotation;
}
/// <summary>
/// Gets the value of the runtime annotation with the given name, adding it if one does not exist.
/// </summary>
/// <param name="name">The name of the annotation.</param>
/// <param name="valueFactory">The factory used to create the value if the annotation doesn't exist.</param>
/// <param name="factoryArgument">An argument for the factory method.</param>
/// <returns>
/// The value of the existing runtime annotation if an annotation with the specified name already exists.
/// Otherwise a newly created value.
/// </returns>
public virtual TValue GetOrAddRuntimeAnnotationValue<TValue, TArg>(
string name,
Func<TArg?, TValue> valueFactory,
TArg? factoryArgument)
=> (TValue)GetOrCreateRuntimeAnnotations().GetOrAdd(
name,
static (n, t) => t.Annotatable.CreateRuntimeAnnotation(n, t.CreateValue(t.Argument)),
(CreateValue: valueFactory, Argument: factoryArgument, Annotatable: this)).Value!;
/// <summary>
/// Gets the runtime annotation with the given name, returning <see langword="null" /> if it does not exist.
/// </summary>
/// <param name="name">The key of the annotation to find.</param>
/// <returns>
/// The existing annotation if an annotation with the specified name already exists. Otherwise, <see langword="null" />.
/// </returns>
public virtual Annotation? FindRuntimeAnnotation(string name)
{
Check.NotEmpty(name, nameof(name));
return _runtimeAnnotations == null
? null
: _runtimeAnnotations.TryGetValue(name, out var annotation)
? annotation
: null;
}
/// <summary>
/// Removes the given runtime annotation from this object.
/// </summary>
/// <param name="name">The annotation to remove.</param>
/// <returns>The annotation that was removed.</returns>
public virtual Annotation? RemoveRuntimeAnnotation(string name)
{
Check.NotNull(name, nameof(name));
EnsureReadOnly();
if (_runtimeAnnotations == null)
{
return null;
}
_runtimeAnnotations.Remove(name, out var annotation);
return annotation;
}
/// <summary>
/// Creates a new runtime annotation.
/// </summary>
/// <param name="name">The key of the annotation.</param>
/// <param name="value">The value to be stored in the annotation.</param>
/// <returns>The newly created annotation.</returns>
protected virtual Annotation CreateRuntimeAnnotation(string name, object? value)
=> new(name, value);
private ConcurrentDictionary<string, Annotation> GetOrCreateRuntimeAnnotations()
{
EnsureReadOnly();
return NonCapturingLazyInitializer.EnsureInitialized(
ref _runtimeAnnotations, (object?)null, static _ => new ConcurrentDictionary<string, Annotation>());
}
/// <inheritdoc />
object? IReadOnlyAnnotatable.this[string name]
{
[DebuggerStepThrough]
get => this[name];
}
/// <inheritdoc />
[DebuggerStepThrough]
IEnumerable<IAnnotation> IReadOnlyAnnotatable.GetAnnotations()
=> GetAnnotations();
/// <inheritdoc />
[DebuggerStepThrough]
IAnnotation? IReadOnlyAnnotatable.FindAnnotation(string name)
=> FindAnnotation(name);
/// <inheritdoc />
IEnumerable<IAnnotation> IAnnotatable.GetRuntimeAnnotations()
=> GetRuntimeAnnotations();
/// <inheritdoc />
IAnnotation? IAnnotatable.FindRuntimeAnnotation(string name)
=> FindRuntimeAnnotation(name);
/// <inheritdoc />
IAnnotation IAnnotatable.AddRuntimeAnnotation(string name, object? value)
=> AddRuntimeAnnotation(name, value);
/// <inheritdoc />
IAnnotation? IAnnotatable.RemoveRuntimeAnnotation(string name)
=> RemoveRuntimeAnnotation(name);
/// <inheritdoc />
[DebuggerStepThrough]
IAnnotation IAnnotatable.SetRuntimeAnnotation(string name, object? value)
=> SetRuntimeAnnotation(name, value);
}