Class WhenExtensions
- Namespace
- FunctionalDdd
- Assembly
- FunctionalDdd.RailwayOrientedProgramming.dll
Conditionally executes an operation based on a predicate, maintaining the railway. Useful for executing operations only when certain conditions are met without breaking the Result chain.
public static class WhenExtensions
- Inheritance
-
WhenExtensions
- Inherited Members
Examples
// Apply discount only for premium users
var result = order
.When(o => o.IsPremiumUser, o => ApplyDiscount(o))
.Bind(o => ProcessPayment(o));
// Validate only if amount exceeds threshold with cancellation support
var ct = cancellationToken;
await GetTransactionAsync(id)
.WhenAsync(
t => t.Amount > 10000,
t => PerformAdditionalValidationAsync(t, ct)
);
Remarks
Users should capture CancellationToken in their lambda closures when cancellation support is needed.
Methods
Unless<T>(Result<T>, bool, Func<T, Result<T>>)
Conditionally executes an operation if the condition is false.
public static Result<T> Unless<T>(this Result<T> result, bool condition, Func<T, Result<T>> operation)
Parameters
resultResult<T>The result to test.
conditionboolBoolean condition.
operationFunc<T, Result<T>>Operation to execute if condition is false.
Returns
- Result<T>
Result from the operation if condition is false and result is success; otherwise the original result.
Type Parameters
TType of the result value.
Unless<T>(Result<T>, Func<T, bool>, Func<T, Result<T>>)
Conditionally executes an operation if the predicate returns false.
public static Result<T> Unless<T>(this Result<T> result, Func<T, bool> predicate, Func<T, Result<T>> operation)
Parameters
resultResult<T>The result to test.
predicateFunc<T, bool>Predicate function to test the value.
operationFunc<T, Result<T>>Operation to execute if predicate is false.
Returns
- Result<T>
Result from the operation if predicate is false and result is success; otherwise the original result.
Type Parameters
TType of the result value.
When<T>(Result<T>, bool, Func<T, Result<T>>)
Conditionally executes an operation if the condition is true.
public static Result<T> When<T>(this Result<T> result, bool condition, Func<T, Result<T>> operation)
Parameters
resultResult<T>The result to test.
conditionboolBoolean condition.
operationFunc<T, Result<T>>Operation to execute if condition is true.
Returns
- Result<T>
Result from the operation if condition is true and result is success; otherwise the original result.
Type Parameters
TType of the result value.
When<T>(Result<T>, Func<T, bool>, Func<T, Result<T>>)
Conditionally executes an operation if the predicate returns true.
public static Result<T> When<T>(this Result<T> result, Func<T, bool> predicate, Func<T, Result<T>> operation)
Parameters
resultResult<T>The result to test.
predicateFunc<T, bool>Predicate function to test the value.
operationFunc<T, Result<T>>Operation to execute if predicate is true.
Returns
- Result<T>
Result from the operation if predicate is true and result is success; otherwise the original result.
Type Parameters
TType of the result value.