-
Notifications
You must be signed in to change notification settings - Fork 11
Description
When I started decoupling from Symfony Form and the EntityType I switched to primitive types only that would then be used on the data_class which is the Command / DTO.
In conlusion my command only uses integers (for IDs) and strings (also UUID) or arrays of these types.
I'm sometimes asked if objects e.g. value objects should be used. As I mentioned I don't use objects in a DTO at all. As @jeremyb states in his slides:
Tips #3: Command & Value Object
Il est recommandé d'utiliser des types primitifs si la Command doit être traitée en asynchrone (à cause de la serialization)
Passer des Values Objects à une Command permet de réutiliser un cadre et les validations définies
Most examples I see follow these "guidelines":
https://github.com/SimpleBus/MessageBus/blob/master/doc/command_bus.md#using-the-command-bus-an-example @matthiasnoback
Sometimes a DTO property transfers a string coming from a constant:
class ContractState
{
const STATE_ACCEPTED = 'contract_accepted';
const STATE_APPROVED = 'contract_approved';
const STATE_QUOTE_SUBMITTED = 'quote_submitted';
const STATE_IN_PROGRESS = 'in_progress';
const STATE_PENDING = 'pending';
const STATE_CLOSED = 'closed';
}People ask how to ensure (validate) that the string $contractState of the DTO is a correct string from the constants.
If you are using Symfony Form you already set the choices and a user cannot select a different choice.
When the input comes directly from the Request I guess you could add a custom Constraint to the Command that could check if the strings exists:
Since I'm using Symfony Form I prefer validating the input inside the Value Object. The handler will try to create an object from String:
ContractState::fromString($command->contractState());The fromString method then will throw an Exception if the string was invalid.
What are your thoughts on this?
Related issues: