Search code examples
phppdodomain-driven-designddd-repositoriesddd-service

Is an emailExist method allowed in UserService?


I'm using domain driven design.

I've got the following model (classes):

  • User
  • UserDAO
  • UserRepository
  • UserService

I know that UserService is supposed to contain all the corresponding application logic. So I got methods like signUp(), logIn() and update() in there.

My signUp() method, signs a user up, but throws a PDO exception when the email UNIQUE constraint has been violated. Now, since exceptions are for exceptional errors only and bad for control flow my question is:

Is an emailExist() method allowed to be in a UserService class?

So I can call that first in my controller (so I can report back with a form error if the email already exist), before actually signing a user up. I know such method actually belongs in the data mapper, but since they aren't supposed to be used directly in controllers I thought about having it added to my UserService class and from there map it to my repository's findByEmail() method.


Solution

  • Systems often expose commands and queries. SignUp, LogIn and Update are commands. FindByEmail is a query.

    User interfaces are there to try and guide a user to composing a valid command. Checking if an email already exists in the UI can provide nicer feedback to the user - allowing him to correct the command before sending it.

    Once you've done your best into guiding the user, you can just have your command throw an exception - without handling it in a clean way, because the chance that you get that far is now extremely small.