Home Ā» New in .NET 8.0 [9]: New and expanded data annotations

New in .NET 8.0 [9]: New and expanded data annotations

by admin
New in .NET 8.0 [9]: New and expanded data annotations

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 , and in Blazor. A validation can also be carried out independently of a control in the program code. There are also the classes ValidationContext, Validator, ValidationResult and ValidationException.

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.

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.

See also  New in .NET 7.0 [19]: Statistics for the memory cache

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;

///

/// New and extended annotations for validation ///

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 Languages ā€‹ā€‹{ get; set; } = new(); // New in .NET 8.0: Lower and upper limits for string length:
[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 Websites { get; set; } = new();

// 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

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy