Result.Net 1.2.0

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

// Install Result.Net as a Cake Tool
#tool nuget:?package=Result.Net&version=1.2.0                

Result.Net

a simple wrapper over an operation execution results to indicate success or failure, instead of throwing exceptions or returning false without explanation.

Quick setup

to get started install the package using the NuGet package manager Install-Package Result.Net.

Simple Usage

// to create success result
var successResult = Result.Success();

// to create a failure result
var failedResult = Result.Failure();

to create a result wrapper for a value you can use Result<TValue>

// to create success result with flout value
var successResult = Result.Success<float>(55.5);

// to create a failed result for a float value
var failedResult = Result.Failure<float>();

you can also add a message associated with the result

// to create success result with a message
var successResult = Result.Success<float>(55.5)
  .WithMessage("Operation Succeeded");

// to create a failed result with a message
var failedResult = Result.Failure<float>()
  .WithMessage("Operation Failed");

you can utilize error codes to give a better explanation of what happens (useful for machine-to-machine communication).

// create a failed result with a message and code
var failedResult = Result.Failure()
  .WithMessage("Othe provided email is not valid")
  .WithCode(ResultCode.InvalidEmail);

Note there is a list of predefined error codes that I use frequently, you can find them in ResultCode or you can simply use strings.

you can also provide a ResultError to better define what happened

var failedResult = Result.Failure()
  .WithMessage("Othe provided email is not valid")
  .WithCode("email_validation")
  .WithError(new []
  {
    new ResultError(
      message: "the email host is not allowed",
      code: ("invalid_email_host")
  });

if you want to encapsulate an exception you can do the following

public Result DoSomeWork()
{
  try
  {
    // you code goes here
    return Result.Success();
  }
  catch(Exception ex)
  {
    return Result.Failure()
      .WithMessage("an internal exception has been thrown")
      .WithCode(ResultCode.OperationFailedException)
      .WithError(ex);
  }
}

or you can use a pre-defined method to convert the exception to result object

public Result DoSomeWork()
{
  try
  {
    // you code goes here
    return Result.Success();
  }
  catch(Exception ex)
  {
    return ex.ToResult();
  }
}

what about logging! by default, if you created a Failed result a LogTraceCode will be generated, and you can include this tracing code in your logs to track the errors.

public Result DoSomeWork()
{
  try
  {
    // you code goes here
    return Result.Success();
  }
  catch(Exception ex)
  {
    var result = ex.ToResult();
      
    _logger.LogError(ex, "exception {@info}", new 
    {
      LogTraceCode = result.LogTraceCode
      // + your meta data about the error
    });
    
    return result;
  }
}

for more details check out the Wiki page.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (15)

Showing the top 5 NuGet packages that depend on Result.Net:

Package Downloads
Be.Vlaanderen.Basisregisters.AssociationRegistry

Association Registry

Indrivo.Audit.Accessor.Contracts

Package Description

Indrivo.Utilities.Result.Extensions

Package Description

Indrivo.FileStorage.Accessor.Contracts

Package Description

Indrivo.FileStorage.Accessor.Provider.MinIo

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.0 12,530 5/9/2023
1.4.0 24,557 11/10/2022
1.3.0 7,224 6/3/2022
1.2.1 1,460 12/6/2021
1.2.0 978 10/23/2021