namespace octo_fiesta.Services.Common;
///
/// Represents the result of an operation that can either succeed with a value or fail with an error.
/// This pattern allows explicit error handling without using exceptions for control flow.
///
/// The type of the value returned on success
public class Result
{
///
/// Indicates whether the operation succeeded
///
public bool IsSuccess { get; }
///
/// Indicates whether the operation failed
///
public bool IsFailure => !IsSuccess;
///
/// The value returned on success (null if failed)
///
public T? Value { get; }
///
/// The error that occurred on failure (null if succeeded)
///
public Error? Error { get; }
private Result(bool isSuccess, T? value, Error? error)
{
IsSuccess = isSuccess;
Value = value;
Error = error;
}
///
/// Creates a successful result with a value
///
public static Result Success(T value)
{
return new Result(true, value, null);
}
///
/// Creates a failed result with an error
///
public static Result Failure(Error error)
{
return new Result(false, default, error);
}
///
/// Implicit conversion from T to Result<T> for convenience
///
public static implicit operator Result(T value)
{
return Success(value);
}
///
/// Implicit conversion from Error to Result<T> for convenience
///
public static implicit operator Result(Error error)
{
return Failure(error);
}
}
///
/// Non-generic Result for operations that don't return a value
///
public class Result
{
public bool IsSuccess { get; }
public bool IsFailure => !IsSuccess;
public Error? Error { get; }
private Result(bool isSuccess, Error? error)
{
IsSuccess = isSuccess;
Error = error;
}
public static Result Success()
{
return new Result(true, null);
}
public static Result Failure(Error error)
{
return new Result(false, error);
}
public static implicit operator Result(Error error)
{
return Failure(error);
}
}