Skip to content

Validate without exceptions? #29

@Bitfiddler

Description

@Bitfiddler

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions