TomRR.Core.ResultType 0.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package TomRR.Core.ResultType --version 0.0.2
                    
NuGet\Install-Package TomRR.Core.ResultType -Version 0.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="TomRR.Core.ResultType" Version="0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TomRR.Core.ResultType" Version="0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="TomRR.Core.ResultType" />
                    
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 TomRR.Core.ResultType --version 0.0.2
                    
#r "nuget: TomRR.Core.ResultType, 0.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.
#addin nuget:?package=TomRR.Core.ResultType&version=0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=TomRR.Core.ResultType&version=0.0.2
                    
Install as a Cake Tool

πŸ“¦ ResultType

A lightweight, expressive, and extensible result abstraction for .NET β€” with support for structured results, errors, and unit-only responses (like NoContent, Accepted, etc).


✨ Features

  • βœ… Simple Result<TSuccess, TError> model
  • βœ… Optional support for StatusOnly results
  • βœ… Clean pattern matching with .Match(...)
  • βœ… ASP.NET Core integration via ToActionResult()
  • βœ… Easily extensible with your own unit result types

πŸš€ Basic Usage

Result<TSuccess, TError>

Result<int, string> Divide(int a, int b)
{
    if (b == 0)
        return "Division by zero";

    return a / b;
}

var result = Divide(10, 0);

var message = result.Match(
    success => $"Result: {success}",
    error => $"Error: {error}"
);

Result<TSuccess, TError, TStatusOnly>

Supports status-only outcomes like NoCoÍntent, Accepted, etc.

Result<string, string, NoContent> TryFindItem(bool found)
{
    if (found)
        return "Found item";

    return Result.NoContent;
}

var response = TryFindItem(false).Match(
    success => $"Found: {success}",
    error => $"Error: {error}",
    status => "No item found."
);

🌐 ASP.NET Core Integration

Add this extension to convert Result into IActionResult:

using Microsoft.AspNetCore.Mvc;

namespace ResultType.Extensions;

public static class ResultExtensions
{
    public static IActionResult ToActionResult<TValue, TError>(
        this Result<TValue, TError> result)
    {
        return result.Match<IActionResult>(
            value => new OkObjectResult(value),
            error => new BadRequestObjectResult(error)
        );
    }

    public static IActionResult ToActionResult<TValue, TError, TStatusOnly>(
        this Result<TValue, TError, TStatusOnly> result)
        where TStatusOnly : IStatusOnlyResult
    {
        return result.Match<IActionResult>(
            value => new OkObjectResult(value),
            error => new BadRequestObjectResult(error),
            statusOnly => statusOnly switch
            {
                NoContent => new NoContentResult(),
                NotModified => new StatusCodeResult(304),
                _ => new StatusCodeResult(204)
            }
        );
    }
}

βœ… Controller Example

[HttpGet]
public IActionResult GetData() =>
    TryFindItem(true).ToActionResult();

🧩 Custom Unit (StatusOnly) Results

Define your own unit-only result:

[StructLayout(LayoutKind.Sequential, Size = 1)]
public readonly struct MyOwnResult : IStatusOnlyResult;

Expose it via the factory:

public static partial class Result
{
    public static MyOwnResult MyOwnResult => new MyOwnResult();
}

Use just like built-in NoContent, Created, etc.

🧰 Built-in Unit Result Types

Result Type Description HTTP Status
NoContent Operation successful, no content 204
Accepted Request accepted for async processing 202
Created New resource created 201
Success Generic success 200
Deleted Resource successfully deleted (custom) Custom
Updated Resource successfully updated (custom) Custom
NotModified Resource not modified 304

All are defined as zero-alloc readonly struct.

πŸ“¦ Installation

Install via NuGet:

dotnet add package ResultType

Or via your .csproj:

<PackageReference Include="ResultType" Version="0.0.2" />

πŸ“„ License

Licensed under the Apache License 2.0.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.
  • net9.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
0.0.4 46 7/7/2025
0.0.3 48 7/7/2025
0.0.2 52 7/5/2025
0.0.1 276 6/10/2025

0.0.2 - Architectural Refinements and API Enhancements
           - Introduced a second specialized Result types: Result<TValue, TError, TStatusOnly> for success/failure/status-only scenarios.
           - Added support for status-only results with constraint on IStatusOnlyResult.
           - Added match methods for better pattern matching and error handling.
           - Consolidated status-only types (`NoContent`, `Created`, etc.) into a dedicated location for better organization.
           - General code cleanup and documentation improvements.

           0.0.1 - Initial Testing Version: Rust-Like Result Type
           - Introduced a robust and type-safe "Rust-Like" Result type for explicit error handling in C#/.NET projects.
           - Result<TValue, TError>
           - Supports result states: Success, Failure.
           - Type safety and nullability analysis with comprehensive `MemberNotNullWhen` attributes.
           - Internal state management with private constructors, encouraging controlled instantiation.
           - Designed to promote more reliable and readable code by avoiding exceptions for control flow.
           - Supports implicit conversions for easy result creation.