模型层的业务规则
Business rules on model layer
在我的 class 中,我需要验证并保存移动状态。我不知道把这个验证放在哪里。我想我最好把它放在模型层而不是我的bean中。
我正在这样做:
1 - 运动
@SuppressWarnings("serial")
@Entity
public class Movimentacao implements Serializable, Entidade {
...
@Column(nullable=false)
@NotNull
@DecimalMin("0.01")
private BigDecimal valor;
@Column(nullable=false)
@NotNull
@DecimalMin("0.01")
private BigDecimal valorQuitado;
@Enumerated(EnumType.STRING)
@Column(nullable=false, length=1)
private MovimentacaoStatus status;
...
public void setStatus(MovimentacaoStatus status) {
this.status = status;
}
}
2 - form.xhtml
<!-- show only on edit mode (status not null) -->
<h:outputText id="status" value="#{movimentacaoBean.movimentacao.status.descricao}" rendered="#{movimentacaoBean.movimentacao.status ne null}" />
3 - MovimentacaoBean
public String salvar() throws Exception{
movimentacaoService.salvar(movimentacao);
this.movimentacao = null;
this.todos = null;
context.addMessage(null, new FacesMessage("Movimentação salva com sucesso", ""));
context.getExternalContext().getFlash().setKeepMessages(true);
return "pretty:financeiro-lista";
}
用户没有定义状态。我应该把验证放在哪里?在 setStatus?
如果我将 setStatus 更改为(例如):
public void setStatus() {
//example. The real Business rules are other.
this.status = MovimentacaoStatus.P;
}
或
public void setStatus(MovimentacaoStatus status) { //status variable never used...
//example. The real Business rules are other.
this.status = MovimentacaoStatus.P;
}
我收到以下错误(因为 MovimentacaoBean 没有收到来自 form.xhtml 的状态):
Caused by: java.sql.SQLIntegrityConstraintViolationException: Column
'status' cannot be null
我应该如何以及在何处放置状态业务规则?当我编辑记录时,同样的问题也适用。根据 "valor" 和 "valorQuitado" 状态可能会改变。编辑模式的区别在于状态属性在 form.xhtml 上可见(只读 - outputText)
视情况而定。我通常把它放进豆子里。但是在服务层中进行检查以及使用异常也是有意义的。 (如果您计划不同的前端等)
缺点可能是,如果你有很多验证并且你想向最终用户提供正确的信息,它可能会有点棘手。
因此:我建议两个地方都检查一下。
在 bean 中使用正确的信息向最终用户验证每个输入。
在服务中引发一般异常并在 bean 中捕获它。
基本规则:
确保您没有在服务中使用 bean 内容(例如 faces 上下文或 beans 本身等)。
确保在数据库出现异常之前进行检查。 (运行时)
也检查一下:link
在我的 class 中,我需要验证并保存移动状态。我不知道把这个验证放在哪里。我想我最好把它放在模型层而不是我的bean中。
我正在这样做:
1 - 运动
@SuppressWarnings("serial")
@Entity
public class Movimentacao implements Serializable, Entidade {
...
@Column(nullable=false)
@NotNull
@DecimalMin("0.01")
private BigDecimal valor;
@Column(nullable=false)
@NotNull
@DecimalMin("0.01")
private BigDecimal valorQuitado;
@Enumerated(EnumType.STRING)
@Column(nullable=false, length=1)
private MovimentacaoStatus status;
...
public void setStatus(MovimentacaoStatus status) {
this.status = status;
}
}
2 - form.xhtml
<!-- show only on edit mode (status not null) -->
<h:outputText id="status" value="#{movimentacaoBean.movimentacao.status.descricao}" rendered="#{movimentacaoBean.movimentacao.status ne null}" />
3 - MovimentacaoBean
public String salvar() throws Exception{
movimentacaoService.salvar(movimentacao);
this.movimentacao = null;
this.todos = null;
context.addMessage(null, new FacesMessage("Movimentação salva com sucesso", ""));
context.getExternalContext().getFlash().setKeepMessages(true);
return "pretty:financeiro-lista";
}
用户没有定义状态。我应该把验证放在哪里?在 setStatus?
如果我将 setStatus 更改为(例如):
public void setStatus() {
//example. The real Business rules are other.
this.status = MovimentacaoStatus.P;
}
或
public void setStatus(MovimentacaoStatus status) { //status variable never used...
//example. The real Business rules are other.
this.status = MovimentacaoStatus.P;
}
我收到以下错误(因为 MovimentacaoBean 没有收到来自 form.xhtml 的状态):
Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'status' cannot be null
我应该如何以及在何处放置状态业务规则?当我编辑记录时,同样的问题也适用。根据 "valor" 和 "valorQuitado" 状态可能会改变。编辑模式的区别在于状态属性在 form.xhtml 上可见(只读 - outputText)
视情况而定。我通常把它放进豆子里。但是在服务层中进行检查以及使用异常也是有意义的。 (如果您计划不同的前端等) 缺点可能是,如果你有很多验证并且你想向最终用户提供正确的信息,它可能会有点棘手。
因此:我建议两个地方都检查一下。
在 bean 中使用正确的信息向最终用户验证每个输入。
在服务中引发一般异常并在 bean 中捕获它。
基本规则:
确保您没有在服务中使用 bean 内容(例如 faces 上下文或 beans 本身等)。
确保在数据库出现异常之前进行检查。 (运行时)
也检查一下:link