What should a domain object's validation cover?

Posted by MarcoR88 on Programmers See other posts from Programmers or by MarcoR88
Published on 2014-06-12T18:42:01Z Indexed on 2014/06/12 21:37 UTC
Read the original article Hit count: 268

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?

© Programmers or respective owner

Related posts about design-patterns

Related posts about domain-driven-design