Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:08:06+00:00 2026-05-29T09:08:06+00:00

I’m interested in DDD (Domain Driven Design) in the last days, but I can’t

  • 0

I’m interested in DDD (Domain Driven Design) in the last days, but I can’t figure out the responsibilities of who creates and validates the entities. Ill break this questions to cover different scenarios.

  1. Regular entity (Possibly with value object). As an example lets take a user who is identified by Email. I have a UserFactory that receives an array of data (possibly from form POST), and return me a new UserEntity. Should the factory validate the integrity of the data (ex: string given as Email is a real email, passwords in password field 1 and field2 match and etc)? Should the factory validate that no such user exists already (we dont want to register two users with the same email)? If yes should it do it my itself or using the UserRepository?

  2. Aggregate entity. Lets assume we have a Post entity and Comments entities. I want to get post 12 with all its comments, so I do something like

    $post = $postRepository->getById(12);

How getById should be implemented? like this:

public function getById($id) {
    $postData = $this->magicFetchFromDB($id);
    $comments = (new CommentRepository())->getForPost(12);
    return new PostEntity($postData, $comments);
}

Or maybe the post responsible for lazy creating its comments, something like:

class PostEntity {
    public function getComments() {
        if(is_null($this->_comments)) $this->_comments = (new CommentRepository())->getForPost($this->_id);
        return $this->_comments;
    }
}

?
I’m very lost here and there is not enough information with examples for DDD in PHP, so any help will be appreciated!

Thank you a lot,
skwee.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:08:08+00:00Added an answer on May 29, 2026 at 9:08 am
    • The factory should only care about creating entities. I personally preferr to put validation both in my views and in the model layer. I’d use some library like jQuery’s validation plugin to make some essential validation on the client side (like checking if required fields have data). And then do the “hardcore” validation on the model.
      I do this is by using a simple BaseEntity abstract class which all entities extend, and since you asked for an example, here it is:

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

      You could also make use of a static helper class with some basic validation methods:

      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
          }
      }
      

      Many argue against static methods because they can break unit tests, but in this case they are pretty basic and they don’t have external dependencies, thus making them “safer”.

    • When it comes to checking for already existing entities you can do this by querying your DB to see if the entity is already there before adding/updating, or (this is how I like to do it) you could add a unique index to those columns that can’t be repeated in the DB, and then wrap the create or update queries in a try-catch block (the query will throw a unique constraint violation, if two users have same e-mail for example) and then show the proper error message

    • On your last question it comes down to a matter of preference. If your DB will get a million hits in 1 minute, then it’s probably better to use lazy loading to avoid fetching unnecesary data until it’s needed. But if your database is relatively small you could perfectly use eager loading without sacrificing too much performance.
      Again, this is a matter of personal preference.

    hope some of this rambling makes sense, cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm interested in microtypography issues on the web. I want a tool to fix:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.