HttpResultPattern 1.0.0

dotnet add package HttpResultPattern --version 1.0.0
                    
NuGet\Install-Package HttpResultPattern -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="HttpResultPattern" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HttpResultPattern" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="HttpResultPattern" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HttpResultPattern --version 1.0.0
                    
#r "nuget: HttpResultPattern, 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.
#:package HttpResultPattern@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HttpResultPattern&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=HttpResultPattern&version=1.0.0
                    
Install as a Cake Tool

Result Pattern NuGet Package

This NuGet package provides a simple and extensible Result pattern implementation to handle the return of either a successful value or an error. It's designed to improve code readability and manage error handling in a functional style. The Result<TValue, TError> type offers a way to work with two possible outcomes: success (value) or failure (error).

This package will help you handle errors in a clean and structured way, avoiding exceptions for control flow, and using explicit success and error handling.

Key Features

  • Generic Result Type: Allows returning a result with a success value of type TValue or an error of type TError.
  • Implicit Conversion: Automatically converts values to a success result or errors to an error result.
  • Error Handling: Supports both success and failure result handling using the Match() method, with or without custom success/failure actions.
  • Readability: Makes it easier to work with multiple outcomes while avoiding complex try-catch blocks or null checks.

Instalação

To install the package via NuGet, run the following command in your .NET project:

  dotnet add package ResultPattern

Or use the NuGet Package Manager:

  Install-Package ResultPattern

Usage

1 - Returning a Result

  public Result<int, MyError> DoSomething()
{
    if (operationSucceeded)
    {
        return 42; // Implicitly creates a success result.
    }
    else
    {
        return new Error(System.Net.HttpStatusCode.BadRequest, "Operattion Failed"); // Implicitly creates an error result.
    }
}

2 - Handling the Result

You can handle the result using the Match() method, which provides two overloads for error and success processing.

Example 1: Default Match

var result = DoSomething();
return result.Match(); // Will return a `200 OK` with the value, or a `ContentResult` with an error message and status code defined previous.

Example 2: Custom Match

var result = DoSomething();
return result.Match(
    success => Results.Ok(success),
    error => Results.Problem(detail: error.Message, statusCode: (int)error.StatusCode)
);

Contributing

Feel free to submit issues, pull requests, or feature requests to improve this package. Contributions are welcome!

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

This package was developed and maintained by Pedro Ventura.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 169 10/23/2024