Railway
Portable library for using railway oriented programming in C#. Heavily inspired by http://fsharpforfunandprofit.com/posts/recipe-part2/, see the site for information about the theory and how railway oriented programming works.
Documentation
Result
The basis of everyhting. Results are created like this:
// Success result
Result<string, int>.CreateSuccess("Success")
// Failed result
Result<int, string>.CreateFailure("Failure")
Composing
Composing functions (all functions must return Result<TSucces, TFailure>
) is done like this:
Validate1().Compose(Validate2)
// or using Func
Validate1().Compose(success => Result<string, string>.CreateSuccess("Success"))
Composing with "dead-ends", meaning functions returning void, is done the same way, except an Action
is used instead of a Func
:
Validate1().Compose(success => { })
Double mapping
Double mapping, means running a function depending on the result. The functions must return object of the same type as the original input.
Validate1().DoubleMap(success => success, failure => failure)
Switch
Switches are adapters for functions not accepting any input.
Switch.Create(() => "Success")
A basic switch always returns object as failure type.
A switch can also handle exceptions thrown by the function:
Switch.CreateWithException(() => { throw new Exception(); return "Success"; })
The returning result will be a failed result containing the thrown exception.