-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathItem.cs
81 lines (73 loc) · 3.07 KB
/
Item.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
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.Azure.CosmosRepository;
/// <summary>
/// A base helper class that implements IItem
/// </summary>
/// <example>
/// Here is an example subclass item, which adds several properties:
/// <code language="c#">
/// <![CDATA[
/// public class SubItem : Item
/// {
/// public DateTimeOffset Date { get; set; }
/// public string Name { get; set; }
/// public IEnumerable<Child> Children { get; set; }
/// public IEnumerable<string> Tags { get; set; }
/// }
///
/// public class Child
/// {
/// public string Name { get; set; }
/// public DateTime BirthDate { get; set; }
/// }
/// ]]>
/// </code>
/// </example>
public abstract class Item : IItem
{
/// <summary>
/// Gets or sets the item's globally unique identifier.
/// </summary>
/// <remarks>
/// Initialized by <see cref="Guid.NewGuid"/>.
/// </remarks>
[JsonProperty("id")]
public string Id { get; set; } = Guid.NewGuid().ToString();
/// <summary>
/// Gets or sets the item's type name. This is used as a discriminator.
/// </summary>
[JsonProperty("type")]
public string Type { get; set; }
/// <summary>
/// Gets the PartitionKey based on <see cref="GetPartitionKeyValues"/> last record.
/// Implemented explicitly to keep out of Item API
/// </summary>
string IItem.PartitionKey => GetPartitionKeyValues().Last();
/// <summary>
/// Gets the PartitionKeys based on <see cref="GetPartitionKeyValues"/>.
/// Implemented explicitly to keep out of Item API
/// </summary>
IEnumerable<string> IItem.PartitionKeys => GetPartitionKeyValues();
/// <summary>
/// Default constructor, assigns type name to <see cref="Type"/> property.
/// </summary>
public Item() => Type = GetType().Name;
/// <summary>
/// Gets the partition key value for the given <see cref="Item"/> type.
/// When overridden, be sure that the <see cref="PartitionKeyPathAttribute.Paths"/> values correspond
/// to the <see cref="JsonPropertyAttribute.PropertyName"/> values, i.e.; "/partition" and "partition"
/// respectively. If these two values do not correspond an error will occur.
/// </summary>
/// <returns>The <see cref="Item.Id"/> unless overridden by the subclass.</returns>
protected virtual string GetPartitionKeyValue() => Id;
/// <summary>
/// Gets the partition key values for the given <see cref="Item"/> type.
/// When overridden, be sure that the <see cref="PartitionKeyPathAttribute.Paths"/> values correspond
/// to the <see cref="JsonPropertyAttribute.PropertyName"/> values, i.e.; "/partition" and "partition"
/// respectively. If all provided key values do not have a matching property with the equivalent name, an error will occur.
/// Make sure to add the latest inner
/// </summary>
/// <returns>The list with <see cref="Item.Id"/> unless overridden by the subclass.</returns>
protected virtual IEnumerable<string> GetPartitionKeyValues() => new string[] { GetPartitionKeyValue() };
}