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
TThe 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
Exception
Gets the exception that caused the failure, if any.
public Exception? Exception { 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 was successful.
public bool IsSuccess { get; }
Property Value
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
errorMessagestringThe error message describing the failure.
exceptionExceptionThe 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
onSuccessFunc<T, Task<TResult>>The asynchronous function to execute if the operation succeeded.
onFailureFunc<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
TResultThe 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
onSuccessFunc<T, TResult>The function to execute if the operation succeeded.
onFailureFunc<string, TResult>The function to execute if the operation failed.
Returns
- TResult
The result of executing the appropriate function.
Type Parameters
TResultThe type of the result to return.
Success(T)
Creates a successful result with the specified data.
public static ServiceResult<T> Success(T data)
Parameters
dataTThe data to include in the result.
Returns
- ServiceResult<T>
A successful service result containing the data.