2013-05-23 16:34 "@banq "的内容
就inserCustomer这个业务来看,应该是创建客户对象,那么校验应该是在这个客户对象的构造函数中。 ...
嗯,就是个新增customer的业务,不知道我这样子理解大侠您的意思是否正确,如果我要新增一个客户,要求客户必须要能提供name和email,那么就必须为Customer对象新增一个构造函数如下:
Customer(String name, String email){
this.name = name ;
this.email = email;
if(!this.isValid()){//在isValid中验证name和email是否合法
throw new BusinessException("不合法custommer对象");
}
}
考虑一下场景:如果通过以上构造方法新增了一个合法customer对象,但是customer却说要修改email地址,那么我必须得为customer对象的emai属性定义一个setter方法如下吗?
public void setEmail(String email){
String oldEmail = this.email;
this.email = email;
if(!this.isValid()){//在isValid中验证name和email是否合法
this.email=oldEmail;
throw new BusinessException("不合法custommer对象");
}
}
这时如果customer还有其他的一些属性如telephome,qq,那么我必须得为每个属性定义一个类似上面的setter方法吗?我感觉这样会显得很累赘,还是说我不在setter里校验,而是在业务方面里面调用校验方法如下:
public void updateCustomer(String id,String email,String telephone,String qq ){
Customer c = dao.reLoadCustomer(id);
String oldEmail = c.getEmai();
String oldTel = c.getTelephone();
String oldQq = c.getQq();
c.setEmail(email);
c.setTelephone(telephonte);
c.setQq(qq);
if(!c.isValid()){ //调用校验方法
c.setEmail(oldEmail);
c.setTelephone(oldTel);
c.setQq(oldQq);
throw new BusinessException(“不合法对象”);
}
}
[该贴被Evil于2013-05-24 10:59修改过]