I'm new to ValueObjects so forgive me if this is a bad idea, but I wanted to avoid throwing exceptions during validation for a variety of reasons and instead have a collection of validation errors. So I added:
private readonly List<TValidationResult> _errors;
and later:
public List<TValidationResult> Errors => _errors ?? new();
public IReadOnlyList<TValidationResult> ValidationErrors => _errors.AsReadOnly();
To the ValueOf base class. This allows the ValueOf consumer to decide whether Validate should throw exceptions, or TryValidate should just fail silently; or whether both should append one or more validation errors to a collection that can be displayed to a user or logged.
Is there a reason I'm unaware of as to why throwing exceptions seems to be the preferred validation mechanism?
I'm new to ValueObjects so forgive me if this is a bad idea, but I wanted to avoid throwing exceptions during validation for a variety of reasons and instead have a collection of validation errors. So I added:
private readonly List<TValidationResult> _errors;and later:
To the ValueOf base class. This allows the ValueOf consumer to decide whether Validate should throw exceptions, or TryValidate should just fail silently; or whether both should append one or more validation errors to a collection that can be displayed to a user or logged.
Is there a reason I'm unaware of as to why throwing exceptions seems to be the preferred validation mechanism?