SmartResults 1.0.4

dotnet add package SmartResults --version 1.0.4
 
NuGet\Install-Package SmartResults -Version 1.0.4
 
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="SmartResults" Version="1.0.4" />
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SmartResults" Version="1.0.4" />
 
Directory.Packages.props
<PackageReference Include="SmartResults" />
 
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 SmartResults --version 1.0.4
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SmartResults, 1.0.4"
 
#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.
#:package SmartResults@1.0.4
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SmartResults&version=1.0.4
 
Install as a Cake Addin
#tool nuget:?package=SmartResults&version=1.0.4
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

SmartResults

Lightweight .NET library to use result pattern instead of throwing exceptions.

License: MIT NuGet

How to use it

  • Result and Result<T> are structs to prevent memory allocation
  • Support for JSON
    • Integrated JsonConverter for Result and Result<T>
    • Polymorphic Error objects
  • Extensions for HttpResponseMessage
  • Chaining flow
  • Exception flow

Create and use results

Result result = Result.Ok();
if (result.IsSucceeded)
{
 Console.WriteLine("Succeeded");
}
else
{
 Console.WriteLine(result.Error);
}

Create and use results with value

Result<int> result = Result.Ok(100);
if (result.IsSucceeded)
{
 Console.WriteLine(result.Value);
}
else
{
 Console.WriteLine(result.Error);
}

Create and use failed results

Result<int> result = Result.Failed("Something is wrong!");
Result<int> result = Result.Failed(new Error(..));
if (result.IsFailed)
{
 Console.WriteLine(result.Error);
}

Use match to evaluate the result

Result<int> result = Result.Ok(100);
bool b = result.Match(value => true, error => false);

Explicit conversion

Result<string> result = Result.Ok(100).ToResult<string>(x => x.ToString());

Implicit operator for value

Result<int> result = 100;
int value = result;

Exception flow by implicit operator

int value = Create(); //throws exception if result is failed

Chaining flow

Result result1 = Create().Then(x => Console.WriteLine(x));
Result result2 = await Create().ThenAsync(async x => await Service.ExecuteAsync(x));
Result result3 = await CreateAsync().ThenAsync(x => Console.WriteLine(x));
Result result4 = await CreateAsync().ThenAsync(async x => await Service.ExecuteAsync(x));

HttpClient extensions

Result result1 = await httpClient.GetAsync("/").ReadResultFromJsonAsync();
Result<int> result2 = await httpClient.GetAsync("/").ReadResultFromJsonAsync<int>();

Supports polymorphic error objects for JSON

public class PermissionError : IError { }
Error.Register<PermissionError>();
Result result = Result.Failed(new PermissionError("Error"));
string json = JsonSerializer.Serialize(result);
Result result2 = JsonSerializer.Deserialize<Result>(json);
result2.Error //PermissionError
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. 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.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SmartResults:

Package Downloads
DragonFly.Core

Headless CMS based on ASP.NET Core and Blazor

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 1,133 4/23/2025
1.0.3 732 3/24/2025
1.0.2 2,573 6/11/2024
1.0.1 244 6/1/2024
1.0.0 214 6/1/2024