Skip to content

Commit 3a16de4

Browse files
committed
Move Validate() method to MemberAccessor
In preparation for centralizing around `ItemConfiguration`, we need to move the `Validate()` method from `PropertyConfiguration` to `MemberAccessor`. This is because it relies on access to the `MemberAccessor`'s `GetValue()` method. Currently, `PropertyConfiguration` has access to that via its `MemberAccessor` property. But once `ItemConfiguration` becomes a property off of `ItemMetadata`, it doesn't make sense to maintain the `ItemMetadata` or `MemberAccessor` fields off of `ItemConfiguration` or `PropertyConfiguration`. Instead, callers will simply pass references to `ItemMetadata` (which will include `ItemConfiguration`) instead of `ItemConfiguration` (which previously included `ItemMetadata`). Basically, we're flipping the relationship between these. Further, this can't be added to the `ItemMetadata` base class since it requires access to the `GetValue()` method on the derived `MemberAccessor` class. That's the same reason it was previously added to the `PropertyConfiguration`, not the `ItemConfiguration` class.
1 parent 74cb817 commit 3a16de4

4 files changed

Lines changed: 17 additions & 17 deletions

File tree

OnTopic/Internal/Reflection/MemberAccessor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| Client Ignia, LLC
44
| Project Topics Library
55
\=============================================================================================================================*/
6+
using System.ComponentModel.DataAnnotations;
67
using System.Reflection;
78
using OnTopic.Attributes;
89

@@ -218,6 +219,20 @@ internal void SetValue(object target, object? value, bool allowConversion = fals
218219

219220
}
220221

222+
/*==========================================================================================================================
223+
| METHOD: VALIDATE
224+
\-------------------------------------------------------------------------------------------------------------------------*/
225+
/// <summary>
226+
/// Given a target DTO, will automatically identify any attributes that derive from <see cref="ValidationAttribute"/> and
227+
/// ensure that their conditions are satisfied.
228+
/// </summary>
229+
/// <param name="target">The target DTO to validate the current property on.</param>
230+
internal void Validate(object target) {
231+
foreach (ValidationAttribute validator in CustomAttributes.OfType<ValidationAttribute>()) {
232+
validator.Validate(GetValue(target), Name);
233+
}
234+
}
235+
221236
/*==========================================================================================================================
222237
| IS VALID?
223238
\-------------------------------------------------------------------------------------------------------------------------*/

OnTopic/Mapping/Internal/PropertyConfiguration.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
| Client Ignia, LLC
44
| Project Topics Library
55
\=============================================================================================================================*/
6-
using System.ComponentModel.DataAnnotations;
76
using System.Reflection;
87
using OnTopic.Internal.Reflection;
98
using OnTopic.Mapping.Annotations;
@@ -57,19 +56,5 @@ internal PropertyConfiguration(MemberAccessor memberAccessor, string? attributeP
5756
/// </summary>
5857
internal MemberAccessor MemberAccessor { get; }
5958

60-
/*==========================================================================================================================
61-
| METHOD: VALIDATE
62-
\-------------------------------------------------------------------------------------------------------------------------*/
63-
/// <summary>
64-
/// Given a target DTO, will automatically identify any attributes that derive from <see cref="ValidationAttribute"/> and
65-
/// ensure that their conditions are satisfied.
66-
/// </summary>
67-
/// <param name="target">The target DTO to validate the current property on.</param>
68-
internal void Validate(object target) {
69-
foreach (ValidationAttribute validator in CustomAttributes.OfType<ValidationAttribute>()) {
70-
validator.Validate(MemberAccessor.GetValue(target), MemberAccessor.Name);
71-
}
72-
}
73-
7459
}
7560
}

OnTopic/Mapping/Reverse/ReverseTopicMappingService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ await MapAsync(
286286
/*------------------------------------------------------------------------------------------------------------------------
287287
| Validate fields
288288
\-----------------------------------------------------------------------------------------------------------------------*/
289-
configuration.Validate(source);
289+
propertyAccessor.Validate(source);
290290

291291
/*------------------------------------------------------------------------------------------------------------------------
292292
| Handle property by type

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ await MapAsync(
513513
/*------------------------------------------------------------------------------------------------------------------------
514514
| Validate fields
515515
\-----------------------------------------------------------------------------------------------------------------------*/
516-
configuration.Validate(target);
516+
propertyAccessor.Validate(target);
517517

518518
}
519519

0 commit comments

Comments
 (0)