YDotNet.ResultType 1.0.1

dotnet add package YDotNet.ResultType --version 1.0.1
                    
NuGet\Install-Package YDotNet.ResultType -Version 1.0.1
                    
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="YDotNet.ResultType" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="YDotNet.ResultType" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="YDotNet.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 YDotNet.ResultType --version 1.0.1
                    
#r "nuget: YDotNet.ResultType, 1.0.1"
                    
#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=YDotNet.ResultType&version=1.0.1
                    
Install YDotNet.ResultType as a Cake Addin
#tool nuget:?package=YDotNet.ResultType&version=1.0.1
                    
Install YDotNet.ResultType as a Cake Tool

ResultType

Overview

ResultType is a lightweight C# library designed to facilitate structured handling of operation results in .NET applications. It provides a Result type that encapsulates success or failure outcomes, along with error details, and includes ASP.NET Core integration for seamless API response handling.

Features

  • Strongly-typed Result and Result<T> for representing success or failure states.
  • Built-in error handling with IError, Error, and ValidationError.
  • ASP.NET Core minimal API integration via ToHttpResult().
  • Utility methods like Match() for streamlined success/failure handling.

Installation

You can add ResultType to your .NET project using:

// Install via .NET CLI to upload
 

Usage

Basic Success and Failure Handling

using ResultType;
using ResultType.Errors;

Result successResult = Result.Success();
Result failureResult = Result.Failure(new Error("NotFound", "Item not found", 404));

if (failureResult.IsFailure)
{
    Console.WriteLine($"Error: {failureResult.Error?.Message}");
}

Working with Generic Results

var successWithValue = Result.Success("Hello, World!");
var failureWithValue = Result.Failure<string>(new Error("InvalidData", "The provided data is incorrect.", 400));

if (successWithValue.IsSuccess)
{
    Console.WriteLine(successWithValue.Value); // Outputs: Hello, World!
}

Working with Http Results

var successWithValue = Result.Success("Hello, World!");
var failureWithValue = Result.Failure<string>(new Error("InvalidData", "The provided data is incorrect.", 400));

IResult success = successWithValue.toHttpResult();
IResult failure = failureWithValue.toHttpResult();

Implicit Conversion

Result<string> result = "Implicit success"; // Automatically wraps value in Result.Success

ASP.NET Core Integration

Returning Result in Minimal APIs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ResultType;
using ResultType.Errors;

var app = WebApplication.Create();

app.MapGet("/user/{id}", (int id) =>
{
    if (id <= 0)
        return Result.Failure(new Error("InvalidId", "User ID must be positive", 400)).ToHttpResult();

    return Result.Success(new { Id = id, Name = "John Doe" }).ToHttpResult();
});

app.Run();

Error Handling

Custom Error Handling

Error error = new Error("Forbidden", "You are not authorized to access this resource.", 403);
Result failure = Result.Failure(error);

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.


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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1 170 a month ago