Table of Contents

Class EnsureExtensions

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Provides extension methods for ensuring conditions on Result values. If conditions fail, the result is converted to a failure with the specified error.

public static class EnsureExtensions
Inheritance
EnsureExtensions
Inherited Members

Remarks

Ensure methods allow you to validate result values against conditions and convert successful results to failures if the validation fails. This is useful for enforcing business rules and constraints in a functional pipeline without breaking the railway pattern.

Methods

Ensure(bool, Error)

Returns a success result if the flag is true; otherwise returns a failure with the specified error.

public static Result<Unit> Ensure(bool flag, Error error)

Parameters

flag bool

The boolean flag to test.

error Error

The error to return if the flag is false.

Returns

Result<Unit>

A success result if flag is true; otherwise a failure with the specified error.

EnsureNotNullOrWhiteSpace(string?, Error)

Ensures a string is not null or whitespace.

public static Result<string> EnsureNotNullOrWhiteSpace(this string? str, Error error)

Parameters

str string

The string to validate.

error Error

The error to return if the string is null or whitespace.

Returns

Result<string>

A success result with the string if valid; otherwise a failure with the specified error.

Ensure<TValue>(Result<TValue>, Func<Result<TValue>>)

Returns a new failure result if the predicate result is a failure. Otherwise returns the starting result.

public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<Result<TValue>> predicate)

Parameters

result Result<TValue>

The result to validate.

predicate Func<Result<TValue>>

The predicate function that returns a Result.

Returns

Result<TValue>

The original result if both it and the predicate result are successes; otherwise a failure.

Type Parameters

TValue

Type of the result value.

Ensure<TValue>(Result<TValue>, Func<bool>, Error)

Returns a new failure result if the predicate is false. Otherwise returns the starting result.

public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<bool> predicate, Error error)

Parameters

result Result<TValue>

The result to validate.

predicate Func<bool>

The predicate to test.

error Error

The error to return if the predicate is false.

Returns

Result<TValue>

The original result if success and predicate is true; otherwise a failure with the specified error.

Type Parameters

TValue

Type of the result value.

Ensure<TValue>(Result<TValue>, Func<TValue, Result<TValue>>)

Returns a new failure result if the predicate result is a failure. Otherwise returns the starting result. The predicate receives the value.

public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<TValue, Result<TValue>> predicate)

Parameters

result Result<TValue>

The result to validate.

predicate Func<TValue, Result<TValue>>

The predicate function that receives the value and returns a Result.

Returns

Result<TValue>

The original result if both it and the predicate result are successes; otherwise a failure.

Type Parameters

TValue

Type of the result value.

Ensure<TValue>(Result<TValue>, Func<TValue, bool>, Error)

Returns a new failure result if the predicate is false. Otherwise returns the starting result.

public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<TValue, bool> predicate, Error error)

Parameters

result Result<TValue>

The result to validate.

predicate Func<TValue, bool>

The predicate function to test the value.

error Error

The error to return if the predicate is false.

Returns

Result<TValue>

The original result if success and predicate is true; otherwise a failure with the specified error.

Type Parameters

TValue

Type of the result value.

Ensure<TValue>(Result<TValue>, Func<TValue, bool>, Func<TValue, Error>)

Returns a new failure result if the predicate is false. Otherwise returns the starting result. The error is generated by a function that receives the value.

public static Result<TValue> Ensure<TValue>(this Result<TValue> result, Func<TValue, bool> predicate, Func<TValue, Error> errorPredicate)

Parameters

result Result<TValue>

The result to validate.

predicate Func<TValue, bool>

The predicate function to test the value.

errorPredicate Func<TValue, Error>

The function that generates an error from the value if the predicate is false.

Returns

Result<TValue>

The original result if success and predicate is true; otherwise a failure with an error from the error function.

Type Parameters

TValue

Type of the result value.