Table of Contents

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

T

The 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

string

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

string

IsSuccess

Gets a value indicating whether the operation succeeded.

public bool IsSuccess { get; }

Property Value

bool

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

bool

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

isSuccess bool

Whether the operation succeeded.

data T

The result data (default if failed).

errorCode string

The error code (null if succeeded).

errorMessage string

The 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

errorCode string

The error code for categorization. Defaults to DB_ERROR if null.

errorMessage string

Safe error message for client communication. Defaults to a generic message if null.

isTransient bool

Whether 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

exception DatabaseException

The 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

data T

The 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.