Skip to content
Hamed ZVand edited this page Sep 17, 2019 · 3 revisions

Result

The Result data type is a container for an element of the given type or null.

It's created for this purpose to return more (useful) information to the (service) caller. Like for something that can partially succeed, something calling across a network boundary, and provide further information about error or warning consisting of StatusCode and Message.

Samples:

private Result<Foo> ExternalService()
{
     // do some network comunication and return value
     var value = new Foo("bar");

     return Result<Foo>.Okay(value);
     // return Result<Foo>.Exception(new TimeoutException());
     // return Result<Foo>.Error("something bad happend");
}

public Foo Demo1()
{
    var result = ExternalService();

    if(result)
    { return result.Data; }
    else
    {
        _logger.LogError(result.Message);
        return NotFound();
    }
}

more samples for Result[T] and Result

Clone this wiki locally