Skip to content

Latest commit

 

History

History
72 lines (52 loc) · 1.89 KB

File metadata and controls

72 lines (52 loc) · 1.89 KB

RailwayOrientedProgrammingInCSharp

Railway Oriented Programming My Implementation In CSharp

SimpleUseCase

Error handling using ROP

alt text

Implementation of Either Monad using Interface

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);
}

Keeping all our errors in an single enum

public enum ErrorType
{
  NameCanNotBeBlank,
  EmailCanNotBeBlank,
  EmailNotValid,
  UserNotFound,
  DatabaseUpdateError,
  EmailNotSend
}

Example taken from Railway Oriented Programming