Table of Contents

Class CheckBase<T>

Namespace
Utils.Objects
Assembly
Utils.dll

Provides a base implementation for value validation helpers that collect errors before throwing them.

public abstract class CheckBase<T>

Type Parameters

T

Type of the value being validated.

Inheritance
CheckBase<T>
Derived
Inherited Members
Extension Methods

Constructors

CheckBase(T, string)

Initializes a new instance of the CheckBase<T> class.

public CheckBase(T value, string name)

Parameters

value T

Value to validate.

name string

Name of the value for diagnostic messages.

Properties

Errors

Gets the collection of validation errors accumulated during the checks.

protected List<Exception> Errors { get; }

Property Value

List<Exception>

Name

Gets the human-readable name of the value being validated.

public string Name { get; }

Property Value

string

Value

Gets the validated value, throwing accumulated validation errors if any were recorded.

public T Value { get; }

Property Value

T

Methods

Must(params Func<T, Exception>[])

Executes custom validation checks and records produced exceptions.

public CheckBase<T> Must(params Func<T, Exception>[] checks)

Parameters

checks Func<T, Exception>[]

Set of functions that return an exception when the value is invalid, or null when the value is valid. Each delegate is invoked even when earlier checks have already recorded errors, so that all independent failures are accumulated (#63).

Returns

CheckBase<T>

The current validation instance for fluent chaining.

Exceptions

ArgumentNullException

Thrown when checks is null.

ArgumentException

Thrown when any element in checks is null.

MustNotBeNull()

Adds a validation rule ensuring the value is not null.

public CheckBase<T> MustNotBeNull()

Returns

CheckBase<T>

The current validation instance for fluent chaining.

OnError(Exception)

Adds a validation error to the internal collection.

protected virtual void OnError(Exception exception)

Parameters

exception Exception

Exception describing the validation failure.

OnNull()

Handles the null-case for the value.

protected abstract void OnNull()

ThrowErrors()

Throws the accumulated validation errors, if any were recorded.

public virtual void ThrowErrors()

Remarks

When exactly one error exists it is rethrown directly so callers can catch its specific type. When multiple errors exist they are wrapped in an AggregateException so that individual causes and stack traces remain accessible to callers (#64).

Exceptions

AggregateException

Thrown with all accumulated validation errors when more than one error was recorded.

Operators

implicit operator T(CheckBase<T>)

Retrieves the validated value.

public static implicit operator T(CheckBase<T> checkBase)

Parameters

checkBase CheckBase<T>

Validation instance whose value should be returned.

Returns

T