Simple monad for handling failures as an alternative to the exception driven code
This monad can be used as an alternative to the exception driven code which is hard to navigate and maintain. With this package you get
- Visibility over all possible failures
- Fluid handling with extension methods
Install with NuGet package manager
Install-Package FailableResult.NetCore
Following code demonstrates usages of the package
void Main()
{
var data = ...;
SaveData(data)
.OnSuccess(success => ...)
.OnFailure(failure => switch(failure)
{
case FailureType.Timeout: ...
default: ...
});
}
IFailableResult<object, FailureType> SaveData(object data)
{
if (IsDataInvalid(data))
return FailureResult<object, FailureType>.Create(FailureType.InvalidData);
...
return SuccessResult<object, FailureType>.Create(data);
}
enum FailureType
{
InvalidData,
Timeout,
ServiceUnavailable,
Other
}
For other use cases see unit tests