Date/Time Types - Implementing Net7 System.Numerics Types #117053
-
Hi all, Maybe this is something that's already been done in .NET 9 or is on the roadmap for .NET 10, but I've run into a curious issue today with .NET 8. I have a configuration library built on top of Microsoft.Extensions.Configuration. One of the features of this library is property validation. Here is one such method. /// <summary>
/// Validates the provided <paramref name="entity"/> to ensure it is not null and is between
/// <paramref name="minValue"/> and <paramref name="maxValue"/>.
/// </summary>
/// <typeparam name="TNumber">The underlying numeric type.</typeparam>
/// <param name="entity">The entity to compare.</param>
/// <param name="minValue">The inclusive minimum value.</param>
/// <param name="maxValue">The exclusive maximum value.</param>
/// <param name="name">The name of the parameter to validate.</param>
protected void ValidateWithinRange<TNumber>
(
TNumber? entity,
TNumber? minValue = null,
TNumber? maxValue = null,
[CallerArgumentExpression(nameof(entity))] string? name = null)
where TNumber : struct, IComparisonOperators<TNumber, TNumber, bool>, IMinMaxValue<TNumber>
{
if (ValidateNotNull(entity, name))
{
minValue ??= TNumber.MinValue;
maxValue ??= TNumber.MaxValue;
if (entity >= minValue && entity.Value < maxValue)
{
return;
}
// TODO: Avoid null suppressions.
Errors.Add(GetValueOutOfRangeError(name!, entity.ToString()!, minValue.ToString()!, maxValue.ToString()!));
}
} (The type param description says numeric type but this is a holdover from when I was filtering on I would expect to be able to have a DateTime property (or any of the other date and/or time types) and validate it like so: ValidateWithinRange(options.SomeDate, DateTime.Today.AddYears(-1), DateTime.Today.AddYears(1)); However, none of these types implement I know that adding each of the relevant interfaces is a different amount of work for each interface, but I did want to at least start discussion on which interfaces are worth implementing here and what the challenges would be. Correction: I stated they all have DateTime, DateTimeOffset, and TimeSpan have DateOnly and TimeOnly have |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
cc: @tannergooding |
Beta Was this translation helpful? Give feedback.
Same as with other similar issues.
I had proposed these types include such interfaces in the initial design and API review rejected it at the time pending user scenarios being provided and further discussion around what it would mean for .NET as a whole.
Such built-in types cannot be updated in already shipped versions (.NET 7/8/9). It could be updated in a future version (.NET 10/11/etc) but needs to go through API review for approval first.