A nice approach for validation I’ve seen recently is to make DTOs validate themselves inside their constructors so it is impossible to construct an invalid DTO. See example below.
To do the above requires every instance of a DTO to build itself a new instance of a validator on construction. For a typical webapp such DTOs could be of course be built 100s of times a second. Is building this many instances of validators recommended / performant? Is the factory effectively passing the same instance every time in any case so it doesn’t matter how many times it is called?
Thanks for any help with this