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.
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 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.ListSuccess<>()
a static method to create a Result object for a list with a success status
// create a success result instance for a list result
ListResult<float> successResult = Result.ListSuccess<float>(new float[] { 1.1, 2.2, 3.3 });
- Result.Failure()
a static method on the Result class this function is your entry 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.ListFailure<>()
a static method to create a Result object for a list with a Failed status
// create a failure result instance for a list result
ListResult<float> failureResult = Result.ListFailure<float>();
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 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 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 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 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")),
});
- .WithMataData()
use this function to add a 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 a result error to it
var result = Result.Failure()
.WithMataData("email", "[email protected]")
.WithMataData("email_host", "onvalid.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, pram2);
// 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 meta-data
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 ...
}
// get the execution result of some operation with a list return type
var listResult = someOperation(param1, pram2);
// check if the list is empty
if (listResult.IsListEmpty()) {
// 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";
});