模型层上的业务规则

在我的课程中,我需要验证并保存movimentation的状态。我不知道把这个验证放在哪里。我想我最好把它放在模型层上,而不是放在我的bean中。模型层上的业务规则

我这样做:

1 - Movimentacao

@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状态):

原因:java.sql.SQLIntegrityConstraintViolationException:列 'status'不能为空

如何以及在哪里我应该放置状态的业务规则?编辑记录时,同样的问题也适用。取决于“勇气”和“valorQuitado”,状态可能会改变。编辑模式的不同之处在于状态属性在form.xhtml(只读 - outputText)上可见

回答:

这取决于。我通常把它放进豆子里。但是使用异常来检查服务层也是有意义的。 (如果你计划不同的前端等) 缺点可能是,如果你有很多验证,你想给最终用户适当的信息,它可能会更棘手一些。

因此:我会建议在两个地方做检查。

  • 在bean中验证每个输入并向最终用户提供正确的信息。

  • 在服务中引发一般异常并在bean中捕获它。

基本规则:

  • 确保你没有在服务中使用的bean的东西(像面背景下,或豆类本身等)。

  • 确保在数据库上升异常之前执行检查。(运行)

检查这个问题,以及:link

以上是 模型层上的业务规则 的全部内容, 来源链接: utcz.com/qa/260404.html

回到顶部