XResults 2.2.0
dotnet add package XResults --version 2.2.0
NuGet\Install-Package XResults -Version 2.2.0
<PackageReference Include="XResults" Version="2.2.0" />
paket add XResults --version 2.2.0
#r "nuget: XResults, 2.2.0"
// 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:
Result
: Indicates a simple success or failure of an operation.Result<T>
: Returns a value if the operation is successful.Result<T, TError>
: Returns a value or a custom error if the operation fails.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
: Returnstrue
if the operation was successful.IsFailure
Returnstrue
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 | Versions 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. |
-
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.