-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathEntityMetadata.cs
More file actions
136 lines (121 loc) · 4.62 KB
/
EntityMetadata.cs
File metadata and controls
136 lines (121 loc) · 4.62 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using Microsoft.DurableTask.Entities;
namespace Microsoft.DurableTask.Client.Entities;
/// <summary>
/// Represents entity metadata.
/// </summary>
/// <typeparam name="TState">The type of state held by the metadata.</typeparam>
[JsonConverter(typeof(EntityMetadataConverter))]
public class EntityMetadata<TState>
{
readonly TState? state;
/// <summary>
/// Initializes a new instance of the <see cref="EntityMetadata{TState}"/> class.
/// </summary>
/// <param name="id">The ID of the entity.</param>
/// <param name="state">The state of the entity.</param>
/// <param name="includesState">
/// A value indicating whether the entity state was included in the response, even when the state is <see langword="null"/>.
/// </param>
public EntityMetadata(EntityInstanceId id, TState? state, bool includesState)
{
this.Id = Check.NotDefault(id);
this.IncludesState = includesState;
this.state = state;
}
/// <summary>
/// Initializes a new instance of the <see cref="EntityMetadata{TState}"/> class.
/// </summary>
/// <param name="id">The ID of the entity.</param>
/// <param name="state">The state of the entity.</param>
public EntityMetadata(EntityInstanceId id, TState? state)
: this(id, state, state is not null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EntityMetadata{TState}"/> class.
/// </summary>
/// <param name="id">The ID of the entity.</param>
public EntityMetadata(EntityInstanceId id)
: this(id, default, false)
{
}
/// <summary>
/// Gets the ID for this entity.
/// </summary>
public EntityInstanceId Id { get; }
/// <summary>
/// Gets the time the entity was last modified.
/// </summary>
public DateTimeOffset LastModifiedTime { get; init; }
/// <summary>
/// Gets the size of the backlog queue, if there is a backlog, and if that metric is supported by the backend.
/// </summary>
public int BacklogQueueSize { get; init; }
/// <summary>
/// Gets the instance id of the orchestration that has locked this entity, or null if the entity is not locked.
/// </summary>
public string? LockedBy { get; init; }
/// <summary>
/// Gets a value indicating whether this metadata response includes the entity state.
/// </summary>
/// <remarks>
/// Queries can exclude the state of the entity from the metadata that is retrieved.
/// </remarks>
[MemberNotNullWhen(true, nameof(State))]
[MemberNotNullWhen(true, nameof(state))]
public bool IncludesState { get; }
/// <summary>
/// Gets the state for this entity.
/// </summary>
/// <remarks>
/// This method can only be used when <see cref="IncludesState"/> = <c>true</c>, meaning that the entity state was
/// included in the response returned by the query.
/// </remarks>
/// <exception cref="InvalidOperationException">
/// Thrown if this metadata object was fetched without including the entity state. In which case,
/// <see cref="IncludesState" /> will be <c>false</c>.
/// </exception>
public TState State
{
get
{
if (this.IncludesState)
{
return this.state;
}
throw new InvalidOperationException($"Cannot retrieve state when {nameof(this.IncludesState)}=false");
}
}
}
/// <summary>
/// Represents the metadata for a durable entity instance.
/// </summary>
[JsonConverter(typeof(EntityMetadataConverter))]
public sealed class EntityMetadata : EntityMetadata<SerializedData>
{
/// <summary>
/// Initializes a new instance of the <see cref="EntityMetadata"/> class.
/// </summary>
/// <param name="id">The ID of the entity.</param>
/// <param name="state">The state of this entity.</param>
public EntityMetadata(EntityInstanceId id, SerializedData? state = null)
: base(id, state)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EntityMetadata"/> class.
/// </summary>
/// <param name="id">The ID of the entity.</param>
/// <param name="state">The state of this entity.</param>
/// <param name="includesState">
/// A value indicating whether the entity state was included in the response, even when the state is <see langword="null"/>.
/// </param>
public EntityMetadata(EntityInstanceId id, SerializedData? state, bool includesState)
: base(id, state, includesState)
{
}
}