Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: How do you handle web Form/DTO/Model/DB?
2 points by yawn on Oct 14, 2021 | hide | past | favorite | 1 comment
I'm curious how folks handle receiving a POSTed web form whose data gets inserted into the database, especially in typed languages/frameworks. Do you use Form structs/objects, DTOs, and Models? How do you handle validation? How do you put the pieces together? I'm working on a personal project in Rust+Tide and I've taken for granted how "easy" some web frameworks have made this.



My personal experience is that there are two kinds of forms one where both read and write models are the same and one where they are not. Most form apps diverge in their read and write models as they grow bigger so it's always a good idea to make separate.

For validation again there's two kinds of validations, one is static like age should be integer and greater than 0, and one dynamic like this date should be between these two dates from some table.

You want to make simple validations be done for you by just specifying the types which some library can use to perform the validation.

More complex validation that cannot be expressed via types should be in your domain models constructor.

So usually there's a form struct and a separate domain struct. The form struct is filled from request and then you use that to build up ur domain object. You always want to be thinking about parsing form data and not merely validating it, that is collecting form data and constructing a proven valid domain model/entity from it before using it for any business logic




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: