What should a domain object's validation cover?
- by MarcoR88
I'm trying to figure out how to do validation of domain objects that need external resources, such as data mappers/dao
Firstly here's my code
class User
{
const INVALID_ID = 1;
const INVALID_NAME = 2;
const INVALID_EMAIL = 4;
int getID();
void setID(Int i);
string getName();
void setName(String s);
string getEmail();
void setEmail(String s);
int getErrorsForInsert(); // returns a bitmask for INVALID_* constants
int getErrorsForUpdate();
}
My worries are about the uniqueness of the email, checking it would require the storage layer.
Reading others' code seems that two solutions are equally accepted: both perform the unique validation in data mapper but some set an error state to the DO user.addError(User.INVALID_EMAIL) while others prefer to throw a totally different type of exception that covers only persistence, like:
UserStorageException
{
const INVALID_EMAIL = 1;
const INVALID_CITY = 2;
}
What are the pros and cons of these solutions?