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
TThe 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
IsFailure
Gets a value indicating whether the operation failed.
public bool IsFailure { get; }
Property Value
IsSuccess
Gets a value indicating whether the operation succeeded.
public bool IsSuccess { get; }
Property Value
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
Returns
- Result<TResult>
The result of the binding function if successful, otherwise the original errors.
Type Parameters
TResultThe type of the bound result.
Failure(DomainError)
Creates a failure result with the specified error.
public static Result<T> Failure(DomainError error)
Parameters
errorDomainErrorThe 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
errorsDomainError[]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
errorsIReadOnlyList<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
mapFunc<T, TResult>The mapping function.
Returns
- Result<TResult>
A result with the mapped value if successful, otherwise the original errors.
Type Parameters
TResultThe 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
onSuccessFunc<T, TResult>Function to execute on success.
onFailureFunc<IReadOnlyList<DomainError>, TResult>Function to execute on failure.
Returns
- TResult
The result of the executed function.
Type Parameters
TResultThe type of the result.
Success(T)
Creates a successful result with the specified value.
public static Result<T> Success(T value)
Parameters
valueTThe 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
errorDomainErrorThe 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
valueTThe value.
Returns
- Result<T>