EFE.Result 1.0.0

dotnet add package EFE.Result --version 1.0.0                
NuGet\Install-Package EFE.Result -Version 1.0.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="EFE.Result" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFE.Result --version 1.0.0                
#r "nuget: EFE.Result, 1.0.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 EFE.Result as a Cake Addin
#addin nuget:?package=EFE.Result&version=1.0.0

// Install EFE.Result as a Cake Tool
#tool nuget:?package=EFE.Result&version=1.0.0                

TS.Result NuGet Package

Overview

The EFE.Result package is a .NET library designed to encapsulate the result of operations in a consistent manner. It provides a structured approach to handling success and failure states, including support for detailed error reporting. This package is ideal for improving error handling and response consistency across various application layers, particularly in API development.

Features

  • Generic Result Type: Facilitates strong typing of operation outcomes, accommodating any data type.
  • Detailed Error Handling: Supports capturing multiple error messages, including error codes and optional targets.
  • HTTP Status Code Integration: Aligns operation results with HTTP standards using the ResultStatus enum.
  • Implicit Conversions: Streamlines result creation through implicit conversion operators.
  • Logging: Offers a method to log errors directly to the console.

Installation

You can install the EFE.Result package via the NuGet Package Manager, .NET CLI, or by editing your project file.

.NET CLI

dotnet add package EFE.Result

Package Manager

Install-Package EFE.Result

Usage

Success Cases

  • Successful Operation with Data:
var successResult = new Result<string>("Operation successful.");
  • Implicit Conversion for Success:
Result<string> result = "Operation successful.";
  • Using the Succeed Method:
Result<string> result = Result<string>.Succeed("Operation successful.");

Failure Cases

  • Failure with Status Code and Multiple Errors:
var errorDetails = new List<ErrorDetail>
{
    new ErrorDetail("Error 1", "400", "Field1"),
    new ErrorDetail("Error 2", "400", "Field2")
};
var errorResult = new Result<string>(ResultStatus.BadRequest, errorDetails);
  • Implicit Conversion for Failure:
Result<string> result = (ResultStatus.BadRequest, errorDetails);
  • Failure with Single Error Message:
Result<string> result = (ResultStatus.NotFound, new ErrorDetail("Item not found", "404"));
  • Using the Failure Method with One Error:
Result<string> result = Result<string>.Failure(ResultStatus.InternalServerError, new ErrorDetail("An error occurred.", "500"));
  • Using the Failure Method with Multiple Errors:
var errorDetails = new List<ErrorDetail>
{
    new ErrorDetail("Error 1", "500"),
    new ErrorDetail("Error 2", "500")
};
Result<string> result = Result<string>.Failure(ResultStatus.InternalServerError, errorDetails.ToArray());
  • Failure Method with Default 500 Status Code:
Result<string> result = Result<string>.Failure(new ErrorDetail("An unexpected error occurred.", "500"));

Logging Errors

You can log errors using the LogErrors method:

if (!result.IsSuccessful)
{
    result.LogErrors();
}

JSON Examples

  • Success Result:
{
  "data": "Operation successful.",
  "errorMessages": null,
  "isSuccessful": true
}
  • Error Result:
{
  "data": null,
  "errorMessages": [
    {
      "message": "Username must be at least 3 characters",
      "code": "400",
      "target": "username"
    }
  ],
  "isSuccessful": false
}

Contributing

We welcome contributions! Feel free to open an issue or submit a pull request on our GitHub repository for any suggestions or improvements.

License

EFE.Result is licensed under the MIT License. See the LICENSE file in the source repository for full 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
1.0.0 126 8/19/2024