When I am writing a builder, if the entity I am building contains value objects, what is the best way to deal with that?
Should I have a method called something like With_MyvalueObject(IMyValueObject $vo) and simply set the VO that way - obviously this technique requires that the value object exists first so I can pass it as an argument. Or should I make basic than that and take in values to create the value object within the builder?
Either way will be fine. I'm personally passing value objects to factories/builders as it's more concise.
Anyway I'm trying to map things as close to UL(ubiquitous language) as I can, so if it wouldn't be a builder, but in example method user.commentOnPost
, then I pass basic values since existing of comment
has no sense until user comments on something. So instead of doing:
comment = Comment(text, author, post)
commentRepository.save(comment)
I do:
user.commentOnPost(text, post)
Internally it creates comment in the same way as above, but it is created when user actually is commenting, not before, so it makes more sense.