Table of Contents

Class Result

Namespace
Trellis
Assembly
Trellis.Core.dll

Static Combine methods for combining multiple independent Result values without chaining (3-9 tuples).

public static class Result
Inheritance
Result
Inherited Members

Remarks

Use Result.Combine(r1, r2, r3, ...) when you already have individual Result variables and want to combine them before error checking. Delegates to the chaining extension methods.

Methods

Combine<T1, T2>(Result<T1>, Result<T2>)

Combines two independent Result<TValue> instances into a single tuple result.

public static Result<(T1, T2)> Combine<T1, T2>(Result<T1> r1, Result<T2> r2)

Parameters

r1 Result<T1>

First result.

r2 Result<T2>

Second result.

Returns

Result<(T1, T2)>

A success result with a 2-element tuple if both succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

Examples

var emailResult = EmailAddress.TryCreate(dto.Email);
var nameResult = FirstName.TryCreate(dto.Name);

var result = Result.Combine(emailResult, nameResult)
    .Bind((email, name) => User.Create(email, name));

Combine<T1, T2, T3>(Result<T1>, Result<T2>, Result<T3>)

Combines 3 independent Result<TValue> instances into a single 3-element tuple result.

public static Result<(T1, T2, T3)> Combine<T1, T2, T3>(Result<T1> r1, Result<T2> r2, Result<T3> r3)

Parameters

r1 Result<T1>

The first result.

r2 Result<T2>

The second result.

r3 Result<T3>

The third result.

Returns

Result<(T1, T2, T3)>

A success result with a 3-element tuple if all succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

T3

Type of the third result value.

Examples

var result = Result.Combine(r1, r2, r3)
    .Bind((v1, v2, v3) => Process(v1, v2, v3));

Combine<T1, T2, T3, T4>(Result<T1>, Result<T2>, Result<T3>, Result<T4>)

Combines 4 independent Result<TValue> instances into a single 4-element tuple result.

public static Result<(T1, T2, T3, T4)> Combine<T1, T2, T3, T4>(Result<T1> r1, Result<T2> r2, Result<T3> r3, Result<T4> r4)

Parameters

r1 Result<T1>

The first result.

r2 Result<T2>

The second result.

r3 Result<T3>

The third result.

r4 Result<T4>

The fourth result.

Returns

Result<(T1, T2, T3, T4)>

A success result with a 4-element tuple if all succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

T3

Type of the third result value.

T4

Type of the fourth result value.

Examples

var result = Result.Combine(r1, r2, r3, r4)
    .Bind((v1, v2, v3, v4) => Process(v1, v2, v3, v4));

Combine<T1, T2, T3, T4, T5>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)

Combines 5 independent Result<TValue> instances into a single 5-element tuple result.

public static Result<(T1, T2, T3, T4, T5)> Combine<T1, T2, T3, T4, T5>(Result<T1> r1, Result<T2> r2, Result<T3> r3, Result<T4> r4, Result<T5> r5)

Parameters

r1 Result<T1>

The first result.

r2 Result<T2>

The second result.

r3 Result<T3>

The third result.

r4 Result<T4>

The fourth result.

r5 Result<T5>

The fifth result.

Returns

Result<(T1, T2, T3, T4, T5)>

A success result with a 5-element tuple if all succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

T3

Type of the third result value.

T4

Type of the fourth result value.

T5

Type of the fifth result value.

Examples

var result = Result.Combine(r1, r2, r3, r4, r5)
    .Bind((v1, v2, v3, v4, v5) => Process(v1, v2, v3, v4, v5));

Combine<T1, T2, T3, T4, T5, T6>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>, Result<T6>)

Combines 6 independent Result<TValue> instances into a single 6-element tuple result.

public static Result<(T1, T2, T3, T4, T5, T6)> Combine<T1, T2, T3, T4, T5, T6>(Result<T1> r1, Result<T2> r2, Result<T3> r3, Result<T4> r4, Result<T5> r5, Result<T6> r6)

Parameters

r1 Result<T1>

The first result.

r2 Result<T2>

The second result.

r3 Result<T3>

The third result.

r4 Result<T4>

The fourth result.

r5 Result<T5>

The fifth result.

r6 Result<T6>

The sixth result.

Returns

Result<(T1, T2, T3, T4, T5, T6)>

A success result with a 6-element tuple if all succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

T3

Type of the third result value.

T4

Type of the fourth result value.

T5

Type of the fifth result value.

T6

Type of the sixth result value.

Examples

var result = Result.Combine(r1, r2, r3, r4, r5, r6)
    .Bind((v1, v2, v3, v4, v5, v6) => Process(v1, v2, v3, v4, v5, v6));

Combine<T1, T2, T3, T4, T5, T6, T7>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>, Result<T6>, Result<T7>)

Combines 7 independent Result<TValue> instances into a single 7-element tuple result.

public static Result<(T1, T2, T3, T4, T5, T6, T7)> Combine<T1, T2, T3, T4, T5, T6, T7>(Result<T1> r1, Result<T2> r2, Result<T3> r3, Result<T4> r4, Result<T5> r5, Result<T6> r6, Result<T7> r7)

Parameters

r1 Result<T1>

The first result.

r2 Result<T2>

The second result.

r3 Result<T3>

The third result.

r4 Result<T4>

The fourth result.

r5 Result<T5>

The fifth result.

r6 Result<T6>

The sixth result.

r7 Result<T7>

The seventh result.

Returns

Result<(T1, T2, T3, T4, T5, T6, T7)>

A success result with a 7-element tuple if all succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

T3

Type of the third result value.

T4

Type of the fourth result value.

T5

Type of the fifth result value.

T6

Type of the sixth result value.

T7

Type of the seventh result value.

Examples

var result = Result.Combine(r1, r2, r3, r4, r5, r6, r7)
    .Bind((v1, v2, v3, v4, v5, v6, v7) => Process(v1, v2, v3, v4, v5, v6, v7));

Combine<T1, T2, T3, T4, T5, T6, T7, T8>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>, Result<T6>, Result<T7>, Result<T8>)

Combines 8 independent Result<TValue> instances into a single 8-element tuple result.

public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Combine<T1, T2, T3, T4, T5, T6, T7, T8>(Result<T1> r1, Result<T2> r2, Result<T3> r3, Result<T4> r4, Result<T5> r5, Result<T6> r6, Result<T7> r7, Result<T8> r8)

Parameters

r1 Result<T1>

The first result.

r2 Result<T2>

The second result.

r3 Result<T3>

The third result.

r4 Result<T4>

The fourth result.

r5 Result<T5>

The fifth result.

r6 Result<T6>

The sixth result.

r7 Result<T7>

The seventh result.

r8 Result<T8>

The eighth result.

Returns

Result<(T1, T2, T3, T4, T5, T6, T7, T8)>

A success result with a 8-element tuple if all succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

T3

Type of the third result value.

T4

Type of the fourth result value.

T5

Type of the fifth result value.

T6

Type of the sixth result value.

T7

Type of the seventh result value.

T8

Type of the eighth result value.

Examples

var result = Result.Combine(r1, r2, r3, r4, r5, r6, r7, r8)
    .Bind((v1, v2, v3, v4, v5, v6, v7, v8) => Process(v1, v2, v3, v4, v5, v6, v7, v8));

Combine<T1, T2, T3, T4, T5, T6, T7, T8, T9>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>, Result<T6>, Result<T7>, Result<T8>, Result<T9>)

Combines 9 independent Result<TValue> instances into a single 9-element tuple result.

public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Combine<T1, T2, T3, T4, T5, T6, T7, T8, T9>(Result<T1> r1, Result<T2> r2, Result<T3> r3, Result<T4> r4, Result<T5> r5, Result<T6> r6, Result<T7> r7, Result<T8> r8, Result<T9> r9)

Parameters

r1 Result<T1>

The first result.

r2 Result<T2>

The second result.

r3 Result<T3>

The third result.

r4 Result<T4>

The fourth result.

r5 Result<T5>

The fifth result.

r6 Result<T6>

The sixth result.

r7 Result<T7>

The seventh result.

r8 Result<T8>

The eighth result.

r9 Result<T9>

The ninth result.

Returns

Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>

A success result with a 9-element tuple if all succeed; otherwise a failure with combined errors.

Type Parameters

T1

Type of the first result value.

T2

Type of the second result value.

T3

Type of the third result value.

T4

Type of the fourth result value.

T5

Type of the fifth result value.

T6

Type of the sixth result value.

T7

Type of the seventh result value.

T8

Type of the eighth result value.

T9

Type of the ninth result value.

Examples

var result = Result.Combine(r1, r2, r3, r4, r5, r6, r7, r8, r9)
    .Bind((v1, v2, v3, v4, v5, v6, v7, v8, v9) => Process(v1, v2, v3, v4, v5, v6, v7, v8, v9));

Ensure(bool, Func<Error>)

Returns success when the condition is true, creating the error only when it is false.

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

Parameters

flag bool

The condition to assert.

errorFactory Func<Error>

Creates the failure error; invoked exactly once on failure and never on success.

Returns

Result<Unit>

A successful no-payload result or a failure containing the factory's error.

Remarks

The factory is required even when the condition passes. Factory exceptions propagate. The eager overload has higher overload-resolution priority to preserve existing null-literal calls.

Exceptions

ArgumentNullException

The factory is null, or an invoked factory returns null.

Ensure(bool, Error)

Returns a successful no-payload result if flag is true; otherwise a failure with the specified error.

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

Parameters

flag bool

The condition to assert. true yields success; false yields failure.

error Error

The error to wrap when flag is false. Always evaluated by the caller.

Returns

Result<Unit>

Ok() when flag is true; otherwise Fail(Error).

Examples

Authorization guard inside IAuthorizeResource<TResource>.Authorize:

public IResult Authorize(Actor actor, Order resource) =>
    Result.Ensure(
        resource.OwnerId == actor.UserId,
        new Error.Forbidden("order.not-owned"));

Domain invariant guard inside an aggregate method (replaces a hand-written if/return):

public Result<Unit> Cancel(DateTimeOffset now) =>
    Result.Ensure(Status == OrderStatus.Pending,
        new Error.Conflict(Resource: null, Code: "order.not-cancellable") { Detail = "Only pending orders can be cancelled." })
    .Tap(() => { Status = OrderStatus.Cancelled; CancelledAt = now; });

Remarks

Canonical guard primitive. Prefer Result.Ensure(condition, error) over hand-written if (!condition) return Result.Fail(error); blocks or ad-hoc ternaries — Ensure reads as a single declarative line, participates in Activity tracing, and is the preferred form for guard clauses in current guidance.

To construct an error only when the condition fails, use Ensure(bool, Func<Error>) with an error factory.

For an asynchronous predicate use EnsureAsync(Func<Task<bool>>, Error). For a value-threaded guard that preserves an existing Result<TValue> on success, use the Result<TValue>.Ensure(predicate, error) extension overloads on EnsureExtensions.

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

Evaluates a predicate once and creates an error only when it returns false.

public static Result<Unit> Ensure(Func<bool> predicate, Func<Error> errorFactory)

Parameters

predicate Func<bool>

The condition to evaluate.

errorFactory Func<Error>

Creates the failure error after a false predicate result.

Returns

Result<Unit>

A successful no-payload result or a failure containing the factory's error.

Remarks

Both delegates are validated before the predicate is invoked. A passing or throwing predicate never invokes the factory. Delegate exceptions propagate.

Exceptions

ArgumentNullException

A delegate is null, or an invoked factory returns null.

Ensure(Func<bool>, Error)

Returns a successful no-payload result if the predicate is true; otherwise a failure with the specified error.

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

Parameters

predicate Func<bool>
error Error

Returns

Result<Unit>

EnsureAsync(Func<Task<bool>>, Func<Error>)

Awaits a predicate once and creates an error only when it completes with false.

public static Task<Result<Unit>> EnsureAsync(Func<Task<bool>> predicate, Func<Error> errorFactory)

Parameters

predicate Func<Task<bool>>

The asynchronous condition to evaluate.

errorFactory Func<Error>

Synchronously creates the error after a false predicate result.

Returns

Task<Result<Unit>>

A task containing a successful no-payload result or the factory's failure.

Remarks

Both delegates are validated before the predicate is invoked. A passing, faulted, or cancelled predicate never invokes the factory. Exceptions and cancellation propagate through the returned task; no errors are created for them.

Exceptions

ArgumentNullException

A delegate is null, or an invoked factory returns null.

EnsureAsync(Func<Task<bool>>, Error)

Asynchronously evaluates the predicate and returns success if true; otherwise a failure with the specified error.

public static Task<Result<Unit>> EnsureAsync(Func<Task<bool>> predicate, Error error)

Parameters

predicate Func<Task<bool>>
error Error

Returns

Task<Result<Unit>>

EnsureNotNull<T>(T?, Func<Error>)

Guards a required nullable struct, creating an error exactly once only when it is null.

public static Result<T> EnsureNotNull<T>(T? value, Func<Error> errorFactory) where T : struct

Parameters

value T?

The possibly missing value.

errorFactory Func<Error>

Creates the error only for a missing value.

Returns

Result<T>

A result carrying the unwrapped value or the created error.

Type Parameters

T

The value type carried by success.

Remarks

The factory is required even on success. Factory exceptions propagate.

Exceptions

ArgumentNullException

The factory is null, or an invoked factory returns null.

EnsureNotNull<T>(T?, string?, string?)

Guards a required nullable struct, creating a standard required-field error only when it is null.

public static Result<T> EnsureNotNull<T>(T? value, string? fieldName, string? detail = null) where T : struct

Parameters

value T?

The possibly missing value.

fieldName string

Property name or full JSON Pointer; null or empty targets the root.

detail string

Optional human-readable violation detail.

Returns

Result<T>

The unwrapped value or an Error.InvalidInput containing one required-field violation.

Type Parameters

T

The value type carried by success.

Exceptions

ArgumentException

The value is null and the field name is a malformed JSON Pointer.

EnsureNotNull<T>(T?, Error)

Guards a required nullable struct, unwrapping its value or returning the supplied error.

public static Result<T> EnsureNotNull<T>(T? value, Error error) where T : struct

Parameters

value T?

The possibly missing value.

error Error

The failure error, used only when the value is null.

Returns

Result<T>

A result carrying the unwrapped value or the error.

Type Parameters

T

The value type carried by success.

Exceptions

ArgumentNullException

The value and error are both null.

EnsureNotNull<T>(T?, Func<Error>)

Guards a required reference, creating an error exactly once only when the value is null.

public static Result<T> EnsureNotNull<T>(T? value, Func<Error> errorFactory) where T : class

Parameters

value T

The possibly missing value.

errorFactory Func<Error>

Creates the error only for a missing value.

Returns

Result<T>

A result carrying the same non-null value or the created error.

Type Parameters

T

The reference type carried by success.

Remarks

The factory is required even on success. Factory exceptions propagate.

Exceptions

ArgumentNullException

The factory is null, or an invoked factory returns null.

EnsureNotNull<T>(T?, string?, string?)

Guards a required reference, creating a standard required-field error only when it is null.

public static Result<T> EnsureNotNull<T>(T? value, string? fieldName, string? detail = null) where T : class

Parameters

value T

The possibly missing value.

fieldName string

Property name or full JSON Pointer; null or empty targets the root.

detail string

Optional human-readable violation detail.

Returns

Result<T>

The same non-null value or an Error.InvalidInput containing one required-field violation.

Type Parameters

T

The reference type carried by success.

Exceptions

ArgumentException

The value is null and the field name is a malformed JSON Pointer.

EnsureNotNull<T>(T?, Error)

Guards a required reference, returning the same non-null value or the supplied error.

public static Result<T> EnsureNotNull<T>(T? value, Error error) where T : class

Parameters

value T

The possibly missing value.

error Error

The failure error, used only when the value is null.

Returns

Result<T>

A result carrying the non-null value or the error.

Type Parameters

T

The reference type carried by success.

Exceptions

ArgumentNullException

The value and error are both null.

Fail(Error)

Creates a failed no-payload result (Result<Unit>) with the specified error.

public static Result<Unit> Fail(Error error)

Parameters

error Error

The error describing the failure.

Returns

Result<Unit>

A failed Result<TValue> containing error.

Exceptions

ArgumentNullException

Thrown when error is null.

FailAfterCommit(Error)

Creates a failed no-payload result that opts in to post-handler persistence. See FailAfterCommit<TValue>(Error) for the full semantics.

public static Result<Unit> FailAfterCommit(Error error)

Parameters

error Error

The error describing the failure.

Returns

Result<Unit>

A failed Result<TValue> whose PersistOnFailure flag is true.

Exceptions

ArgumentNullException

Thrown when error is null.

FailAfterCommit<TValue>(Error)

Creates a failed result that opts in to post-handler persistence: pipeline behaviors implementing the IPersistOnFailure contract (notably TransactionalCommandBehavior) will still commit staged changes alongside this failure, so the failure state is durably recorded.

public static Result<TValue> FailAfterCommit<TValue>(Error error)

Parameters

error Error

The error describing the failure.

Returns

Result<TValue>

A failed Result<TValue> whose PersistOnFailure flag is true.

Type Parameters

TValue

Success value type for the result envelope.

Remarks

Worker-handler pattern. The canonical use case is a background-worker handler that converts a transient external-service rejection into a persisted permanently_failed state on the corresponding aggregate. The handler updates the aggregate via the change tracker, then returns Result.FailAfterCommit<TResponse>(error). TransactionalCommandBehavior observes the per-instance flag and commits the staged row even though the result is a failure. The caller still receives the original Error — the result is still a failure.

Commit-failure semantics. If the commit itself fails, the commit error is returned (overwriting the original handler error). This is intentional — the caller must learn that the persist-on-failure guarantee was not honored.

Domain-event dispatch. DomainEventDispatchBehavior does not fan out events on a failed result, including persist-on-failure ones. Any events the handler raised on aggregates remain on those in-memory aggregate instances and are discarded with them when the request scope ends — they are not a durable retry buffer. If your scenario requires post-failure side effects (notifications, downstream commands), model them explicitly via an outbox row or a dedicated follow-up command instead of relying on event re-dispatch.

Operator propagation. The persist-on-failure flag is preserved by every railway operator that projects a failure (Map, Bind, MapOnFailure, Check, BindZip, Traverse, Ensure upstream-propagation). Aggregating operators (Combine, TraverseAll, SequenceAll, WhenAllAsync) OR-accumulate the flag: if any failing source opted in, the aggregated failure carries the flag. The flag is sticky once introduced — a later validation failure combined with an upstream FailAfterCommit cannot suppress the persist intent. Fresh failures created without an upstream source (e.g., Ensure evaluating false on a previously successful value, EnsureNotNull's value-was-null branch) do not set the flag.

Do not compose FailAfterCommit with aggregating operators. FailAfterCommit is a leaf worker-handler operation: it converts a single aggregate's transient external rejection into a persisted permanently_failed state and returns. Threading that result through Combine, TraverseAll, SequenceAll, or WhenAllAsync — composing it with the outcomes of other, independent operations — is outside the supported pattern. The OR-accumulated flag will then commit the staged permanent-failure mutation alongside whatever the other legs produced, which is unlikely to match the handler author's intent. Restructure such handlers so the aggregating step runs to its terminal outcome first and FailAfterCommit is invoked at the end (or in a follow-up command), never as a leg inside a multi-aggregate composition.

Exceptions

ArgumentNullException

Thrown when error is null.

Fail<TValue>(Error)

Creates a failed result with the specified error.

public static Result<TValue> Fail<TValue>(Error error)

Parameters

error Error

The error describing the failure.

Returns

Result<TValue>

A failed Result<TValue> containing error.

Type Parameters

TValue

The success value type for the failed result envelope.

Exceptions

ArgumentNullException

Thrown when error is null.

Ok()

Creates a successful no-payload result (Result<Unit>).

public static Result<Unit> Ok()

Returns

Result<Unit>

A successful Result<TValue> containing Default.

Remarks

Goes through the constructor so that Current receives the success status, matching the tracing behavior of Ok<TValue>(TValue). Always prefer this factory over default(Result<Unit>): default represents failure with the Error.Unexpected sentinel, not success.

Ok<TValue>(TValue)

Creates a successful result wrapping the provided value.

public static Result<TValue> Ok<TValue>(TValue value)

Parameters

value TValue

The value to wrap in the successful result.

Returns

Result<TValue>

A successful Result<TValue> containing value.

Type Parameters

TValue

The success value type.

ParallelAsync<T1, T2>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>)

Starts 2 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>) ParallelAsync<T1, T2>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

Returns

(Task<Result<T1>>, Task<Result<T2>>)

A tuple of 2 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

ParallelAsync<T1, T2, T3>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>, Func<Task<Result<T3>>>)

Starts 3 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>) ParallelAsync<T1, T2, T3>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2, Func<Task<Result<T3>>> taskFactory3)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

taskFactory3 Func<Task<Result<T3>>>

Factory function that creates and starts the third task.

Returns

(Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>)

A tuple of 3 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

T3

The type of the result value from the third operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async(),
    () => GetData3Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

ParallelAsync<T1, T2, T3, T4>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>, Func<Task<Result<T3>>>, Func<Task<Result<T4>>>)

Starts 4 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>) ParallelAsync<T1, T2, T3, T4>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2, Func<Task<Result<T3>>> taskFactory3, Func<Task<Result<T4>>> taskFactory4)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

taskFactory3 Func<Task<Result<T3>>>

Factory function that creates and starts the third task.

taskFactory4 Func<Task<Result<T4>>>

Factory function that creates and starts the fourth task.

Returns

(Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>)

A tuple of 4 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

T3

The type of the result value from the third operation.

T4

The type of the result value from the fourth operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async(),
    () => GetData3Async(),
    () => GetData4Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

ParallelAsync<T1, T2, T3, T4, T5>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>, Func<Task<Result<T3>>>, Func<Task<Result<T4>>>, Func<Task<Result<T5>>>)

Starts 5 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>) ParallelAsync<T1, T2, T3, T4, T5>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2, Func<Task<Result<T3>>> taskFactory3, Func<Task<Result<T4>>> taskFactory4, Func<Task<Result<T5>>> taskFactory5)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

taskFactory3 Func<Task<Result<T3>>>

Factory function that creates and starts the third task.

taskFactory4 Func<Task<Result<T4>>>

Factory function that creates and starts the fourth task.

taskFactory5 Func<Task<Result<T5>>>

Factory function that creates and starts the fifth task.

Returns

(Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>)

A tuple of 5 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

T3

The type of the result value from the third operation.

T4

The type of the result value from the fourth operation.

T5

The type of the result value from the fifth operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async(),
    () => GetData3Async(),
    () => GetData4Async(),
    () => GetData5Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

ParallelAsync<T1, T2, T3, T4, T5, T6>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>, Func<Task<Result<T3>>>, Func<Task<Result<T4>>>, Func<Task<Result<T5>>>, Func<Task<Result<T6>>>)

Starts 6 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>) ParallelAsync<T1, T2, T3, T4, T5, T6>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2, Func<Task<Result<T3>>> taskFactory3, Func<Task<Result<T4>>> taskFactory4, Func<Task<Result<T5>>> taskFactory5, Func<Task<Result<T6>>> taskFactory6)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

taskFactory3 Func<Task<Result<T3>>>

Factory function that creates and starts the third task.

taskFactory4 Func<Task<Result<T4>>>

Factory function that creates and starts the fourth task.

taskFactory5 Func<Task<Result<T5>>>

Factory function that creates and starts the fifth task.

taskFactory6 Func<Task<Result<T6>>>

Factory function that creates and starts the sixth task.

Returns

(Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>)

A tuple of 6 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

T3

The type of the result value from the third operation.

T4

The type of the result value from the fourth operation.

T5

The type of the result value from the fifth operation.

T6

The type of the result value from the sixth operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async(),
    () => GetData3Async(),
    () => GetData4Async(),
    () => GetData5Async(),
    () => GetData6Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

ParallelAsync<T1, T2, T3, T4, T5, T6, T7>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>, Func<Task<Result<T3>>>, Func<Task<Result<T4>>>, Func<Task<Result<T5>>>, Func<Task<Result<T6>>>, Func<Task<Result<T7>>>)

Starts 7 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>, Task<Result<T7>>) ParallelAsync<T1, T2, T3, T4, T5, T6, T7>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2, Func<Task<Result<T3>>> taskFactory3, Func<Task<Result<T4>>> taskFactory4, Func<Task<Result<T5>>> taskFactory5, Func<Task<Result<T6>>> taskFactory6, Func<Task<Result<T7>>> taskFactory7)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

taskFactory3 Func<Task<Result<T3>>>

Factory function that creates and starts the third task.

taskFactory4 Func<Task<Result<T4>>>

Factory function that creates and starts the fourth task.

taskFactory5 Func<Task<Result<T5>>>

Factory function that creates and starts the fifth task.

taskFactory6 Func<Task<Result<T6>>>

Factory function that creates and starts the sixth task.

taskFactory7 Func<Task<Result<T7>>>

Factory function that creates and starts the seventh task.

Returns

(Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>, Task<Result<T7>>)

A tuple of 7 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

T3

The type of the result value from the third operation.

T4

The type of the result value from the fourth operation.

T5

The type of the result value from the fifth operation.

T6

The type of the result value from the sixth operation.

T7

The type of the result value from the seventh operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async(),
    () => GetData3Async(),
    () => GetData4Async(),
    () => GetData5Async(),
    () => GetData6Async(),
    () => GetData7Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

ParallelAsync<T1, T2, T3, T4, T5, T6, T7, T8>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>, Func<Task<Result<T3>>>, Func<Task<Result<T4>>>, Func<Task<Result<T5>>>, Func<Task<Result<T6>>>, Func<Task<Result<T7>>>, Func<Task<Result<T8>>>)

Starts 8 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>, Task<Result<T7>>, Task<Result<T8>>) ParallelAsync<T1, T2, T3, T4, T5, T6, T7, T8>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2, Func<Task<Result<T3>>> taskFactory3, Func<Task<Result<T4>>> taskFactory4, Func<Task<Result<T5>>> taskFactory5, Func<Task<Result<T6>>> taskFactory6, Func<Task<Result<T7>>> taskFactory7, Func<Task<Result<T8>>> taskFactory8)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

taskFactory3 Func<Task<Result<T3>>>

Factory function that creates and starts the third task.

taskFactory4 Func<Task<Result<T4>>>

Factory function that creates and starts the fourth task.

taskFactory5 Func<Task<Result<T5>>>

Factory function that creates and starts the fifth task.

taskFactory6 Func<Task<Result<T6>>>

Factory function that creates and starts the sixth task.

taskFactory7 Func<Task<Result<T7>>>

Factory function that creates and starts the seventh task.

taskFactory8 Func<Task<Result<T8>>>

Factory function that creates and starts the eighth task.

Returns

(Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>, Task<Result<T7>>, Task<Result<T8>>)

A tuple of 8 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

T3

The type of the result value from the third operation.

T4

The type of the result value from the fourth operation.

T5

The type of the result value from the fifth operation.

T6

The type of the result value from the sixth operation.

T7

The type of the result value from the seventh operation.

T8

The type of the result value from the eighth operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async(),
    () => GetData3Async(),
    () => GetData4Async(),
    () => GetData5Async(),
    () => GetData6Async(),
    () => GetData7Async(),
    () => GetData8Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

ParallelAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9>(Func<Task<Result<T1>>>, Func<Task<Result<T2>>>, Func<Task<Result<T3>>>, Func<Task<Result<T4>>>, Func<Task<Result<T5>>>, Func<Task<Result<T6>>>, Func<Task<Result<T7>>>, Func<Task<Result<T8>>>, Func<Task<Result<T9>>>)

Starts 9 async operations in parallel and returns a tuple of tasks. All operations start immediately and execute concurrently.

public static (Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>, Task<Result<T7>>, Task<Result<T8>>, Task<Result<T9>>) ParallelAsync<T1, T2, T3, T4, T5, T6, T7, T8, T9>(Func<Task<Result<T1>>> taskFactory1, Func<Task<Result<T2>>> taskFactory2, Func<Task<Result<T3>>> taskFactory3, Func<Task<Result<T4>>> taskFactory4, Func<Task<Result<T5>>> taskFactory5, Func<Task<Result<T6>>> taskFactory6, Func<Task<Result<T7>>> taskFactory7, Func<Task<Result<T8>>> taskFactory8, Func<Task<Result<T9>>> taskFactory9)

Parameters

taskFactory1 Func<Task<Result<T1>>>

Factory function that creates and starts the first task.

taskFactory2 Func<Task<Result<T2>>>

Factory function that creates and starts the second task.

taskFactory3 Func<Task<Result<T3>>>

Factory function that creates and starts the third task.

taskFactory4 Func<Task<Result<T4>>>

Factory function that creates and starts the fourth task.

taskFactory5 Func<Task<Result<T5>>>

Factory function that creates and starts the fifth task.

taskFactory6 Func<Task<Result<T6>>>

Factory function that creates and starts the sixth task.

taskFactory7 Func<Task<Result<T7>>>

Factory function that creates and starts the seventh task.

taskFactory8 Func<Task<Result<T8>>>

Factory function that creates and starts the eighth task.

taskFactory9 Func<Task<Result<T9>>>

Factory function that creates and starts the ninth task.

Returns

(Task<Result<T1>>, Task<Result<T2>>, Task<Result<T3>>, Task<Result<T4>>, Task<Result<T5>>, Task<Result<T6>>, Task<Result<T7>>, Task<Result<T8>>, Task<Result<T9>>)

A tuple of 9 tasks that can be awaited using WhenAllAsync.

Type Parameters

T1

The type of the result value from the first operation.

T2

The type of the result value from the second operation.

T3

The type of the result value from the third operation.

T4

The type of the result value from the fourth operation.

T5

The type of the result value from the fifth operation.

T6

The type of the result value from the sixth operation.

T7

The type of the result value from the seventh operation.

T8

The type of the result value from the eighth operation.

T9

The type of the result value from the ninth operation.

Examples

var result = await Result.ParallelAsync(
    () => GetData1Async(),
    () => GetData2Async(),
    () => GetData3Async(),
    () => GetData4Async(),
    () => GetData5Async(),
    () => GetData6Async(),
    () => GetData7Async(),
    () => GetData8Async(),
    () => GetData9Async()
).WhenAllAsync();

Remarks

This method invokes the factory functions eagerly and returns the created tasks as a tuple. No returned task is awaited here. Already-started tasks can also execute concurrently and be combined directly with WhenAllAsync.

Any tasks that are created successfully can execute concurrently once started by their factories. If a factory throws synchronously, the exception escapes immediately and later factories are not invoked.

ParallelAsync builds the tuple structure without awaiting any tasks.

Try(Action, Func<Exception, Error>?)

Executes the action and converts exceptions to a failed no-payload result using the optional mapper.

public static Result<Unit> Try(Action work, Func<Exception, Error>? map = null)

Parameters

work Action
map Func<Exception, Error>

Returns

Result<Unit>

TryAsync(Func<Task>, Func<Exception, Error>?)

Executes the asynchronous action and converts exceptions to a failed no-payload result.

public static Task<Result<Unit>> TryAsync(Func<Task> work, Func<Exception, Error>? map = null)

Parameters

work Func<Task>
map Func<Exception, Error>

Returns

Task<Result<Unit>>

TryAsync<T>(Func<Task<T>>, Func<Exception, Error>?)

Executes the asynchronous function and converts exceptions to a failed result.

public static Task<Result<T>> TryAsync<T>(Func<Task<T>> func, Func<Exception, Error>? map = null)

Parameters

func Func<Task<T>>
map Func<Exception, Error>

Returns

Task<Result<T>>

Type Parameters

T

Exceptions

ArgumentNullException

Thrown when func is null.

Try<T>(Func<T>, Func<Exception, Error>?)

Executes the function and converts exceptions to a failed result using the optional mapper (default Error.Unexpected with a per-incident fault ID).

public static Result<T> Try<T>(Func<T> func, Func<Exception, Error>? map = null)

Parameters

func Func<T>
map Func<Exception, Error>

Returns

Result<T>

Type Parameters

T

Exceptions

ArgumentNullException

Thrown when func is null.