Table of Contents

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

result Result<T>

The result to test.

condition bool

Boolean condition.

operation Func<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

T

Type 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

result Result<T>

The result to test.

predicate Func<T, bool>

Predicate function to test the value.

operation Func<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

T

Type 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

result Result<T>

The result to test.

condition bool

Boolean condition.

operation Func<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

T

Type 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

result Result<T>

The result to test.

predicate Func<T, bool>

Predicate function to test the value.

operation Func<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

T

Type of the result value.