Railway Oriented Programming My Implementation In CSharp
Error handling using ROP
public interface IResult<T>
{
TResult Match<TResult>(
Func<T, TResult> onSuccess,
Func<ErrorType, TResult> onError
);
IResult<TResult> Map<TResult>(Func<T, TResult> f);
IResult<TResult> Bind<TResult>(Func<T, IResult<TResult>> f);
}
public class Success<T> : IResult<T>
{
private readonly T _results;
public Success(T results)
{
_results = results;
}
public TResult Match<TResult>(Func<T, TResult> onSuccess, Func<ErrorType, TResult> _) => onSuccess(_results);
public IResult<TResult> Map<TResult>(Func<T, TResult> f) => new Success<TResult>(f(_results));
public IResult<TResult> Bind<TResult>(Func<T, IResult<TResult>> f) => f(_results);
}
public class Error<T> : IResult<T>
{
private readonly ErrorType _error;
public Error(ErrorType error)
{
_error = error;
}
public TResult Match<TResult>(Func<T, TResult> _, Func<ErrorType, TResult> onError) => onError(_error);
public IResult<TResult> Map<TResult>(Func<T, TResult> _) => new Error<TResult>(_error);
public IResult<TResult> Bind<TResult>(Func<T, IResult<TResult>> _) => new Error<TResult>(_error);
}
public enum ErrorType
{
NameCanNotBeBlank,
EmailCanNotBeBlank,
EmailNotValid,
UserNotFound,
DatabaseUpdateError,
EmailNotSend
}
Example taken from Railway Oriented Programming