Class Age
- Namespace
- Trellis.Primitives
- Assembly
- Trellis.Primitives.dll
Age value object with validation for ages 0-150.
[JsonConverter(typeof(ParsableJsonConverter<Age>))]
public class Age : ScalarValueObject<Age, int>, IComparable<ValueObject>, IComparable, IEquatable<ValueObject>, IConvertible, IFormattable, IFormattableScalarValue<Age, int>, IScalarValue<Age, int>, IParsable<Age>
- Inheritance
-
Age
- Implements
- Inherited Members
- Extension Methods
Remarks
Validation Rules (Opinionated):
- Must be non-negative (>= 0)
- Must be realistic (<= 150)
If these rules don't fit your domain, create your own Age value object using the ScalarValueObject<TSelf, T> base class from the DomainDrivenDesign package.
Methods
Parse(string?, IFormatProvider?)
Parses an age.
public static Age Parse(string? s, IFormatProvider? provider)
Parameters
sstringproviderIFormatProvider
Returns
TryCreate(int, string?)
Attempts to create an age.
If fieldName is not provided, validation errors use "age" as the field name.
public static Result<Age> TryCreate(int value, string? fieldName = null)
Parameters
valueintThe integer age value to validate.
fieldNamestringOptional field name for validation error messages. If not provided, defaults to "age".
Returns
- Result<Age>
Success with the Age if valid; Failure with Error.InvalidInput otherwise.
TryCreate(string?, IFormatProvider?, string?)
Attempts to create an Age from a string using the specified format provider.
If fieldName is not provided, validation errors use "age" as the field name.
public static Result<Age> TryCreate(string? value, IFormatProvider? provider, string? fieldName = null)
Parameters
valuestringThe string value to parse.
providerIFormatProviderThe format provider for culture-sensitive parsing. Defaults to InvariantCulture when null.
fieldNamestringOptional field name for validation error messages. If not provided, defaults to "age".
Returns
- Result<Age>
Success with the Age if valid; Failure with Error.InvalidInput otherwise.
TryCreate(string?, string?)
Attempts to create an Age from a string representation.
If fieldName is not provided, validation errors use "age" as the field name.
public static Result<Age> TryCreate(string? value, string? fieldName = null)
Parameters
valuestringThe string value to parse.
fieldNamestringOptional field name for validation error messages. If not provided, defaults to "age".
Returns
- Result<Age>
Success with the Age if valid; Failure with Error.InvalidInput otherwise.
Remarks
Delegates to the TryCreate(string?, IFormatProvider?, string?) overload using the invariant culture.
TryParse(string?, IFormatProvider?, out Age)
Tries to parse an age.
public static bool TryParse(string? s, IFormatProvider? provider, out Age result)
Parameters
sstringproviderIFormatProviderresultAge