SmartResults 1.0.0

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

// Install SmartResults as a Cake Tool
#tool nuget:?package=SmartResults&version=1.0.0

SmartResults

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

License: MIT

How to use it

  • Result and Result{T} are structs to prevent memory allocation
  • Integrated JsonConverter
  • Extensions for HttpResponseMessage
  • Chaining flow
  • Exception flow

Create and use results

Result result = Result.Ok();

if (result.IsSucceeded)
{
   Console.WriteLine("Completed");
}
else
{
   Console.WriteLine(result.Error.Message);
}

Create and use results with value

Result<int> result = Result.Ok(100);

if (result.IsSucceeded)
{
   Console.WriteLine(result.Value);
}

Create and use failed results

Result<int> result = Result.Failed("Something is wrong!");
Result<int> result = Result.Failed(new Error(..));
Result<int> result = Result.Failed(new Exception());

if (result.IsFailed)
{
	Console.WriteLine(result.Error.Message);
}

Use match to evaluate the result

Result<int> result = Result.Ok(100);

bool b = result.Match(value => true, error => false);

Implicit conversion

Result<int> result = 100;

int value = result;

Use exception flow by implicit operator

int value = Create(); //throw 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));

Use HttpClient extensions

Result result1 = await httpClient.GetAsync("/").ToResultAsync();

Result<int> result2 = await httpClient.GetAsync("/").ToResultAsync<int>();
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.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 79 6/11/2024
1.0.1 95 6/1/2024
1.0.0 88 6/1/2024