Data annotation in the System.ComponentModel.DataAnnotations namespace has been around since the classic .NET Framework. The first like [RegularExpression], [Range] or [DataType] were introduced in .NET Framework 3.5. Further data annotation followed in .NET Framework 4.0 (e.g. [Display] and [Editable]) und .NET Framework 4.5 (z. B. [MaxLength], [Compare], [CreditCard] and [Phone]). This data annotation can be used on classes or properties or fields. They serve to specify the representation (e.g. [Display] and [Editable]) or the validation of values āā(e.g [RegularExpression], [Compare], [Range], [Phone] and [CreditCard]). GUI controls must respect the appropriate annotations.
Advertisement
There are only a few controls that take the display annotations into account ā .NET developers usually create their own controls that create a graphical user interface on the fly at runtime based on the metadata stored in the annotations. With validation annotations, there are more controls that take the annotations into account, including
New properties in .NET 8.0
Die [Range]-Annotation has the additional properties MinimumIsExclusive and MaximumIsExclusive in .NET 8.0 to exclude the specified lower and upper bounds themselves as valid values. As before, the standard is that the boundaries are included.
New annotations in .NET 8.0
Microsoft has also added three new annotation classes:
The new annotation [Length] can be used to set the minimum and maximum length of object sets and strings.With the new annotation [Base64String] you check whether a character string is Base64 encoded [AllowedValues] and [DeniedValues] You can specify a list of permitted values āāfor properties of an object. However, this only works for the values āāin individual properties or fields; Unfortunately, these two annotations do not work when applied to object sets.
The next listing shows a meaningful example of all the new annotation features using the SoftwareArchitect class with some annotated properties. Validation is carried out using the ValidationContext and Validator classes.
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using ITVisions;
using NET8_Console.CustomAnnotations;
namespace NET8_Console.FCL_Annotations;
///
public class SoftwareArchitect { // New in .NET 8.0 ā> Not allowed as a salutation:
[DeniedValues(āā, āDr.ā, āProf. Dr.ā)]
public string? salutation { get; set; } // New in .NET 8.0 ā> Allowed titles:
[AllowedValues(āā, āDr.ā, āProf. Dr.ā)]
public string? title { get; set; } // doesnāt work here because itās a list:
[AllowedValues(āC#ā, āJavaā, āJavaScriptā)]
public List
[Length(2, 50)]
public string? name { get; set; } // New in .NET 8.0: Lower and upper limits for set length:
[Length(0, 3)]
public List
// Neu in .NET 8.0:
[Base64String]
public string? token { get; set; } // MinimumIsExclusive and MaximumIsExclusive // āāare new since .NET 8.0 // > 0 and = and ();
// Validierung durchfĆ¼hren
if (!Validator.TryValidateObject(hs, ctx, results, true))
{
CUI.Error($ā{results.Count} errors validating the objects:ā);
// Fehler ausgeben
foreach (var validationResult in results)
{
CUI.LI(validationResult.ErrorMessage, ConsoleColor.Red);
}
}
else
{
// Es war alles OK
CUI.Success(āValidation succeeded!ā);
}
}
}
When running the program code in the above listing with .NET 8.0, six validation errors occur
(Image: Screenshot by Holger Schwichtenberg)
(rme)
To home page