I’ve been interested in DDD (Domain Driven Design) in recent days, but I can’t understand who creates and validates entities. I break these questions to cover different scenarios.
A regular object (possibly with an object value). An example is a user who is identified by email. I have a UserFactory that gets an array of data (possibly from a POST form) and returns me a new UserEntity. If the factory checks the integrity of the data (for example: the line indicated as email is the real email address, passwords in password field 1 and field2 correspond, etc.)? If the factory confirms that such a user no longer exists (we do not want to register two users with the same email address)? If so, should he do it himself or use UserRepository?
Aggregate entity. Suppose we have Post and Notes objects. I want to get message 12 with all its comments, so I am doing something like
$ post = $ postRepository-> getById (12);
How should getById be implemented? eg:
public function getById($id) {
$postData = $this->magicFetchFromDB($id);
$comments = (new CommentRepository())->getForPost(12);
return new PostEntity($postData, $comments);
}
Or maybe the post responsible for lazily creating your comments, something like:
class PostEntity {
public function getComments() {
if(is_null($this->_comments)) $this->_comments = (new CommentRepository())->getForPost($this->_id);
return $this->_comments;
}
}
? I am very lost here and there is not enough information with examples for DDD in PHP, so any help would be appreciated!
Thank you so much, skwee.
source
share