| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ValidationGraphVisitor.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using MyNet.Reflection; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Observable.Behaviors; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Provides functionality to traverse an object graph and validate objects that implement the IValidationAware interfac |
| | | 16 | | /// </summary> |
| | | 17 | | public static class ValidationGraphVisitor |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Performs a depth-first traversal of the object graph starting from the specified root object, invoking the Valid |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="root">The root object of the graph to validate.</param> |
| | | 23 | | /// <returns>True if all validatable objects in the graph are valid; otherwise, false.</returns> |
| | | 24 | | public static bool Validate(object? root) |
| | | 25 | | { |
| | 12 | 26 | | if (root is null) |
| | 3 | 27 | | return true; |
| | | 28 | | |
| | 9 | 29 | | var visited = new HashSet<object>(ReferenceEqualityComparer.Instance); |
| | | 30 | | |
| | 9 | 31 | | return ValidateInternal(root, visited); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Performs a depth-first traversal of the object graph starting from the specified current object, invoking the Va |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="current">The current object being validated.</param> |
| | | 38 | | /// <param name="visited">A set of objects that have already been visited to prevent infinite loops.</param> |
| | | 39 | | /// <returns>True if all validatable objects in the graph are valid; otherwise, false.</returns> |
| | | 40 | | private static bool ValidateInternal(object current, HashSet<object> visited) |
| | | 41 | | { |
| | 21 | 42 | | if (!visited.Add(current)) |
| | 3 | 43 | | return true; |
| | | 44 | | |
| | 18 | 45 | | var result = true; |
| | | 46 | | |
| | 18 | 47 | | switch (current) |
| | | 48 | | { |
| | | 49 | | case IValidationAware validation: |
| | 9 | 50 | | result &= validation.Validate(); |
| | 9 | 51 | | break; |
| | 0 | 52 | | case ObservableObject observableObject when observableObject.Behaviors.TryGet<IValidationBehavior>(out var b |
| | 0 | 53 | | result &= behavior.Validate(); |
| | | 54 | | break; |
| | | 55 | | } |
| | | 56 | | |
| | 18 | 57 | | TraverseChildren(current, child => result &= ValidateInternal(child, visited)); |
| | | 58 | | |
| | 18 | 59 | | return result; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Traverses the child objects of the specified current object, invoking the provided visitor action for each child |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="current">The current object whose child objects are to be traversed.</param> |
| | | 66 | | /// <param name="visitor">An action to be invoked for each child object.</param> |
| | | 67 | | private static void TraverseChildren(object current, Action<object> visitor) |
| | | 68 | | { |
| | 18 | 69 | | var properties = current.GetType().GetPublicProperties(); |
| | | 70 | | |
| | 102 | 71 | | foreach (var property in properties) |
| | | 72 | | { |
| | 33 | 73 | | var value = property.GetValue(current); |
| | | 74 | | |
| | 33 | 75 | | if (value is null) |
| | | 76 | | continue; |
| | | 77 | | |
| | 30 | 78 | | if (property.PropertyType.IsSimple()) |
| | | 79 | | continue; |
| | | 80 | | |
| | | 81 | | switch (value) |
| | | 82 | | { |
| | | 83 | | case string: |
| | | 84 | | continue; |
| | | 85 | | |
| | | 86 | | case IEnumerable enumerable: |
| | | 87 | | { |
| | 30 | 88 | | foreach (var item in enumerable) |
| | | 89 | | { |
| | 3 | 90 | | if (item is null) |
| | | 91 | | continue; |
| | | 92 | | |
| | 3 | 93 | | visitor(item); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | break; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | default: |
| | 9 | 100 | | visitor(value); |
| | | 101 | | break; |
| | | 102 | | } |
| | | 103 | | } |
| | 18 | 104 | | } |
| | | 105 | | } |
| | | 106 | | |