Helper methods - YoussefSell/Result.Net GitHub Wiki

You will find several helper functions, both static and extension methods, that will help you create your result object instance in a chaining manner.

1. Static Methods:

You can use a list of static methods to create a Result Object instance with both Success and Failed status.

- Result.Success()
A static method on the Result class. This function is your entry point to create a Result instance with a Success status.

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

- Result.Success<>()
A generic overload on the Result.Success() method to create a Result<TData> instance with a Success status and a value.

// create success result instance with a float value
Result<float> successResult = Result.Success<float>(1.1);

- Result.Failure()
A static method on the Result class. This function is your entry point to create a Result instance with a Failed status.

// create failure result instance
Result failureResult = Result.Failure();

- Result.Failure<>()
A generic overload on the Result.Failure() method to create a Result<TData> instance with a Failed status and a value.

// create a failure result instance with a float value
Result<float> failureResult = Result.Failure<float>();

- Result.Configure()
The configuration of the result object.

Result.Configure(config => {
  // here goes the configuration
});

2. Extension Methods:

A list of extension methods that you can use to customize the Result object instance.

- .WithMessage()
Use this function to add a message to the Result Object.

// create a failure result and add a message to it
var result = Result.Failure()
     .WithMessage("Failed to process the email");

- .WithLocalizedMessage()
Use this function to add a localized message to the Result Object.

// create a failure result and add a message to it
var result = Result.Failure()
     .WithErrorCode("invalid_email_host")
     .WithLocalizedMessage();

For more information, check out the Working With Localization page.

- .WithCode()
Use this function to add an error code to the Result Object.
There is a list of predefined error codes that you can use: ResultCode.

// create a failure result and add an error code to it
var result = Result.Failure()
     .WithCode("validation_failed");

// or
var result = Result.Failure() 
     .WithCode(ResultCode.ValidationFailed);

- .WithLogTraceCode()
Use this function to add a log trace code to the Result Object.
Useful to locate the errors in your logs.

// create failure result and add a log trace code to it
var result = Result.Failure()
     .WithLogTraceCode("1TrEcZIDd938DsJZ948");

- .WithError() Use this function to register a single error.

// attach a result error
var result = Result.Failure()
     .WithError(new ResultError(
         message: "Email host is not allowed",
         code: "invalid_email_host"));

// attach a result error with a shorter version
var result = Result.Failure()
     .WithError("Email host is not allowed", "invalid_email_host");

// attach a result error with an exception
var result = Result.Failure()
     .WithError(new Exception("This is a test exception"));

- .WithErrors()
Use this function to add error objects to the Result Object.

// add a list of errors
var result = Result.Failure()
     .WithErrors(new []
     {
          new ResultError(
               message: "Email host is not allowed",
               code: "invalid_email_host"),

          new ResultError(new Exception("This is a test exception")),
     });

- .WithMetaData()
Use this function to add key/value metadata to the Result Object.
Useful to add some context about the execution results.
The value property is of type object so that it can be any type.

// create failure result and add metadata to it
var result = Result.Failure()
     .WithMetaData("email", "[email protected]")
     .WithMetaData("email_host", "nonvalid.com");

Here you can find a list of extension methods used to check the result object status:

// get the execution result of some operation
var result = someOperation(param1, param2);

// to check if the result is a failure
if (result.IsFailure()) {
      // your code ...
}

// to check if the result is a success
if (result.IsSuccess()) {
      // your code ...
}

// to check if the result has any errors
if (result.HasErrors()) {
      // your code ...
}

// to check if the result has any metadata
if (result.HasMetaData()) {
      // your code ...
}

// to check if the result has failed because of a given error code
if (result.FailedBecause("invalid_email_host")) {
      // your code ...
}

- .Match()
Use this extension method to match the status of the result and execute an Action based on the success or the failure status.

var result = DoSomeWork();

// or you can use the Match extension
result.Match(
      // the action to run on success
      onSuccess: result => {
        Console.WriteLine("Executed if result has a success status");
      },
      // the action to run on failure
      onFailure: result => {
        Console.WriteLine("Executed if result has a failure status");
      });

- .Match<TResult, TOut>()
Use this extension method to match the status of the result and execute a function based on the success or the failure status and return a value.

var result = DoSomeWork();

// you can also return a value
var output = result.Match(
      // the func to run on success
      onSuccess: result => {
        Console.WriteLine("Executed if result has a success status");
        return "test";
      },
      // the func to run on failure
      onFailure: result => {
        Console.WriteLine("Executed if result has a failure status");
        return "test";
      });
⚠️ **GitHub.com Fallback** ⚠️