DDD - Organization of Creation and Validation

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.

+5
source share
2 answers
  • factory . , . , ​​ jQuery, (, ). "" .   BaseEntity, , , :

    abstract class BaseEntity {
        public function isValid();
    }         
    
    class MyEntity extends BaseEntity {
         public function isValid() {
             //actual validation goes here
         }
     }
    

    :

    class ValidationHelper {
        public static function isValidPhonenumber($value) {
            //check valid phonenumber, using a regex maybe
        }
    
        public static function isAlphanumeric($value) {
            //check for letters and numbers only
        }
    }
    

    , , , , " ".

  • , , , , / ( ) a unique , , try-catch ( , ),

  • . 1 , , , , , . , , . , .

, , !

+4

- Doctrine2. , , 2, - .

PHP DDD Doctrine2 http://giorgiosironi.blogspot.com/ .

RE: - , . , - , . .

- .

+1

All Articles