Repository navigation
Open
Labels
api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.ComponentModel.DataAnnotationspartner-impactThis issue impacts a partner who needs to be kept updated
Milestone
Description
Background and motivation
The RangeAttribute currently supports int and double ranges out of the box using dedicated constructor overloads, however any other range necessitates using this overload requiring the operand type as well as string formatted representations of the lower and upper bounds. This only works for types supported by the TypeConverter class expressing limits in strings forces concerns around culture-sensitive formatting:
public class TimeSpanRangeAttribute : RangeAttribute
{
public TimeSpanRangeAttribute(int minMilliseconds, int maxMilliseconds)
: base(type: typeof(TimeSpan),
minimum: TimeSpan.FromMilliseconds(minMilliseconds).ToString("c"),
maximum: TimeSpan.FromMilliseconds(maxMilliseconds).ToString("c"))
{
ParseLimitsInInvariantCulture = true;
}
}I've been working on a prototype that adds a protected constructor which accepts arbitrary IComparable bounds directly.
API Proposal
namespace System.ComponentModel.DataAnnotations;
public partial class RangeAttribute
{
public RangeAttribute(int minimum, int maximum);
public RangeAttribute(double minimum, double maximum);
+ public RangeAttribute(IComparable minimum, IComparable maximum);
[RequiresUnreferencedCode("Generic TypeConverters may require the generic types to be annotated.")]
public RangeAttribute(Type type, string minimum, string maximum);
}API Usage
The above example is now rendered as follows:
public class TimeSpanMillisecondRangeAttribute : RangeAttribute
{
public TimeSpanMillisecondRangeAttribute(int minimumMs, int maximumMs)
: base(TimeSpan.FromMilliseconds(minimumMs), TimeSpan.FromMilliseconds(maximumMs))
{ }
}Alternative Designs
- I marked the proposed constructor as
protected, since marking it public would add OOTB support for things like string or long ranges using their built-in IComparable implementation. Marking itprotectedemphasizes its use as an extensibility point for user-defined derived validation attributes. If people thinks it is useful, we could mark aspublicinstead. - Proposal leaves out
IComparersupport since the validator implementation is oriented around handling ofIComparablevalues.
Risks
No response
Reacted by Jeff Handley
Metadata
Metadata
Assignees
Labels
api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.ComponentModel.DataAnnotationspartner-impactThis issue impacts a partner who needs to be kept updated
Activity
jeffhandley commentedon Mar 25, 2023
@eiriktsarpalis I'm inclined to pull this into .NET 8 to group it with the other DataAnnotations enhancements we're making during the release. @geeknoid Would you be able to use this if it was in .NET 8?
/cc @dotnet/area-system-componentmodel-dataannotations
eiriktsarpalis commentedon Mar 27, 2023
Sure, marking as ready for review.
geeknoid commentedon Mar 27, 2023
@jeffhandley Yes, I think we could use this in .NET 8
terrajobst commentedon Apr 25, 2023
RangeAttribute<T>that is abstract and constrains the parameters to have the same type.RangeAttribute<T>doesn't extendRangeAttributesuch that we can constrainTtoIComparable<T>rather thanIComparable.jeffhandley commentedon May 12, 2023
We will not pursue that revised design in .NET 8. Moving to Future and removing the blockingMarks issues that we want to fast track in order to unblock other important work
label.
eerhardt commentedon Oct 30, 2023
Could we also use the
IParsableinterface and create:Then users wouldn't need to create their own derived attribute classes to use types that are IParsable, like TimeSpan.
Regarding naming, I think it would make more sense to use the
Range<T>attribute name for the sealed class - the one that would be used in all the callsites. So maybe it would make sense to say:And then usages would look like:
sbomer commentedon Feb 4, 2025
This proposal would help make dotnet-monitor compatible with native AOT since it uses
[Range(typeof(TimeSpan)), ...], for example here.