Class RequiredDateTime<TSelf>
- Namespace
- Trellis
- Assembly
- Trellis.Core.dll
Base class for creating strongly-typed DateTime value objects that prevent primitive
obsession for dates. Rejects only null by default; MinValue
rejection is opt-in via the NotDefaultAttribute attribute. Recommended for
any DateTime type used as an EF-mapped property to preserve the database invariant
guarantee enforced by TrellisScalarConverter on rehydration.
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
Lenient default — only null is rejected:
public partial class EventTimestamp : RequiredDateTime<EventTimestamp> { }
var ok = EventTimestamp.TryCreate(DateTime.UtcNow); // Success
var min = EventTimestamp.TryCreate(DateTime.MinValue); // Success (lenient)
var nul = EventTimestamp.TryCreate((DateTime?)null); // Failure
Strict opt-in — MinValue rejected:
[NotDefault]
public partial class OrderDate : RequiredDateTime<OrderDate> { }
var ok = OrderDate.TryCreate(DateTime.UtcNow); // Success
var min = OrderDate.TryCreate(DateTime.MinValue);
// Failure: "Order Date cannot be DateTime.MinValue."
Remarks
This class extends ScalarValueObject<TSelf, T> to provide a specialized base
for DateTime-based value objects. 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 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
Validation rules emitted by the source generator:
nullis always rejected with the per-type "cannot be empty." message.- Apply NotDefaultAttribute to additionally reject MinValue with the per-type "cannot be DateTime.MinValue." message. Recommended for any DateTime used as an EF-mapped property to preserve the database invariant guarantee enforced by
TrellisScalarConverteron rehydration.
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
valueDateTimeThe DateTime value.
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()