Skip to content

Commit cd8abf7

Browse files
committed
Use Task.Run for async validation methods to improve thread utilization
- Changed from Task.FromResult to Task.Run in async implementations - This ensures the validation work runs on a thread pool thread rather than blocking the calling thread - While System.ComponentModel.DataAnnotations is still synchronous, this approach better aligns with proper async patterns in .NET - Provides better performance characteristics in high-concurrency scenarios (like web applications) - Maintains exact same functional behavior as before The underlying validation still uses the synchronous Validator.TryValidateObject methods, but the async API now properly releases calling threads when awaited.
1 parent a8007a7 commit cd8abf7

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/RecursiveDataAnnotationsValidation/RecursiveDataAnnotationValidator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public bool TryValidateObjectRecursive(
5353
/// <param name="validationContext">Validation context.</param>
5454
/// <param name="validationResults">A collection that will be populated if validation errors occur.</param>
5555
/// <returns>Returns true if all validation passes.</returns>
56-
public Task<bool> TryValidateObjectRecursiveAsync(
56+
public async Task<bool> TryValidateObjectRecursiveAsync(
5757
object obj,
5858
ValidationContext validationContext,
5959
List<ValidationResult> validationResults
6060
)
6161
{
62-
return Task.FromResult(TryValidateObjectRecursive(
62+
return await Task.Run(() => TryValidateObjectRecursive(
6363
obj,
6464
validationResults,
6565
validationContext.Items
@@ -71,13 +71,13 @@ List<ValidationResult> validationResults
7171
/// <param name="validationResults">A collection that will be populated if validation errors occur.</param>
7272
/// <param name="validationContextItems">Validation context items.</param>
7373
/// <returns>Returns true if all validation passes.</returns>
74-
public Task<bool> TryValidateObjectRecursiveAsync(
74+
public async Task<bool> TryValidateObjectRecursiveAsync(
7575
object obj,
7676
List<ValidationResult> validationResults,
7777
IDictionary<object, object> validationContextItems = null
7878
)
7979
{
80-
return Task.FromResult(TryValidateObjectRecursive(
80+
return await Task.Run(() => TryValidateObjectRecursive(
8181
obj,
8282
validationResults,
8383
new HashSet<object>(),

0 commit comments

Comments
 (0)