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.

OK, say I have a data class for a user:

public class User {
private String firstName;
private String lastName;
private long personCode;
private Date birthDate;
private Gender gender;
private String email;
private String password;

Now let's say I want to validate email, whether names are not empty, whether birth date is in normal range, etc. Can I put that validation method in this class together with data? Or should it be in UserManager which in my case handles the lists of these users?

share|improve this question
    
What language are you coding in? –  hbrock Oct 23 '12 at 21:53
3  
it is not a good practice to have the validation in you'r domain classes. If you want the data to be validated on the client side it would be better to create a viewmodel and user the data annotations attribute in the view model. Or create a separate layer for validating your data before saving it to the database –  CoffeeCode Oct 23 '12 at 21:57
6  
@CoffeeCode I disagree. The domain must be valid and consistent regardless of the client, therefore it must also perform its own validation. –  Andres F. Oct 23 '12 at 23:19
1  
@user1598390: that regex will reject a great many valid email addresses. Email validation is hard. –  Carson63000 Oct 24 '12 at 3:49
1  
@Carson63000: Correction: email validation is easy... just send the customer an e-mail. –  Steve Evers Oct 24 '12 at 4:04
show 3 more comments

4 Answers

up vote 4 down vote accepted

You can definitely validate these attributes in your setters(). Unlike getters which are meant to return values, setters are methods to assign a value via an argument/parameter.

Ideally, i would advise you to write separate methods and have their return values true or false. for validating your class attributes particularly when the validation is quite extensive.

Examples:

bool validateEmail(string email) {

}

bool validatePassword(string pwd) {

}

bool validateBirthdate(date dob) {

}

I would rather advise to validate your class attributes in the Business Logic Layer. I wonder if you know about the software logical architecture. FYI: BLL

Also if you have a database then it is best to have them validated there via constraints.

share|improve this answer
add comment

If you are coding in Java, you should take a look at Bean Validation, that is part of the Java EE 6 specification. It uses metadata (annotations) to help validation handling. Bean validation can also be used in other layers.

Hope it helps you.

share|improve this answer
add comment

In short, YES you can definitely implement that validation method on a class.

I would also advice to define validation method (or methods) through a common interface like IValidate. It will look somewhat like:

public class User: IValidate 
{}

You may define the generic public bool IsValid() method signature and others that make sense in the given context.

share|improve this answer
    
Shouldn't the setter rise an exception when passed a valud that doesn't pass the validation ? –  user61852 Oct 24 '12 at 1:07
add comment

Don't put the code that actually does the validation logic in with the class itself - see SRP principle. And what if you were to code some validation for something that might be useful to another class (for example email validation)? Extract each type of validation logic out to its own separate class.

share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.