-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEntityBase.cs
More file actions
289 lines (247 loc) · 11.5 KB
/
Copy pathEntityBase.cs
File metadata and controls
289 lines (247 loc) · 11.5 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
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
namespace CoreEx.DomainDriven;
/// <summary>
/// Provides the base <see href="https://en.wikipedia.org/wiki/Domain-driven_design">domain-driven</see> entity functionality.
/// </summary>
public abstract class EntityBase : IEntity
{
/// <summary>
/// Gets the read-only error message.
/// </summary>
public const string ReadOnlyErrorMessage = "The operation is not valid due to the current state being read-only.";
/// <inheritdoc/>
object? IIdentifierCore.Id => throw new NotSupportedException();
/// <inheritdoc/>
[JsonIgnore]
public abstract CompositeKey EntityKey { get; }
/// <inheritdoc/>
[JsonIgnore]
Type IIdentifierCore.IdType => throw new NotSupportedException();
/// <inheritdoc/>
[JsonIgnore]
bool IIdentifierCore.IsIdReadOnly => true;
/// <inheritdoc/>
void IIdentifierCore.SetIdentifier(object? id) => throw new InvalidOperationException("Identifier is read-only.");
/// <inheritdoc/>
[JsonIgnore]
public bool IsReadOnly { get; private set; }
/// <inheritdoc/>
[JsonIgnore]
public PersistenceState PersistenceState { get; private set; }
/// <summary>
/// Gets the <see cref="ChangeLog"/>.
/// </summary>
public ChangeLog? ChangeLog { get; protected set; }
/// <summary>
/// Gets the entity tag.
/// </summary>
public string? ETag { get; protected set; }
/// <summary>
/// Sets (overrides) the <see cref="PersistenceState"/>.
/// </summary>
/// <param name="state">The new <see cref="DomainDriven.PersistenceState"/>.</param>
/// <remarks>This method does not check <see cref="IsReadOnly"/> by design as this is considered independent to.</remarks>
protected void SetPersistenceState(PersistenceState state)
{
// Nothing doing!
if (state == PersistenceState)
return;
// Validate state transition.
state.ThrowWhen(state => state == PersistenceState.Unknown, $"The {nameof(PersistenceState)} cannot be set to '{PersistenceState.Unknown}'.");
state.ThrowWhen(state => state == PersistenceState.NotModified && PersistenceState != PersistenceState.Unknown, $"The {nameof(PersistenceState)} can only be set to '{PersistenceState.NotModified}' from '{PersistenceState.Unknown}' state.");
// Transition to the new state.
PersistenceState = state;
}
/// <summary>
/// Encapsulates modification to the entity.
/// </summary>
/// <param name="action">The action that performs the entity modification.</param>
/// <remarks>Wraps the invocation of the <paramref name="action"/> by performing the following:
/// <list type="bullet">
/// <item><see cref="CheckCanMutate"/>.</item>
/// <item><see cref="CheckReadOnly"/>.</item>
/// <item>Invokes the <paramref name="action"/>.</item>
/// <item>Sets to <see cref="PersistenceState.Modified"/> (where <see cref="PersistenceState.NotModified"/>).</item>
/// <item><see cref="OnMutate"/>.</item>
/// <item>Raises <see cref="Mutated"/> event.</item>
/// </list>
/// </remarks>
protected void Modify(Action? action = null)
{
CheckCanMutate();
CheckReadOnly();
action?.Invoke();
if (PersistenceState.IsNotModified)
SetPersistenceState(PersistenceState.Modified);
OnMutate();
Mutated?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Encapsulates modification to the entity with a corresponding result.
/// </summary>
/// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam>
/// <param name="function">The function that performs the entity modification.</param>
/// <returns>The resulting value.</returns>
/// <remarks>Wraps the invocation of the <paramref name="function"/> by performing the following:
/// <list type="bullet">
/// <item><see cref="CheckCanMutate"/>.</item>
/// <item><see cref="CheckReadOnly"/>.</item>
/// <item>Invokes the <paramref name="function"/>.</item>
/// <item>Sets to <see cref="PersistenceState.Modified"/> (where <see cref="PersistenceState.NotModified"/>).</item>
/// <item><see cref="OnMutate"/>.</item>
/// <item>Raises <see cref="Mutated"/> event.</item>
/// </list>
/// </remarks>
protected TResult Modify<TResult>(Func<TResult> function)
{
CheckCanMutate();
CheckReadOnly();
var result = function.ThrowIfNull()();
if (PersistenceState.IsNotModified)
SetPersistenceState(PersistenceState.Modified);
OnMutate();
Mutated?.Invoke(this, EventArgs.Empty);
return result;
}
/// <summary>
/// Encapsulates modification to the entity, finally setting to read-only.
/// </summary>
/// <param name="action">The optional action that performs the entity modification.</param>
/// <remarks>Wraps the invocation of the <paramref name="action"/> by performing the following:
/// <list type="bullet">
/// <item><see cref="CheckCanMutate"/>.</item>
/// <item><see cref="CheckReadOnly"/>.</item>
/// <item>Invokes the <paramref name="action"/>.</item>
/// <item>Sets to <see cref="PersistenceState.Modified"/> (where <see cref="PersistenceState.NotModified"/>).</item>
/// <item><see cref="OnMutate"/>.</item>
/// <item>Raises <see cref="Mutated"/> event.</item>
/// <item><see cref="MakeReadOnly"/>.</item>
/// </list>
/// </remarks>
protected void ModifyAndMakeReadOnly(Action? action = null)
{
Modify(action);
MakeReadOnly();
}
/// <summary>
/// Encapsulates modification to the entity with a corresponding result, finally setting to read-only.
/// </summary>
/// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam>
/// <param name="function">The function that performs the entity modification.</param>
/// <returns>The resulting value.</returns>
/// <remarks>Wraps the invocation of the <paramref name="function"/> by performing the following:
/// <list type="bullet">
/// <item><see cref="CheckCanMutate"/>.</item>
/// <item><see cref="CheckReadOnly"/>.</item>
/// <item>Invokes the <paramref name="function"/>.</item>
/// <item>Sets to <see cref="PersistenceState.Modified"/> (where <see cref="PersistenceState.NotModified"/>).</item>
/// <item><see cref="OnMutate"/>.</item>
/// <item>Raises <see cref="Mutated"/> event.</item>
/// <item><see cref="MakeReadOnly"/>.</item>
/// </list>
/// </remarks>
protected TResult ModifyAndMakeReadOnly<TResult>(Func<TResult> function)
{
var result = Modify(function);
MakeReadOnly();
return result;
}
/// <summary>
/// Encapsulates marking for removal (deletion) of the entity, finally setting to read-only.
/// </summary>
/// <param name="action">The optional action that performs any entity modification.</param>
/// <remarks>Wraps the invocation of the <paramref name="action"/> by performing the following:
/// <list type="bullet">
/// <item><see cref="CheckCanMutate"/>.</item>
/// <item><see cref="CheckReadOnly"/>.</item>
/// <item>Invokes the optional <paramref name="action"/>.</item>
/// <item>Sets to <see cref="PersistenceState.Removed"/>.</item>
/// <item><see cref="OnMutate"/>.</item>
/// <item>Raises <see cref="Mutated"/> event.</item>
/// <item><see cref="MakeReadOnly"/>.</item>
/// </list>
/// </remarks>
protected void Remove(Action? action = null)
{
CheckCanMutate();
CheckReadOnly();
action?.Invoke();
SetPersistenceState(PersistenceState.Removed);
OnMutate();
Mutated?.Invoke(this, EventArgs.Empty);
MakeReadOnly();
}
/// <summary>
/// Encapsulates marking for removal (deletion) of the entity, finally setting to read-only.
/// </summary>
/// <typeparam name="TResult">The result <see cref="Type"/>.</typeparam>
/// <param name="function">The function that performs any entity modification.</param>
/// <returns>The resulting value.</returns>
/// <remarks>Wraps the invocation of the <paramref name="function"/> by performing the following:
/// <list type="bullet">
/// <item><see cref="CheckCanMutate"/>.</item>
/// <item><see cref="CheckReadOnly"/>.</item>
/// <item>Invokes the <paramref name="function"/>.</item>
/// <item>Sets to <see cref="PersistenceState.Removed"/>.</item>
/// <item><see cref="OnMutate"/>.</item>
/// <item>Raises <see cref="Mutated"/> event.</item>
/// <item><see cref="MakeReadOnly"/>.</item>
/// </list>
/// </remarks>
protected TResult Remove<TResult>(Func<TResult> function)
{
CheckCanMutate();
CheckReadOnly();
var result = function.ThrowIfNull()();
SetPersistenceState(PersistenceState.Removed);
OnMutate();
Mutated?.Invoke(this, EventArgs.Empty);
MakeReadOnly();
return result;
}
/// <summary>
/// Makes the entity read-only.
/// </summary>
/// <remarks>See <see cref="IsReadOnly"/>.</remarks>
protected void MakeReadOnly() => IsReadOnly = true;
/// <summary>
/// Checks whether the entity <see cref="IsReadOnly"/> and if so throws an <see cref="InvalidOperationException"/>.
/// </summary>
protected void CheckReadOnly()
{
if (IsReadOnly)
throw new InvalidOperationException(ReadOnlyErrorMessage);
}
/// <summary>
/// Checkes whether the entity can be mutated by invoking <see cref="OnCheckCanMutate"/> and throwing on error.
/// </summary>
protected void CheckCanMutate() => OnCheckCanMutate().ThrowOnError();
/// <summary>
/// Provides an opportunity to perform pre-checks prior to mutation of the entity; i.e. within the <see cref="Modify(Action?)"/> and <see cref="Remove(Action?)"/> methods.
/// </summary>
/// <returns>A <see cref="Result"/> indicating the outcome of the pre-checks.</returns>
/// <remarks>This is invoked by <see cref="CheckCanMutate"/>.</remarks>
protected virtual Result OnCheckCanMutate() => Result.Success;
/// <summary>
/// Provides an opportunity to perform additional actions after mutation of the entity; i.e. within the <see cref="Modify(Action?)"/> and <see cref="Remove(Action?)"/> methods.
/// </summary>
/// <remarks>This is invoked by the <see cref="Modify(Action?)"/> and <see cref="Remove(Action?)"/> methods.</remarks>
protected virtual void OnMutate() { }
/// <summary>
/// Occurs when the entity is mutated.
/// </summary>
public event EventHandler? Mutated;
/// <summary>
/// Sets (overrides) the <see cref="ChangeLog"/>.
/// </summary>
/// <param name="changeLog">The <see cref="Entities.ChangeLog"/>.</param>
/// <remarks>Bypasses <see cref="IsReadOnly"/> checking and will not result in an <see cref="PersistenceState"/> change by design; intended to enable setting during hydration from a data source.</remarks>
public void SetChangeLog(ChangeLog? changeLog) => ChangeLog = changeLog;
/// <summary>
/// Sets (overrides) the <see cref="ETag"/>.
/// </summary>
/// <param name="eTag">The entity tag.</param>
/// <remarks>Bypasses <see cref="IsReadOnly"/> checking and will not result in an <see cref="PersistenceState"/> change by design; intended to enable setting during hydration from a data source.</remarks>
public void SetETag(string? eTag) => ETag = eTag;
/// <inheritdoc/>
public override string? ToString() => ((IEntityKey)this).EntityKey.ToString();
}