Pavas.Patterns.Result 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pavas.Patterns.Result --version 1.0.2                
NuGet\Install-Package Pavas.Patterns.Result -Version 1.0.2                
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="Pavas.Patterns.Result" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pavas.Patterns.Result --version 1.0.2                
#r "nuget: Pavas.Patterns.Result, 1.0.2"                
#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 Pavas.Patterns.Result as a Cake Addin
#addin nuget:?package=Pavas.Patterns.Result&version=1.0.2

// Install Pavas.Patterns.Result as a Cake Tool
#tool nuget:?package=Pavas.Patterns.Result&version=1.0.2                

Result Design Pattern

Description

The Result design pattern is a technique used to handle the outcomes of operations that can either succeed or fail. This pattern is widely adopted in functional programming and has gained popularity in C# for its effectiveness in managing errors and ensuring safe, predictable code flow.

Basic Usage

The Result pattern encapsulates the result of an operation, which can either be successful or contain an error. Below are the common methods used with the Result pattern:

Creating a Result

  • Failure result: Use Result.Failure(error) to represent a failed operation.
  • Success result: Use Result.Success() to represent a successful operation without a value.
  • Success result with value: Use Result.Success(value) to represent a successful operation that returns a value.

Example

Result.Success();

Result.Success("Operation completed successfully");

new Error {
    Code = "Unespected",
    Name = "Unespected Error",
    Description = "A Unespected Error Has Been Ocurred",
    Extensions = { ["Key"] = "Value" }
};

ErrorFactory.CreateSystemError(
    "Error", 
    "OperationFailed", 
    "An error occurred"
);

ErrorFactory.CreateHttpError(
    StatusCodes.InternalServerError,
    "OperationFailed",
    "An error occurred"
);

Result.Failure(Error);

Matching on Result

result.Match(
    onSuccess: () => "Operation succeeded",
    onFailure: error => $"Operation failed: {error.Message}"
);

result.Match(
    onSuccess: value => $"Operation succeeded, result: {value}",
    onFailure: error => $"Operation failed: {error.Message}"
);

Benefits

  • Clear Error Handling: Distinguishes between successful and failed operations, ensuring that error handling is explicit and predictable.
  • Immutable Results: Once created, a Result object cannot be altered, making it safe to pass around in your codebase.
  • Functional Programming Influence: Encourages a functional approach to handling outcomes, improving code readability and maintainability.

Conclusion

The Result design pattern is a powerful tool for managing outcomes in C# applications, making your code more robust and easier to maintain. By encapsulating success and failure cases, it enforces a clear structure for handling different operation outcomes, leading to more reliable and bug-free code.

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.5 91 9/23/2024
1.0.4 114 8/25/2024
1.0.3 111 8/20/2024
1.0.2 130 8/13/2024
1.0.1 111 8/12/2024
1.0.0 105 8/2/2024