Class RequiredDateTime<TSelf>
- Namespace
- Trellis
- Assembly
- Trellis.Primitives.dll
Base class for creating strongly-typed DateTime value objects that prevent primitive obsession for dates. Rejects MinValue as the "empty" equivalent.
public abstract class RequiredDateTime<TSelf> : ScalarValueObject<TSelf, DateTime>, IComparable<ValueObject>, IComparable, IEquatable<ValueObject>, IConvertible, IFormattable where TSelf : RequiredDateTime<TSelf>, IScalarValue<TSelf, DateTime>
Type Parameters
TSelf
- Inheritance
-
ScalarValueObject<TSelf, DateTime>RequiredDateTime<TSelf>
- Implements
- Inherited Members
- Extension Methods
Examples
Creating a strongly-typed DateTime value object:
public partial class OrderDate : RequiredDateTime<OrderDate> { }
var result = OrderDate.TryCreate(DateTime.UtcNow);
var fromString = OrderDate.TryCreate("2026-01-15T12:00:00Z");
var invalid = OrderDate.TryCreate(DateTime.MinValue); // Failure
Remarks
This class extends ScalarValueObject<TSelf, T> to provide a specialized base for DateTime-based value objects
with automatic validation that prevents default/MinValue dates. When used with the partial keyword,
the PrimitiveValueObjectGenerator source generator automatically creates:
IScalarValue<TSelf, DateTime>implementation for ASP.NET Core automatic validationTryCreate(DateTime)- Factory method for DateTimes (required by IScalarValue)TryCreate(DateTime?, string?)- Factory method with null/MinValue validation and custom field nameTryCreate(string?, string?)- Factory method for parsing strings with validationIParsable<T>implementation (Parse,TryParse)- JSON serialization support via
ParsableJsonConverter<T> - Explicit cast operator from DateTime
- OpenTelemetry activity tracing
Common use cases:
- Order dates (OrderDate, ShipDate)
- Personal dates (BirthDate, HireDate)
- Scheduling (DueDate, StartDate, EndDate)
- Any domain concept requiring a non-default DateTime
Constructors
RequiredDateTime(DateTime)
Initializes a new instance of the RequiredDateTime<TSelf> class with the specified DateTime value.
protected RequiredDateTime(DateTime value)
Parameters
Methods
ToString()
Returns the DateTime in ISO 8601 round-trip format ("O") using invariant culture. This ensures JSON serialization via ParsableJsonConverter is deterministic, culture-independent, and preserves DateTimeKind (UTC/Local/Unspecified).
public override string ToString()