It is (was?) considered best practices when it come to web programming to let the database do the 'heavy lifting', when possible. For example, I have a contact list, and it consists of many emails addresses and name. Each email must be unique, so one potentially bad solution for an active record model (Eloquent) is this case will be:

 public function addEmail($name, $address)
    if ($this->where('name', $name)->where('address', $address)->count() > 0)
  throw new \Exception("Name and email already exists!");

 // perform add new logic


If what I have understood is correct, I shouldn't be doing this -- that the entities should be plain data objects, and unable to make calls out to the DB.

In that case, how should I enforce that bit of validation? Use a service, or load in all the addresses for the ContactList, or move it somewhere else (an AddNewEmailToConactList service?)

  • 2
    Actually, what you're describing (the "anemic data pattern") is sometimes considered an antipattern. Note that DDD is not a development technique; it is a design technique. You're allowed to do anything you want in software development, so long as it solves the problem in a way that is easily understood, maintainable, and has adequate performance characteristics. Sep 1, 2016 at 14:38
  • I've clarified my question; I know that the example code is considered 'bad' by DDD standards. The question is - do I have to load in all the existing email addresses that the contact list have, and check them in-memory without the help of the database for it to be considered 'correct'?
    – Extrakun
    Sep 1, 2016 at 16:34
  • There is no such thing as "correct." There is only that which best meets your specific requirements and tradeoffs. Sep 1, 2016 at 16:34
  • True, let's try "what are the possible recommendations or alternatives", or will that be too subjective for SO?
    – Extrakun
    Sep 1, 2016 at 16:35
  • What technique do you think would best solve your problem? Sep 1, 2016 at 16:36

1 Answer 1


You have a look at the business-rules that govern your domain, and you find the one that will cause you a problem, and you validate it at the point where you might break it.

So if the rule is There can be no duplicate email addresses in the contact list then you would validate it when you add the new Entry to the ContactList.

The ContactList itself will most likely have a method AddNewEntry which will do the check, and the ContactList either is or has a database table that it can query to check if the name already exists.

It's perfectly valid to have multiple copies of the same email addresses in this case; they just can't all go on the ContactList. There could even be multiple ContactLists in which case the above code would still work, with each list checking its own contents. But it really depends on the exact domain you're working from.

  • Thanks for your answer; how could an entity query a database, if it is to be just a plain data object?
    – Extrakun
    Sep 1, 2016 at 16:45
  • You should use an UseCase to enforce the validation, for example AddNewEntry, this could be part of your service. He can talk to your external DB system via an adapter to avoid being tangled with the DB itself, and in there you can enforce the validation any way you want. You could retrieve the ContactList and check that the email is not part of the received list, or try asking the adapter to add the email to the database, and check if it returns an error. You end up with a plain object entity, your database could do the heavy lifting if you want to. Check out bit.ly/2a1px8t
    – cllamach
    Sep 1, 2016 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.