if else

有什么好的解决方案
if (null == attend) {
throw new IncompleteStaffException(1);
}
if (null == password) {
throw new IncompleteStaffException(2);
}
if (null == name) {
throw new IncompleteStaffException(3);
}
if (null == corpMail) {
throw new IncompleteStaffException(4);
}
if (null == idCard) {
throw new IncompleteStaffException(5);
}
if (null == education) {
throw new IncompleteStaffException(6);
}
if (null == jobs) {
throw new IncompleteStaffException(7);
}
if (null == exp) {
throw new IncompleteStaffException(8);
}
if (null == department) {
throw new IncompleteStaffException(9);
}
if (null == state) {
throw new IncompleteStaffException(10);
}
if (null == dateEn) {
throw new IncompleteStaffException(11);
}

用hibernate作为持久层实现吗?
可以试试hibernate validation:

// In your User object

@Notnull
Object attend;

@Notnull
String password;

@Notnull
String name;
...

private boolean _staffValidation(Staff staff) throws RepeatAttendException,
RepeatCompanyEmailException, RepeatLicenseException,
RepeatIdentityCardException, IncompleteStaffException,
InvalidStaffProperty {
String method = "_staffValidation";

if (null == staff) {
return false;
}

// Required properties validation
String attend = staff.getAttend();
String password = staff.getPassword();
String name = staff.getName();
String corpMail = staff.getCorpMail();
String idCard = staff.getIdCard();
Integer education = staff.getEducation();
Integer jobs = staff.getJobs();
Integer exp = staff.getExp();
Integer department = staff.getDepartment();
Integer state = staff.getState();
Date dateEn = staff.getDateEn();

_logger.debug(method, "attend=" + attend);
_logger.debug(method, "password=" + password);
_logger.debug(method, "name=" + name);
_logger.debug(method, "corpMail=" + corpMail);
_logger.debug(method, "idCard=" + idCard);
_logger.debug(method, "education=" + education);
_logger.debug(method, "jobs=" + jobs);
_logger.debug(method, "exp=" + exp);
_logger.debug(method, "department=" + department);
_logger.debug(method, "state=" + state);
_logger.debug(method, "dateEn=" + dateEn);

// Null properties check
if (null == attend) {
throw new IncompleteStaffException(1);
}
if (null == password) {
throw new IncompleteStaffException(2);
}
if (null == name) {
throw new IncompleteStaffException(3);
}
if (null == corpMail) {
throw new IncompleteStaffException(4);
}
if (null == idCard) {
throw new IncompleteStaffException(5);
}
if (null == education) {
throw new IncompleteStaffException(6);
}
if (null == jobs) {
throw new IncompleteStaffException(7);
}
if (null == exp) {
throw new IncompleteStaffException(8);
}
if (null == department) {
throw new IncompleteStaffException(9);
}
if (null == state) {
throw new IncompleteStaffException(10);
}
if (null == dateEn) {
throw new IncompleteStaffException(11);
}

// Properties validation
if (!ValidatorUtil.isAttend(attend)) {
throw new InvalidStaffProperty(1);
}
if (!ValidatorUtil.isPassword(password)) {
throw new InvalidStaffProperty(2);
}
if (!ValidatorUtil.isName(name)) {
throw new InvalidStaffProperty(3);
}
if (!ValidatorUtil.isEmail(corpMail)) {
throw new InvalidStaffProperty(4);
}
if (!ValidatorUtil.isIdentityCard(idCard)) {
throw new InvalidStaffProperty(5);
}

//exp must between after has been born 15years later and dateEn
int dateEnYear = Integer.parseInt(DateUtil.parseString(dateEn).substring(0, 4));
int afterBirth15 = Integer.parseInt(idCard.substring(6, 10)) + 15;
if (exp < afterBirth15 || exp > dateEnYear) {
throw new InvalidStaffProperty(6);
}
if (!corpMail.endsWith("@cn.ufinity.com")) {
throw new InvalidStaffProperty(7);
}

boolean bool = _notRequiredPropertiesValidation(staff);
if (bool) {
_repeatStaffProperties(staff);
} else {
_logger.debug(method,
"Validation failure, Not required properties invalid!");
return false;
}
return true;
}
[该贴被xuezhongde于2009-01-19 17:15修改过]

我以为,没必要剥离validation到entity外面,validation logic是entity的一部分

PS,对你的这个


private boolean _staffValidation(Staff staff) throws RepeatAttendException,
RepeatCompanyEmailException, RepeatLicenseException,
RepeatIdentityCardException, IncompleteStaffException,
InvalidStaffProperty

我无语...

该验证是解析Excel后,将一行信息封装成Staff对象,要验证Staff的有效性,你的好方法应该是怎么处理?

给你一个具体的方案:
比如可以在你的Staff对象中使用hibernate validator来验证属性的有效性(要验证的属性上加上对应annotation即可)