What does your Serialisation & validation pipeline look like?
Playing around with this again has me wondering how everyone else is handling this.
- are you using custom formatters, DTOs or a more manual decoder?
- How are you handling version tolerance?
- what are you using for validation and how usable is the feedback?
F# seems to have so many options for this problem, how is you’re stack, if you will, built and what do like/dislike about it?
E: And autocorrect's gone and used an s for serialization, it’s not wrong, but it feels wrong.
Posted Oct 20, 2020, 10:01 AM UTC · edited
We use FSharp.JSON for serialisation, helps with reducing manual conversions between domain models and DTOs. For example, it handles wrapper DUs, DUs with payloads (
| CreditCard of string | Cheque of int) and options out of the box.Reading pipeline looks like:
readEntity Id |> Result.bind extractDto |> Result.bind toDomainSo the whole pipeline uses railway approach and pipes the Result value through. ExtractDto cares about versions (if needed), but, generally, just unwraps persistence aware entity. ToDomain does the validation and, if successful, returns a trusted domain model.
Sometimes we need a consistent update, so the logic is chained as “read, then calculate a new value, then update with a version check”. Read and update are async, so the code starts sprawling. We found that FSToolkit.ErrorHandling helps a lot with its computation expressions, like taskResult.