Suppose I need to register user in my system.
Buisness rules are:
- email shoud be unique (a kind of identity);
- name shouldn't be blank.
It looks like I need Service for it.
Probably something like that:
public interface RegistrationService {
bool Register(String email, String name);
}
And it's fine until I have to return failure reason to user. How to deal with it?
I can see few options (but I don't like any of them):
Implement a kind of result object:
public interface RegistrationService { RegistrationResult Register(String email, String name); } public interface RegistrationService { bool Succes(); Error[] Errors(); User NewUser(); }
It's fine, and even could be useful for example for REST api. But isn't it too cumbersome(especially considering that blank name probably should be checked at factory)?
Throw exceptions
public interface RegistrationService { void Register(String email, String name) throws RegistrationError; }
It looks a bit more accurate. But exception are expensive. Using them like this is looks like bad idea.
- Use DB constraint. But it looks even more messy than (2).