Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
Create an Email value object that encapsulates the validation in its constructor. If your validation is complex create the value object using a factory. –  Songo Jun 12 at 20:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.