if else

09-01-19 xzdbruce
有什么好的解决方案

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);

}

    

dearshor
2009-01-19 16:31
用hibernate作为持久层实现吗?

可以试试hibernate validation:

// In your User object

@Notnull

Object attend;

@Notnull

String password;

@Notnull

String name;

...

xzdbruce
2009-01-19 17:14
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修改过]

dearshor
2009-01-19 17:49
我以为,没必要剥离validation到entity外面,validation logic是entity的一部分

PS,对你的这个

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

我无语...

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

猜你喜欢
2Go 1 2 下一页