Resultant 1.0.2

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

// Install Resultant as a Cake Tool
#tool nuget:?package=Resultant&version=1.0.2                

Resultant

Resultant is a robust and flexible C# library designed for implementing the Result pattern, enhancing error handling in .NET applications. It offers a structured way to return success or error information, making your code more readable, maintainable, and less prone to errors.

Features

  • Generic and Non-Generic Result Types: Handle operations with and without return values.
  • Fluent API: Chain operations for readability and efficiency.
  • Async Support: Seamlessly integrate with async methods.
  • Error Handling: Advanced error handling with messages, codes, and multiple errors.
  • Paged Results: Special handling for operations returning paginated data.
  • Implicit Conversion Operators: Simplify usage with intuitive type conversions.

Getting Started

Installation

Install Resultant via NuGet:

dotnet add package Resultant

Basic Usage

Creating a Successful Result
var successResult = Result.Ok();
var successResultWithValue = Result.Ok("Success value");
Creating a Failure Result
var failResult = Result.Fail("Error message");
var failResultWithCode = Result.Fail(new Error("Error message", errorCode));
Working with Result
public Result<string> GetData()
{
    if (someCondition)
        return Result.Fail("Error occurred");

    return Result.Ok("Data");
}

var result = GetData();
if (result.IsSuccess)
{
    Console.WriteLine(result.Value); // Use the data
}
else
{
    Console.WriteLine(result.Error); // Handle the error
}
Using Async Methods
public async Task<Result<string>> GetDataAsync()
{
    // Async operation...
    return await Result.Ok("Async data");
}

// Usage
var result = await GetDataAsync();
Fluent API with Map and Bind

The Map and Bind methods provide a fluent way to transform and chain result operations.

  • Map: Use this method to transform the value of a successful result. It doesn't execute if the result is a failure.
public Result<int> ParseData(string data)
{
    if (int.TryParse(data, out var number))
        return Result.Ok(number);
    return Result.Fail("Invalid data");
}

var result = Result.Ok("123").Map(ParseData);
// If parsing succeeds, 'result' is a successful Result<int>
  • Bind: Use this method to chain results, where each result depends on the previous one.
public Result<string> FetchData(int id)
{
    // Fetch data logic...
    return Result.Ok("Fetched data");
}

public Result<string> ProcessData(string data)
{
    // Data processing logic...
    return Result.Ok("Processed data");
}

var result = Result.Ok(1)
    .Bind(FetchData)   // Fetch data with the id
    .Bind(ProcessData); // Then process the fetched data
// 'result' holds the final result of these chained operations
public Result<int> ParseData(string data)
{
    if (int.TryParse(data, out var number))
        return Result.Ok(number);

    return Result.Fail("Invalid data");
}

var result = Result.Ok("123").Map(ParseData);

Advanced Topics

Handling Paged Results

public PagedResult<Item> GetItems(int page, int pageSize)
{
    var items = FetchItems(page, pageSize); // Your logic to fetch items
    return PagedResult<Item>.Create(items, page, pageSize, totalItemCount);
}

Combining Results

Use Result.Combine to aggregate multiple results into one.

Error Handling

Customize error handling by using the Error class to include error codes and detailed messages.

Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

Check out our contributing guidelines for more information.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Arda Terekeci - @ardaterekeci

Project Link: https://github.com/adomorn/Resultant

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.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.2 204 12/17/2023
1.0.1 142 12/16/2023