1

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.

Kid Diamond
  • 2,232
  • 8
  • 37
  • 79
  • Not sure where you got the idea that exceptions are only for "exceptional error". Exceptions are excellent to report any deviation from the expected execution flow back to a caller without using out and ref parameters. You can specify granular exceptions for any fallback scenarios that you need and handle only those, re-throw anything else. And this "anything else" will be your "exceptional errors" flow. – Alexey Zimarev Jul 17 '14 at 11:29

1 Answers1

3

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.

JefClaes
  • 3,275
  • 21
  • 23