Class ResultFilterExtensions
- Namespace
- REslava.Result.Extensions
- Assembly
- REslava.Result.dll
Extension methods for filtering Result<TValue> by a predicate on the success value.
public static class ResultFilterExtensions
- Inheritance
-
ResultFilterExtensions
- Inherited Members
Methods
FilterAsync<T>(Result<T>, Func<T, Task<bool>>, Func<T, IError>, CancellationToken)
Asynchronously filters a result by an async predicate on the success value.
Converts a successful result to a failure when predicate returns false.
If the result is already a failure, it is returned unchanged.
public static Task<Result<T>> FilterAsync<T>(this Result<T> result, Func<T, Task<bool>> predicate, Func<T, IError> errorFactory, CancellationToken cancellationToken = default)
Parameters
resultResult<T>The result to filter.
predicateFunc<T, Task<bool>>The async condition the value must satisfy.
errorFactoryFunc<T, IError>Produces the error when the predicate fails; receives the value.
cancellationTokenCancellationTokenOptional cancellation token.
Returns
Type Parameters
TThe result value type.
Examples
Result<Order> valid = await orderResult
.FilterAsync(async o => await validator.IsValidAsync(o),
o => new ValidationError("Order", o.Id.ToString(), "failed validation"));
FilterAsync<T>(Task<Result<T>>, Func<T, Task<bool>>, Func<T, IError>, CancellationToken)
Awaits the result task and filters the result by an async predicate on the success value.
public static Task<Result<T>> FilterAsync<T>(this Task<Result<T>> resultTask, Func<T, Task<bool>> predicate, Func<T, IError> errorFactory, CancellationToken cancellationToken = default)
Parameters
resultTaskTask<Result<T>>predicateFunc<T, Task<bool>>errorFactoryFunc<T, IError>cancellationTokenCancellationToken
Returns
Type Parameters
T
Filter<T>(Result<T>, Func<T, bool>, IError)
Converts a successful result to a failure when predicate returns false.
If the result is already a failure, it is returned unchanged.
public static Result<T> Filter<T>(this Result<T> result, Func<T, bool> predicate, IError error)
Parameters
resultResult<T>The result to filter.
predicateFunc<T, bool>The condition the value must satisfy.
errorIErrorThe error to use when the predicate fails.
Returns
- Result<T>
Type Parameters
T
Filter<T>(Result<T>, Func<T, bool>, Func<T, IError>)
Converts a successful result to a failure when predicate returns false.
The error is built from the value, enabling contextual error messages.
If the result is already a failure, it is returned unchanged.
public static Result<T> Filter<T>(this Result<T> result, Func<T, bool> predicate, Func<T, IError> errorFactory)
Parameters
resultResult<T>The result to filter.
predicateFunc<T, bool>The condition the value must satisfy.
errorFactoryFunc<T, IError>Produces the error when the predicate fails; receives the value.
Returns
- Result<T>
The original result if successful and predicate passes; otherwise a failed result.
Type Parameters
TThe result value type.
Examples
Result<User> activeUser = userResult
.Filter(u => u.IsActive, u => new Error($"User '{u.Name}' is not active."));
Filter<T>(Result<T>, Func<T, bool>, string)
Converts a successful result to a failure when predicate returns false.
If the result is already a failure, it is returned unchanged.
public static Result<T> Filter<T>(this Result<T> result, Func<T, bool> predicate, string errorMessage)
Parameters
resultResult<T>The result to filter.
predicateFunc<T, bool>The condition the value must satisfy.
errorMessagestringThe error message to use when the predicate fails.
Returns
- Result<T>
Type Parameters
T
Filter<T>(Task<Result<T>>, Func<T, bool>, IError, CancellationToken)
Awaits the result task and filters the result by a predicate on the success value.
public static Task<Result<T>> Filter<T>(this Task<Result<T>> resultTask, Func<T, bool> predicate, IError error, CancellationToken cancellationToken = default)
Parameters
Returns
Type Parameters
T
Filter<T>(Task<Result<T>>, Func<T, bool>, Func<T, IError>, CancellationToken)
Awaits the result task and filters the result by a predicate on the success value.
public static Task<Result<T>> Filter<T>(this Task<Result<T>> resultTask, Func<T, bool> predicate, Func<T, IError> errorFactory, CancellationToken cancellationToken = default)
Parameters
resultTaskTask<Result<T>>predicateFunc<T, bool>errorFactoryFunc<T, IError>cancellationTokenCancellationToken
Returns
Type Parameters
T