generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ValidationContext.cs
552 lines (476 loc) · 28.9 KB
/
ValidationContext.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
using CoreEx.Abstractions.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using CoreEx.Localization;
using CoreEx.Results;
namespace CoreEx.Validation
{
/// <summary>
/// Provides a validation context for an entity.
/// </summary>
/// <typeparam name="TEntity">The entity <see cref="Type"/>.</typeparam>
public class ValidationContext<TEntity> : IValidationContext, IValidationResult<TEntity>
{
private readonly Dictionary<string, MessageItem> _propertyMessages = [];
/// <summary>
/// Initializes a new instance of the <see cref="ValidationContext{TEntity}"/> class.
/// </summary>
/// <param name="value">The entity value.</param>
/// <param name="args">The <see cref="ValidationArgs"/>.</param>
/// <param name="fullyQualifiedEntityNameOverride">Optional <see cref="ValidationArgs.FullyQualifiedEntityName"/> override.</param>
/// <param name="fullyQualifiedJsonEntityNameOverride">Optional <see cref="ValidationArgs.FullyQualifiedJsonEntityName"/> override.</param>
public ValidationContext(TEntity value, ValidationArgs args, string? fullyQualifiedEntityNameOverride = null, string? fullyQualifiedJsonEntityNameOverride = null)
{
args.ThrowIfNull(nameof(args));
Value = value;
FullyQualifiedEntityName = fullyQualifiedEntityNameOverride ?? args.FullyQualifiedEntityName;
FullyQualifiedJsonEntityName = fullyQualifiedJsonEntityNameOverride ?? args.FullyQualifiedJsonEntityName ?? FullyQualifiedEntityName;
UsedJsonNames = args.UseJsonNamesSelection;
Config = args.Config;
SelectedPropertyName = args.SelectedPropertyName;
ShallowValidation = args.ShallowValidation;
}
/// <summary>
/// Gets the <see cref="ExecutionContext.Current"/> instance.
/// </summary>
public CoreEx.ExecutionContext ExecutionContext => ExecutionContext.Current;
/// <inheritdoc/>
public Type EntityType => typeof(TEntity);
/// <inheritdoc/>
object? IValidationResult.Value => Value;
/// <summary>
/// Gets the entity value.
/// </summary>
public TEntity Value { get; }
/// <summary>
/// Gets the entity prefix used for fully qualified <i>entity.property</i> naming (<c>null</c> represents the root).
/// </summary>
public string? FullyQualifiedEntityName { get; }
/// <summary>
/// Gets the entity prefix used for fully qualified JSON <i>entity.property</i> naming (<c>null</c> represents the root).
/// </summary>
public string? FullyQualifiedJsonEntityName { get; }
/// <summary>
/// Gets the optional name of a selected (specific) property to validate for the entity (<c>null</c> indicates to validate all).
/// </summary>
public string? SelectedPropertyName { get; }
/// <summary>
/// Indicates whether to use the JSON name for the <see cref="MessageItem"/> <see cref="MessageItem.Property"/>; by default (<c>false</c>) uses the .NET name.
/// </summary>
public bool UsedJsonNames { get; }
/// <summary>
/// Gets the <see cref="MessageItemCollection"/>.
/// </summary>
public MessageItemCollection? Messages { get; private set; }
/// <summary>
/// Indicates whether there has been a validation error.
/// </summary>
public bool HasErrors { get; private set; }
/// <summary>
/// Indicates that a shallow validation is required; i.e. will only validate the top level properties.
/// </summary>
public bool ShallowValidation { get; }
/// <inheritdoc/>
public IDictionary<string, object?>? Config { get; set; }
/// <summary>
/// Gets (creates) the messages collection.
/// </summary>
/// <returns></returns>
private MessageItemCollection GetMessages()
{
if (Messages == null)
{
Messages = [];
Messages.CollectionChanged += Messages_CollectionChanged;
}
return Messages;
}
/// <summary>
/// Handle the add of a message.
/// </summary>
private void Messages_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
foreach (var m in e.NewItems!)
{
MessageItem mi = (MessageItem)m;
if (mi.Type == MessageType.Error)
HasErrors = true;
}
break;
default:
throw new InvalidOperationException("Operation invalid for Messages; only add supported.");
}
}
/// <inheritdoc/>
public Result? FailureResult { get; private set; }
/// <summary>
/// Sets the <see cref="FailureResult"/>.
/// </summary>
/// <param name="result">The <see cref="Result"/>.</param>
internal void SetFailureResult(Result? result)
{
if (result is null)
return;
if (FailureResult.HasValue)
throw new InvalidOperationException("The ValidationContext is already in a Failure state.");
if (result.Value.IsFailure)
{
FailureResult = result;
HasErrors = true;
}
}
/// <inheritdoc/>
public Exception? ToException() => FailureResult.HasValue ? FailureResult.Value.Error : (HasErrors ? new ValidationException(Messages!) : null);
/// <inheritdoc/>
IValidationResult IValidationResult.ThrowOnError() => ThrowOnError(false);
/// <summary>
/// Throws a <see cref="Exception"/> (typically a <see cref="ValidationException"/>) where an error was found (and optionally if warnings).
/// </summary>
/// <param name="includeWarnings">Indicates whether to throw where only warnings exist.</param>
/// <returns>The <see cref="ValidationContext{TEntity}"/> to support fluent-style method-chaining.</returns>
public ValidationContext<TEntity> ThrowOnError(bool includeWarnings = false)
{
var ex = ToException();
if (ex is not null)
throw ex;
if (includeWarnings && Messages != null && Messages.Any(x => x.Type == MessageType.Warning))
throw new ValidationException(Messages);
return this;
}
/// <inheritdoc/>
public Result<R> ToResult<R>() => FailureResult.HasValue ? FailureResult.Value.Bind<R>() : (HasErrors ? Result<R>.ValidationError(Messages!) : Validation.ConvertValueToResult<TEntity, R>(Value!));
/// <inheritdoc/>
public Result ToResult() => FailureResult ?? (HasErrors ? Result.ValidationError(Messages!) : Result.Success);
/// <summary>
/// Merges a validation result into this.
/// </summary>
/// <param name="context">The <see cref="IValidationContext"/> to merge.</param>
public void MergeResult(IValidationContext context)
{
if (context.FailureResult.HasValue)
SetFailureResult(context.FailureResult.Value);
else
MergeResult(context?.Messages);
}
/// <summary>
/// Merges a <see cref="MessageItemCollection"/> into this.
/// </summary>
/// <param name="messages">The <see cref="MessageItemCollection"/> to merge.</param>
public void MergeResult(MessageItemCollection? messages)
{
if (messages == null || messages.Count == 0)
return;
MessageItemCollection? errs = null;
foreach (var mi in messages)
{
if (!string.IsNullOrEmpty(mi.Property) && mi.Type == MessageType.Error)
{
if (!HasError(mi.Property))
_propertyMessages.Add(mi.Property, mi);
}
(errs ??= GetMessages()).Add(mi);
}
}
/// <summary>
/// Determines whether the specified property has an error.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <returns><c>true</c> where an error exists for the specified property; otherwise, <c>false</c>.</returns>
public bool HasError<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression) => HasError(CreateFullyQualifiedName(propertyExpression));
/// <summary>
/// Determines whether the specified property has an error.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <returns><c>true</c> where an error exists for the specified property; otherwise, <c>false</c>.</returns>
internal bool HasError<TProperty>(PropertyExpression<TEntity, TProperty> propertyExpression) => HasError(CreateFullyQualifiedName(propertyExpression));
/// <summary>
/// Determines whether one of the specified fully qualified property name has an error.
/// </summary>
/// <param name="fullyQualifiedPropertyName">The fully qualified property name.</param>
/// <returns><c>true</c> where an error exists for at least one of the specified properties; otherwise, <c>false</c>.</returns>
public bool HasError(string fullyQualifiedPropertyName) => _propertyMessages.ContainsKey(fullyQualifiedPropertyName);
/// <summary>
/// Gets the error <see cref="MessageItem"/> for the specified property.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <returns>The corresponding <see cref="MessageItem"/>; otherwise, <c>null</c>.</returns>
public MessageItem? GetError<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression) => _propertyMessages.TryGetValue(CreateFullyQualifiedName(propertyExpression), out MessageItem? mi) ? null : mi;
/// <summary>
/// Adds an <see cref="MessageType.Error"/> <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property and explicit text.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="text">The text <see cref="LText"/>.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddError<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, LText text)
{
var pe = CreatePropertyExpression(propertyExpression);
return AddMessage(pe.Name, pe.JsonName, MessageType.Error, text, GetTextAndValue(pe));
}
/// <summary>
/// Adds an <see cref="MessageType.Error"/> <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property, explicit text format and and additional values included in the text. The property
/// friendly text and value are automatically passed as the first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="format">The composite format string.</param>
/// <param name="values">The values that form part of the message text.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddError<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, LText format, params object[] values)
{
var pe = CreatePropertyExpression(propertyExpression);
return AddMessage(pe.Name, pe.JsonName, MessageType.Error, format, [.. GetTextAndValue(pe), .. values]);
}
/// <summary>
/// Adds an <see cref="MessageType.Warning"/> <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property and explicit text. The property friendly text and value are automatically passed as the
/// first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="text">The text <see cref="LText"/>.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddWarning<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, LText text)
{
var pe = CreatePropertyExpression(propertyExpression);
return AddMessage(pe.Name, pe.JsonName, MessageType.Warning, text, GetTextAndValue(pe));
}
/// <summary>
/// Adds an <see cref="MessageType.Warning"/> <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property, explicit text format and and additional values included in the text. The property
/// friendly text and value are automatically passed as the first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="format">The composite format string.</param>
/// <param name="values">The values that form part of the message text.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddWarning<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, LText format, params object[] values)
{
var pe = CreatePropertyExpression(propertyExpression);
return AddMessage(pe.Name, pe.JsonName, MessageType.Warning, format, [.. GetTextAndValue(pe), .. values]);
}
/// <summary>
/// Adds an <see cref="MessageType.Info"/> <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property and explicit text. The property friendly text and value are automatically passed as the
/// first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="text">The text <see cref="LText"/>.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddInfo<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, LText text)
{
var pe = CreatePropertyExpression(propertyExpression);
return AddMessage(pe.Name, pe.JsonName, MessageType.Info, text, GetTextAndValue(pe));
}
/// <summary>
/// Adds an <see cref="MessageType.Info"/> <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property, explicit text format and and additional values included in the text. The property
/// friendly text and value are automatically passed as the first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="format">The composite format string.</param>
/// <param name="values">The values that form part of the message text.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddInfo<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, LText format, params object[] values)
{
var pe = CreatePropertyExpression(propertyExpression);
return AddMessage(pe.Name, pe.JsonName, MessageType.Info, format, [.. GetTextAndValue(pe), .. values]);
}
/// <summary>
/// Adds a <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property and explicit text.
/// </summary>
/// <param name="propertyName">The property name.</param>
/// <param name="jsonPropertyName">The JSON property name.</param>
/// <param name="type">The <see cref="MessageType"/>.</param>
/// <param name="text">The text <see cref="LText"/>.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
internal MessageItem AddMessage(string propertyName, string? jsonPropertyName, MessageType type, LText text)
{
var mi = GetMessages().Add(CreateFullyQualifiedName(propertyName, jsonPropertyName), type, text);
if (type == MessageType.Error && !HasError(mi.Property!))
_propertyMessages.Add(mi.Property!, mi);
return mi;
}
/// <summary>
/// Adds a <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified property, explicit text format and additional values included in the text.
/// </summary>
/// <param name="propertyName">The property name.</param>
/// <param name="jsonPropertyName">The JSON property name.</param>
/// <param name="type">The <see cref="MessageType"/>.</param>
/// <param name="format">The composite format string.</param>
/// <param name="values">The values that form part of the message text.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
internal MessageItem AddMessage(string propertyName, string? jsonPropertyName, MessageType type, LText format, params object?[] values)
{
var mi = GetMessages().Add(CreateFullyQualifiedName(propertyName, jsonPropertyName), type, format, values);
if (type == MessageType.Error && !HasError(mi.Property!))
_propertyMessages.Add(mi.Property!, mi);
return mi;
}
/// <summary>
/// Adds a <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified text.
/// </summary>
/// <param name="type">The <see cref="MessageType"/>.</param>
/// <param name="text">The text <see cref="LText"/>.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddMessage(MessageType type, LText text)
{
var mi = GetMessages().Add(UsedJsonNames ? FullyQualifiedJsonEntityName : FullyQualifiedEntityName, type, text);
if (type == MessageType.Error && !HasError(mi.Property!))
_propertyMessages.Add(mi.Property!, mi);
return mi;
}
/// <summary>
/// Adds a <see cref="MessageItem"/> to the <see cref="Messages"/> for the specified text format and additional values included in the text.
/// </summary>
/// <param name="type">The <see cref="MessageType"/>.</param>
/// <param name="format">The composite format string.</param>
/// <param name="values">The values that form part of the message text.</param>
/// <returns>The <see cref="MessageItem"/>.</returns>
public MessageItem AddMessage(MessageType type, LText format, params object?[] values)
{
var mi = GetMessages().Add(UsedJsonNames ? FullyQualifiedJsonEntityName : FullyQualifiedEntityName, type, format, values);
if (type == MessageType.Error && !HasError(mi.Property!))
_propertyMessages.Add(mi.Property!, mi);
return mi;
}
/// <summary>
/// Checks whether a specified property has not had an error, then executes a predicate to determine whether an error has occurred (returns <c>true</c>) adding an error <see cref="MessageItem"/> for the
/// specified property and text. The property friendly text and value are automatically passed as the first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="predicate">The error checking predicate; a <c>true</c> result indicates an error.</param>
/// <param name="text">The error text <see cref="LText"/>.</param>
/// <returns><c>true</c> indicates that the specified property has had an error, or is now considered in error; otherwise, <c>false</c> for no error.</returns>
public bool Check<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, Func<TProperty, bool> predicate, LText text)
{
var pe = CreatePropertyExpression(propertyExpression);
predicate.ThrowIfNull(nameof(predicate));
if (HasError(pe))
return true;
var tv = GetTextAndValue(pe);
if (!predicate((TProperty)tv[1]))
return false;
AddMessage(pe.Name, pe.JsonName, MessageType.Error, text, tv);
return true;
}
/// <summary>
/// Checks whether a specified property has not had an error, then where <paramref name="when"/> is <c>true</c> adds an error <see cref="MessageItem"/> for the
/// specified property and text. The property friendly text and value are automatically passed as the first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="when"><c>true</c> indicates an error; otherwise, <c>false</c>.</param>
/// <param name="text">The error text <see cref="LText"/>.</param>
/// <returns><c>true</c> indicates that the specified property has had an error, or is now considered in error; otherwise, <c>false</c> for no error.</returns>
public bool Check<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, bool when, LText text)
{
var pe = CreatePropertyExpression(propertyExpression);
if (HasError(pe))
return true;
var tv = GetTextAndValue(pe);
if (!when)
return false;
AddMessage(pe.Name, pe.JsonName, MessageType.Error, text, tv);
return true;
}
/// <summary>
/// Checks whether a specified property has not had an error, then executes a predicate to determine whether an error has occurred (returns <c>true</c>) adding an error <see cref="MessageItem"/> for the
/// specified property, text format and and additional values included in the text. The property friendly text and value are automatically passed as the first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="predicate">The error checking predicate; a <c>false</c> result indicates an error.</param>
/// <param name="format">The error composite format string.</param>
/// <param name="values">The values that form part of the message text.</param>
/// <returns><c>true</c> indicates that the specified property has had an error, or is now considered in error; otherwise, <c>false</c> for no error.</returns>
public bool Check<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, Func<TProperty, bool> predicate, LText format, params object[] values)
{
var pe = CreatePropertyExpression(propertyExpression);
predicate.ThrowIfNull(nameof(predicate));
if (HasError(pe))
return true;
var tv = GetTextAndValue(pe);
if (!predicate((TProperty)tv[1]))
return false;
AddMessage(pe.Name, pe.JsonName, MessageType.Error, format, [.. tv, .. values]);
return true;
}
/// <summary>
/// Checks whether a specified property has not had an error, then where <paramref name="when"/> is <c>true</c> adds an error <see cref="MessageItem"/> for the specified property,
/// text format and and additional values included in the text. The property friendly text and value are automatically passed as the first two arguments to the string formatter.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <param name="when"><c>true</c> indicates an error; otherwise, <c>false</c>.</param>
/// <param name="format">The error composite format string.</param>
/// <param name="values">The values that form part of the message text.</param>
/// <returns><c>true</c> indicates that the specified property has had an error, or is now considered in error; otherwise, <c>false</c> for no error.</returns>
public bool Check<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression, bool when, LText format, params object[] values)
{
var pe = CreatePropertyExpression(propertyExpression);
if (HasError(pe))
return true;
var tv = GetTextAndValue(pe);
if (!when)
return false;
AddMessage(pe.Name, pe.JsonName, MessageType.Error, format, [.. tv, .. values]);
return true;
}
/// <summary>
/// Gets the friendly text and value for a property expression.
/// </summary>
private object[] GetTextAndValue<TProperty>(PropertyExpression<TEntity, TProperty> propertyExpression) => [propertyExpression.Text, propertyExpression.GetValue(Value)!];
/// <summary>
/// Creates the property expression from the expression.
/// </summary>
private static PropertyExpression<TEntity, TProperty> CreatePropertyExpression<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression) => PropertyExpression.Create(propertyExpression);
/// <summary>
/// Gets the text for the specified property.
/// </summary>
/// <typeparam name="TProperty">The property <see cref="Type"/>.</typeparam>
/// <param name="propertyExpression">The property expression.</param>
/// <returns>The property text.</returns>
public LText GetText<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression) => CreatePropertyExpression(propertyExpression).Text;
/// <summary>
/// Creates the fully qualified name from an expression.
/// </summary>
private string CreateFullyQualifiedName<TProperty>(Expression<Func<TEntity, TProperty>> propertyExpression)
=> CreateFullyQualifiedName(CreatePropertyExpression(propertyExpression));
/// <summary>
/// Creates the fully qualified name from a property expression.
/// </summary>
private string CreateFullyQualifiedName<TProperty>(PropertyExpression<TEntity, TProperty> propertyExpression)
=> CreateFullyQualifiedName(propertyExpression.ThrowIfNull(nameof(propertyExpression)).Name, propertyExpression.JsonName);
/// <summary>
/// Creates the fully qualified name using the property and json property names.
/// </summary>
private string CreateFullyQualifiedName(string propertyName, string? jsonPropertyName)
{
propertyName.ThrowIfNullOrEmpty(nameof(propertyName));
return UsedJsonNames ? CreateFullyQualifiedJsonPropertyName(jsonPropertyName ?? propertyName) : CreateFullyQualifiedPropertyName(propertyName);
}
/// <summary>
/// Creates a fully qualified property name for the specified name.
/// </summary>
/// <param name="name">The property name.</param>
/// <returns>The fully qualified property name.</returns>
internal string CreateFullyQualifiedPropertyName(string name) => FullyQualifiedEntityName == null ? name : name.StartsWith('[') ? FullyQualifiedEntityName + name : FullyQualifiedEntityName + "." + name;
/// <summary>
/// Creates a fully qualified JSON property name for the specified name.
/// </summary>
/// <param name="name">The property name.</param>
/// <returns>The fully qualified property name.</returns>
internal string CreateFullyQualifiedJsonPropertyName(string name) => FullyQualifiedJsonEntityName == null ? name : name.StartsWith('[') ? FullyQualifiedJsonEntityName + name : FullyQualifiedJsonEntityName + "." + name;
}
}