YoussefSell.Result.Net 1.2.0

A Result Object for better error handling.

Install-Package YoussefSell.Result.Net -Version 1.2.0
dotnet add package YoussefSell.Result.Net --version 1.2.0
<PackageReference Include="YoussefSell.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 YoussefSell.Result.Net --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: YoussefSell.Result.Net, 1.2.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install YoussefSell.Result.Net as a Cake Addin
#addin nuget:?package=YoussefSell.Result.Net&version=1.2.0

// Install YoussefSell.Result.Net as a Cake Tool
#tool nuget:?package=YoussefSell.Result.Net&version=1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 YoussefSell.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.

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 YoussefSell.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.

Release Notes

https://github.com/YoussefSell/Result.Net/releases

  • .NETStandard 2.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 History

Version Downloads Last updated
1.2.0 80 8/2/2021
1.1.0 63 7/28/2021
1.0.0 76 7/23/2021