Pekspro.DataAnnotationValuesExtractor
1.0.0
dotnet add package Pekspro.DataAnnotationValuesExtractor --version 1.0.0
NuGet\Install-Package Pekspro.DataAnnotationValuesExtractor -Version 1.0.0
<PackageReference Include="Pekspro.DataAnnotationValuesExtractor" Version="1.0.0" />
<PackageVersion Include="Pekspro.DataAnnotationValuesExtractor" Version="1.0.0" />
<PackageReference Include="Pekspro.DataAnnotationValuesExtractor" />
paket add Pekspro.DataAnnotationValuesExtractor --version 1.0.0
#r "nuget: Pekspro.DataAnnotationValuesExtractor, 1.0.0"
#:package Pekspro.DataAnnotationValuesExtractor@1.0.0
#addin nuget:?package=Pekspro.DataAnnotationValuesExtractor&version=1.0.0
#tool nuget:?package=Pekspro.DataAnnotationValuesExtractor&version=1.0.0
DataAnnotationValuesExtractor
A C# source generator that automatically extracts values from data annotation
attributes and exposes them as strongly-typed constants. Access your
StringLength, Range, Required and Display attribute values as constants
in your classes.
Why Use This?
When working with data annotations, you often need to reference validation constraints. A good way to solve it is to create constants. But it takes time to do and makes your data models harder to read. And it's harder when your models are auto generated like when you are scaffolding with Entity Framework.
This source generator creates the constants automatically for you. If you have this model:
public partial class Product
{
[Required]
[StringLength(100)]
public string? Name { get; set; }
[Required]
[Range(0.01, 999999.99)]
public decimal Price { get; set; }
}
Constants will be generated that you can access like this:
// Name length constraints
int maxNameLength = Product.Annotations.Name.MaximumLength; // 100
bool nameRequired = Product.Annotations.Name.IsRequired; // true
// Price constraints
double minPrice = Product.Annotations.Price.Minimum; // 0.01
double maxPrice = Product.Annotations.Price.Maximum; // 999999.99
Usage Patterns
There are two ways to configure DataAnnotationValuesExtractor depending on your needs:
1. Direct Approach
Apply [DataAnnotationValues] directly to each class you want to generate
constants for:
[DataAnnotationValues(StringLength = true, Range = true, Required = true, Display = true)]
public partial class Product
{
[Display(Name = "Product name")]
[Required]
[StringLength(100)]
public string? Name { get; set; }
[Display(Name = "Product price")]
[Required]
[Range(0.01, 999999.99)]
public decimal Price { get; set; }
public string? Sku { get; set; }
}
2. Centralized Approach
Create a dummy class and use the DataAnnotationValuesToGenerate attribute for
each class you want to generate constants for. You can use the
DataAnnotationValuesConfiguration attribute to configure what to be generated.
using Pekspro.DataAnnotationValuesExtractor;
[DataAnnotationValuesConfiguration(StringLength = true, Range = true, Required = true, Display = true)]
[DataAnnotationValuesToGenerate(typeof(Customer))]
[DataAnnotationValuesToGenerate(typeof(Order))]
[DataAnnotationValuesToGenerate(typeof(Product))]
partial class ValidationConstants
{
}
Your model classes remain unchanged.
This approach is especially useful when working with auto-generated models, such as those created by Entity Framework scaffolding.
Use the generated constants
No matter which approach your are using, you can access generated constants like this:
// Name
int maxNameLength = Product.Annotations.Name.MaximumLength; // 100
int minNameLength = Product.Annotations.Name.MinimumLength; // 0
bool nameRequired = Product.Annotations.Name.IsRequired; // true
string? nameDisplayName = Product.Annotations.Name.Display.Name; // Product name
// Price
double minPrice = Product.Annotations.Price.Minimum; // 0.01
double maxPrice = Product.Annotations.Price.Maximum; // 999999.99
bool priceRequired = Product.Annotations.Price.IsRequired;
string? priceDisplayName = Product.Annotations.Price.Display.Name; // Price name
// Sku
bool skuRequired = Product.Annotations.Sku.IsRequired; // false
Installation
Add the package to your project:
dotnet add package Pekspro.DataAnnotationValuesExtractor
For optimal setup, configure the package reference in your .csproj to exclude
it from your output assembly:
<ItemGroup>
<PackageReference Include="Pekspro.DataAnnotationValuesExtractor" Version="0.0.1"
PrivateAssets="all" ExcludeAssets="runtime" />
</ItemGroup>
Links
You can find more information and can report issues on GitHub.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net451 is compatible. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.