TomRR.Core.ResultType
0.0.2
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
<PackageReference Include="TomRR.Core.ResultType" Version="0.0.2" />
<PackageVersion Include="TomRR.Core.ResultType" Version="0.0.2" />
<PackageReference Include="TomRR.Core.ResultType" />
paket add TomRR.Core.ResultType --version 0.0.2
#r "nuget: TomRR.Core.ResultType, 0.0.2"
#addin nuget:?package=TomRR.Core.ResultType&version=0.0.2
#tool nuget:?package=TomRR.Core.ResultType&version=0.0.2
π¦ 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 | Versions 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. |
-
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.
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.