From 9abd00487bb8fa8d81e9e447a6b7af4ed2d119d5 Mon Sep 17 00:00:00 2001 From: AJ Date: Tue, 18 Nov 2025 17:43:12 -0500 Subject: [PATCH] Add Result pattern for standardized error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement Result and Result classes to provide type-safe, functional error handling throughout the application. This eliminates the need for exceptions as control flow and provides a consistent way to communicate success/failure. Features: - Result for operations that return values - Result for operations without return values - Map() method for functional composition - IsSuccess/IsFailure properties for easy checking This pattern enables cleaner service contracts and eliminates the need for out parameters and exception handling boilerplate. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- CutList/Common/Result.cs | 96 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 CutList/Common/Result.cs diff --git a/CutList/Common/Result.cs b/CutList/Common/Result.cs new file mode 100644 index 0000000..628bf7e --- /dev/null +++ b/CutList/Common/Result.cs @@ -0,0 +1,96 @@ +using System; + +namespace CutList.Common +{ + /// + /// Represents the result of an operation that can either succeed with a value or fail with an error. + /// Implements the Result pattern for standardized error handling. + /// + /// The type of the success value + public class Result + { + private Result(T value, bool isSuccess, string error) + { + Value = value; + IsSuccess = isSuccess; + Error = error; + } + + public T Value { get; } + public bool IsSuccess { get; } + public bool IsFailure => !IsSuccess; + public string Error { get; } + + /// + /// Creates a successful result with a value. + /// + public static Result Success(T value) + { + return new Result(value, true, null); + } + + /// + /// Creates a failed result with an error message. + /// + public static Result Failure(string error) + { + if (string.IsNullOrWhiteSpace(error)) + throw new ArgumentException("Error message cannot be empty", nameof(error)); + + return new Result(default(T), false, error); + } + + /// + /// Maps the success value to a new result type. + /// + public Result Map(Func mapper) + { + if (IsFailure) + return Result.Failure(Error); + + try + { + return Result.Success(mapper(Value)); + } + catch (Exception ex) + { + return Result.Failure(ex.Message); + } + } + } + + /// + /// Represents the result of an operation that can succeed or fail without a return value. + /// + public class Result + { + protected Result(bool isSuccess, string error) + { + IsSuccess = isSuccess; + Error = error; + } + + public bool IsSuccess { get; } + public bool IsFailure => !IsSuccess; + public string Error { get; } + + /// + /// Creates a successful result. + /// + public static Result Success() + { + return new Result(true, null); + } + + /// + /// Creates a failed result with an error message. + /// + public static Result Failure(string error) + { + if (string.IsNullOrWhiteSpace(error)) + throw new ArgumentException("Error message cannot be empty", nameof(error)); + + return new Result(false, error); + } + } +}