Useful opinionated domain components and/or helper functions for .NET solutions.
Classes and interfaces to help with functional programming in C#.
- NuGet package: ToolPack.DomainUtils.Functional
This is a simple implementation of the Result pattern in C#.
The package contains Result<T>
and Result
(i.e. Result<Success>
) classes, used to represent the result of an operation that can succeed or fail:
- In case of success, the
Value
property (of typeT
) will contain the successful output of the operation, - In case of failure, the
Error
property will contain theError
output of the operation.
The
Error
class represents operation errors with all their details.
Useful references:
- The ServiceResult Pattern
- Functional C#: Handling failures, input errors
- Improving API error responses with the Result pattern
- What's the Result Type Everyone Is Using in .NET?
The package contains a set of extension methods to help with the handling of Result
instances:
Execute
/ExecuteAsync
execute aResult
function or action if the input result is successful, or return the initialError
in case of input failure;Log
methods log the output of the inputResult
instance, depending on the input result being a success or error;Map
/MapAsync
methods apply functions of generic return type, depending on the input result being a success or error;Pipe
/PipeAsync
methods apply aResult
function to theValue
of an inputResult<T>
instance if it is successful, or return the initialError
in case of input failure;Switch
/SwitchAsync
methods run actions, depending on the input result being a success or error.
These methods are useful to chain operations and handle the output of each operation in a functional way.
They intend to help with the implementation of the Railway Oriented Programming (ROP) pattern.
Useful references: