Having read what feels like hundreds of articles on the correct location for Validation I settled on placing superficial validation within my Command objects.
My Command object contains a combination of required and optional information used to construct my domain object.
class Command
{
/*
| Required stuff
*/
private $req1;
private $req2;
/*
| Optional stuff
*/
private $opt1;
private $opt2;
private $opt4;
private function __construct($req1, $req1, $opt1, $opt2, $opt3)
{
// perform superficial validation & set if OK
$this->setReq1($req1);
// ...
}
}
For objects which have a large number of parameters (required and/or optional), you might make use of the builder pattern rather than having lengthy constructors. I could use a builder for my command, however, I'm not sure this is the correct approach?
Is the builder pattern appropriate here or should I simplify the command to be more DTO like (public attributes) and perform the validation in the command handler? Or is there another option?
Aucun commentaire:
Enregistrer un commentaire