Lightweight .NET library to use result pattern instead of throwing exceptions.
Result
andResult<T>
are structs to prevent memory allocation- Support for JSON
- Integrated JsonConverter for
Result
andResult<T>
- Polymorphic
Error
objects
- Integrated JsonConverter for
- Extensions for HttpResponseMessage
- Chaining flow
- Exception flow
Result result = Result.Ok();
if (result.IsSucceeded)
{
Console.WriteLine("Succeeded");
}
else
{
Console.WriteLine(result.Error);
}
Result<int> result = Result.Ok(100);
if (result.IsSucceeded)
{
Console.WriteLine(result.Value);
}
else
{
Console.WriteLine(result.Error);
}
Result<int> result = Result.Failed("Something is wrong!");
Result<int> result = Result.Failed(new Error(..));
if (result.IsFailed)
{
Console.WriteLine(result.Error);
}
Result<int> result = Result.Ok(100);
bool b = result.Match(value => true, error => false);
Result<string> result = Result.Ok(100).ToResult<string>(x => x.ToString());
Result<int> result = 100;
int value = result;
int value = Create(); //throws exception if result is failed
Result result1 = Create().Then(x => Console.WriteLine(x));
Result result2 = await Create().ThenAsync(async x => await Service.ExecuteAsync(x));
Result result3 = await CreateAsync().ThenAsync(x => Console.WriteLine(x));
Result result4 = await CreateAsync().ThenAsync(async x => await Service.ExecuteAsync(x));
Result result1 = await httpClient.GetAsync("/").ReadResultFromJsonAsync();
Result<int> result2 = await httpClient.GetAsync("/").ReadResultFromJsonAsync<int>();
public class PermissionError : IError { }
Error.Register<PermissionError>();
Result result = Result.Failed(new PermissionError("Error"));
string json = JsonSerializer.Serialize(result);
Result result2 = JsonSerializer.Deserialize<Result>(json);
result2.Error //PermissionError