Table of Contents

Class ServiceResult<T>

Namespace
Tudormobile.OpenTrivia.UI.Services
Assembly
Tudormobile.OpenTrivia.UI.dll

Represents the result of a service operation that can either succeed with data or fail with an error.

public class ServiceResult<T>

Type Parameters

T

The type of data returned on success.

Inheritance
ServiceResult<T>
Inherited Members

Properties

Data

Gets the data returned by the operation, or null if the operation failed.

public T? Data { get; }

Property Value

T

ErrorMessage

Gets the error message if the operation failed, or null if it succeeded.

public string? ErrorMessage { get; }

Property Value

string

Exception

Gets the exception that caused the failure, if any.

public Exception? Exception { get; }

Property Value

Exception

IsFailure

Gets a value indicating whether the operation failed.

public bool IsFailure { get; }

Property Value

bool

IsSuccess

Gets a value indicating whether the operation was successful.

public bool IsSuccess { get; }

Property Value

bool

Methods

Failure(string, Exception?)

Creates a failed result with the specified error message and optional exception.

public static ServiceResult<T> Failure(string errorMessage, Exception? exception = null)

Parameters

errorMessage string

The error message describing the failure.

exception Exception

The exception that caused the failure, if any.

Returns

ServiceResult<T>

A failed service result containing the error information.

MatchAsync<TResult>(Func<T, Task<TResult>>, Func<string, Task<TResult>>)

Asynchronously executes one of two functions based on whether the operation succeeded or failed.

public Task<TResult> MatchAsync<TResult>(Func<T, Task<TResult>> onSuccess, Func<string, Task<TResult>> onFailure)

Parameters

onSuccess Func<T, Task<TResult>>

The asynchronous function to execute if the operation succeeded.

onFailure Func<string, Task<TResult>>

The asynchronous function to execute if the operation failed.

Returns

Task<TResult>

A task representing the asynchronous operation, containing the result of executing the appropriate function.

Type Parameters

TResult

The type of the result to return.

Match<TResult>(Func<T, TResult>, Func<string, TResult>)

Executes one of two functions based on whether the operation succeeded or failed.

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

Parameters

onSuccess Func<T, TResult>

The function to execute if the operation succeeded.

onFailure Func<string, TResult>

The function to execute if the operation failed.

Returns

TResult

The result of executing the appropriate function.

Type Parameters

TResult

The type of the result to return.

Success(T)

Creates a successful result with the specified data.

public static ServiceResult<T> Success(T data)

Parameters

data T

The data to include in the result.

Returns

ServiceResult<T>

A successful service result containing the data.