Struct DatabaseResult<T>
- Namespace
- FishMMO.Database
- Assembly
- FishMMO-DB.dll
Represents the result of a database operation with success/failure status and optional data. Type-safe alternative to tuples that provides consistent error handling across all database operations. Follows Functional Programming principles: explicit success/failure handling without exceptions for expected failures.
public readonly struct DatabaseResult<T>
Type Parameters
TThe type of data returned on success.
- Inherited Members
Properties
Data
Gets the result data if the operation succeeded. Default value of T if operation failed.
public T Data { get; }
Property Value
- T
ErrorCode
Gets the error code if the operation failed. Null when IsSuccess is true; non-null when IsSuccess is false.
public string? ErrorCode { get; }
Property Value
ErrorMessage
Gets the safe error message suitable for client communication. Null when IsSuccess is true; non-null when IsSuccess is false.
public string? ErrorMessage { get; }
Property Value
IsSuccess
Gets a value indicating whether the operation succeeded.
public bool IsSuccess { get; }
Property Value
IsTransient
Gets a value indicating whether the error is transient and the operation can be retried. False if operation succeeded or error is permanent.
public bool IsTransient { get; }
Property Value
Methods
Deconstruct(out bool, out T, out string?, out string?)
Deconstructs the result into its components for pattern matching.
public void Deconstruct(out bool isSuccess, out T data, out string? errorCode, out string? errorMessage)
Parameters
isSuccessboolWhether the operation succeeded.
dataTThe result data (default if failed).
errorCodestringThe error code (null if succeeded).
errorMessagestringThe error message (null if succeeded).
Failure(string?, string?, bool)
Creates a failed result with error information.
public static DatabaseResult<T> Failure(string? errorCode, string? errorMessage, bool isTransient = false)
Parameters
errorCodestringThe error code for categorization. Defaults to
DB_ERRORif null.errorMessagestringSafe error message for client communication. Defaults to a generic message if null.
isTransientboolWhether the error is transient and retryable.
Returns
- DatabaseResult<T>
A failed DatabaseResult containing error information.
FromException(DatabaseException)
Creates a failed result from a DatabaseException.
public static DatabaseResult<T> FromException(DatabaseException exception)
Parameters
exceptionDatabaseExceptionThe database exception to convert.
Returns
- DatabaseResult<T>
A failed DatabaseResult containing exception information.
Success(T)
Creates a successful result with data.
public static DatabaseResult<T> Success(T data)
Parameters
dataTThe result data.
Returns
- DatabaseResult<T>
A successful DatabaseResult containing the data.
ToString()
Returns a string representation of the result.
public override string ToString()
Returns
- string
Formatted string with success/failure status.