The ResultObject package provides a robust utility to handle operation outcomes in .NET applications. It promotes clean code by clearly separating success from failure scenarios, offering rich error categorization, and providing type-safe error handling without relying on exceptions.
- Success/Failure Handling: Type-safe operation results with clear success/failure states
- Error Categories: Built-in and custom error categorization for better error handling
- Framework Support: Targets both .NET 8.0 and .NET 9.0
- Generic Result Types: Type-safe results with support for any value type
- Error Management: Detailed error information with categorization and sanitization
- Type-Safe Casting: Transform result values while preserving error information
- Unit Results: Support for void-equivalent operations
- Stack Trace Support: Optional stack traces for debugging
- Error Sanitization: Configurable error detail sanitization for external exposure
Install via NuGet:
dotnet add package ResultObject
Or search for "ResultObject" in the Visual Studio package manager.
// Success results
var successResult = Result.Success(42);
var voidSuccess = Result.Success(); // Returns Result<Unit>
// Failure results
var failureResult = Result.Failure<int>(
"404",
"Not Found",
"The requested resource was not found."
);
if (successResult.IsSuccess)
{
Console.WriteLine($"Operation succeeded with value: {successResult.Value}");
}
if (failureResult.IsFailure)
{
Console.WriteLine($"Operation failed: {failureResult.Error.Message}");
}
var error = new ResultError(
"USER_404",
"User Not Found",
"The specified user does not exist",
ErrorCategory.NotFound
);
var result = Result.Failure<User>(error);
if (result.Error?.Category == ErrorCategory.NotFound)
{
// Handle not found case
}
public enum OrderErrorCategory
{
Validation,
Processing,
Payment
}
var error = new ResultError<OrderErrorCategory>(
"ORD001",
"Invalid Order",
"Order total cannot be negative",
OrderErrorCategory.Validation
);
var result = Result.Failure<Order, OrderErrorCategory>(error);
var error = new ResultError(
"DB_ERROR",
"Database Connection Failed",
"Failed to connect to server: sensitive-server:1433"
)
.WithStackTrace();
// Sanitize for external use
var sanitized = error.Sanitize(ResultError.SanitizationLevel.Full);
var result = Result.Success<object>("42");
var castResult = result.Cast<string>();
if (castResult.IsSuccess)
{
Console.WriteLine($"Cast successful: {castResult.Value}");
}
public Result<Unit> DeleteUser(string userId)
{
try
{
// Delete user logic
return Result.Success();
}
catch (Exception ex)
{
return Result.Failure<Unit>("DELETE_FAILED", "Deletion Failed", ex.Message);
}
}
IsSuccess
: Indicates if the operation succeededIsFailure
: Indicates if the operation failedValue
: The operation's value (if successful)Error
: Error information (if failed)Cast<T>()
: Safely cast the result's value
Success()
: Creates a Unit resultSuccess<TValue>(TValue value)
: Creates a success resultFailure<TValue>(ResultError error)
: Creates a failure resultFailure<TValue>(string code, string reason, string message)
: Creates a failure resultFailure<TValue, TErrorCategory>(ResultError<TErrorCategory> error)
: Creates a categorized failure result
Code
: Error identifierReason
: Brief error descriptionMessage
: Detailed error messageCategory
: Error categoryInnerError
: Nested error informationStackTrace
: Optional stack traceWithStackTrace()
: Adds current stack traceSanitize()
: Creates sanitized error copy
Validation
: Input/business rule violationsNotFound
: Resource not foundUnauthorized
: Authentication required/failedForbidden
: Insufficient permissionsConflict
: State conflictsInternal
: System errorsExternal
: External service errors
This project is licensed under the MIT License.