XResults 2.2.0

dotnet add package XResults --version 2.2.0
NuGet\Install-Package XResults -Version 2.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XResults" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XResults --version 2.2.0
#r "nuget: XResults, 2.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install XResults as a Cake Addin
#addin nuget:?package=XResults&version=2.2.0

// Install XResults as a Cake Tool
#tool nuget:?package=XResults&version=2.2.0

Description

A compact results library similar to FluentResults, offering all essential features for effective error handling and response management.

How to Use the Library

The library offers four main result types, each suited for different scenarios:

  1. Result: Indicates a simple success or failure of an operation.
  2. Result<T>: Returns a value if the operation is successful.
  3. Result<T, TError>: Returns a value or a custom error if the operation fails.
  4. SuccessOr<TError>: Indicates success or returns a custom error for operations that do not return a value.

Result

Use Result to simply indicate the success or failure of an operation.

Usage Example
Result DoWork()  
{  
    if (isOperationSuccessful) 
    {        
	    return Result.Ok();  
    }    
    else  
    {  
        return Result.Fail("Operation is unsuccessful"); 
    }
}
Properties
Result result = DoWork(); 

result.IsSuccess; 
result.IsFailure;
result.ErrorMessage; 
  • IsSuccess: Returns true if the operation was successful.
  • IsFailure Returns true if the operation failed.
  • ErrorMessage: Provides the error message if the operation failed.

Result<T>

Use Result<T> when the operation needs to return a value upon success.

Usage Example
Result<int> DoWork()  
{  
    int valueToReturn = 123;
    if (isOperationSuccessful)  
    {        
	    return Result.Ok(valueToReturn);  
    }    
    else  
    {  
        return Result.Fail("Operation is unsuccessful");  
    }
}
Properties
Result<int> result = DoWork(); 

result.IsSuccess; 
result.IsFailure;
result.Value; 
result.ErrorMessage; 
  • IsSuccess: Indicates if the operation was successful.
  • IsFailure: Indicates if the operation failed.
  • Value: The value returned by the operation (e.g., 123).
  • ErrorMessage: The error message if the operation failed.

Result<T, TError>

Use Result<T, TError> to return either a value upon success or a custom error if the operation fails.

Usage Example
Result<int, CustomError> DoWork()  
{  
    int valueToReturn = 123;  
    if (isOperationSuccessful)  
    {        
	    return Result.Ok(valueToReturn);  
    }    
    else  
    {  
		return Result.Fail(new CustomError(666, "Error message")); 
    }
}
Custom Error Class
public class CustomError
{  
    public int StatusCode { get; init; }  
    public string Message { get; init; }  

    public CustomError(int statusCode, string message)  
    {  
        StatusCode = statusCode;  
        Message = message;  
    }
}
Properties
Result<int, CustomError> result = DoWork(); 

result.IsSuccess; 
result.IsFailure;
result.Value; 
result.Error; 
  • IsSuccess: Whether the operation succeeded.
  • IsFailure: Indicates if the operation failed.
  • Value: The value returned if successful.
  • Error: The custom error returned if the operation failed.

SuccessOr<TError>

Use SuccessOr<TError> for operations that don't return a value but may fail with a custom error.

Usage Example
SuccessOr<CustomError> DoWork()  
{  
    if (isOperationSuccessful)  
    {        
	    return Result.Ok();  
    }    
    else  
    {  
        return Result.Fail(new CustomError(666, "Error message"));  
    }
}
Properties
SuccessOr<CustomError> result = DoWork(); 

result.IsSuccess; 
result.IsFailure;
result.Error; 
  • IsSuccess: Indicates if the operation was successful.
  • IsFailure: Indicates if the operation failed.
  • Error: The custom error returned if the operation failed.

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.0 72 4/25/2024
2.1.0 71 4/25/2024
2.0.0 72 4/25/2024
1.3.0 65 4/24/2024
1.2.0 84 4/22/2024
1.1.0 81 4/22/2024
1.0.0 77 4/22/2024