Table of Contents

Class Result<T>

Namespace
JD.Domain.Abstractions
Assembly
JD.Domain.Abstractions.dll

Represents the result of an operation that can succeed with a value or fail with errors.

public sealed class Result<T>

Type Parameters

T

The type of the success value.

Inheritance
Result<T>
Inherited Members

Properties

Errors

Gets the collection of errors. Empty for successful results.

public IReadOnlyList<DomainError> Errors { get; }

Property Value

IReadOnlyList<DomainError>

IsFailure

Gets a value indicating whether the operation failed.

public bool IsFailure { get; }

Property Value

bool

IsSuccess

Gets a value indicating whether the operation succeeded.

public bool IsSuccess { get; }

Property Value

bool

Value

Gets the success value. Throws if the result is a failure.

public T Value { get; }

Property Value

T

Exceptions

InvalidOperationException

Thrown when accessing value on a failure result.

Methods

Bind<TResult>(Func<T, Result<TResult>>)

Binds the result to another result-producing function.

public Result<TResult> Bind<TResult>(Func<T, Result<TResult>> bind)

Parameters

bind Func<T, Result<TResult>>

The binding function.

Returns

Result<TResult>

The result of the binding function if successful, otherwise the original errors.

Type Parameters

TResult

The type of the bound result.

Failure(DomainError)

Creates a failure result with the specified error.

public static Result<T> Failure(DomainError error)

Parameters

error DomainError

The error.

Returns

Result<T>

A failure result.

Failure(params DomainError[])

Creates a failure result with the specified errors.

public static Result<T> Failure(params DomainError[] errors)

Parameters

errors DomainError[]

The collection of errors.

Returns

Result<T>

A failure result.

Failure(IReadOnlyList<DomainError>)

Creates a failure result with the specified errors.

public static Result<T> Failure(IReadOnlyList<DomainError> errors)

Parameters

errors IReadOnlyList<DomainError>

The collection of errors.

Returns

Result<T>

A failure result.

Map<TResult>(Func<T, TResult>)

Maps the success value to a new type using the specified function.

public Result<TResult> Map<TResult>(Func<T, TResult> map)

Parameters

map Func<T, TResult>

The mapping function.

Returns

Result<TResult>

A result with the mapped value if successful, otherwise the original errors.

Type Parameters

TResult

The type of the mapped value.

Match<TResult>(Func<T, TResult>, Func<IReadOnlyList<DomainError>, TResult>)

Matches the result to one of two functions based on success or failure.

public TResult Match<TResult>(Func<T, TResult> onSuccess, Func<IReadOnlyList<DomainError>, TResult> onFailure)

Parameters

onSuccess Func<T, TResult>

Function to execute on success.

onFailure Func<IReadOnlyList<DomainError>, TResult>

Function to execute on failure.

Returns

TResult

The result of the executed function.

Type Parameters

TResult

The type of the result.

Success(T)

Creates a successful result with the specified value.

public static Result<T> Success(T value)

Parameters

value T

The success value.

Returns

Result<T>

A successful result.

Operators

implicit operator Result<T>(DomainError)

Implicitly converts a domain error to a failure result.

public static implicit operator Result<T>(DomainError error)

Parameters

error DomainError

The error.

Returns

Result<T>

implicit operator Result<T>(T)

Implicitly converts a value to a successful result.

public static implicit operator Result<T>(T value)

Parameters

value T

The value.

Returns

Result<T>