对域模型执行验证 [英] Perform validation on domain model

查看:133
本文介绍了对域模型执行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的业务领域模型具有复杂的验证规则,只有在模型更新后才能执行。

Our business domain model has complex validation rules, which can only be executed when the model has been updated.

这样的事情:

public classs MyDomainModel {
    public DomainValidationContext getValidationContext() {
       ...
    }
}

我们不想提取这些,因为:

We don't want to extract these, since:


  1. 这意味着重复验证规则。

  2. 列表项某些规则只是软规则,应该会在不停止进一步处理的情况下产生警告消息。

我们考虑重复Validationr中的DomainModel更新值并检查验证错误/警告。

We considered duplicating the DomainModel in the Validationr updating the value and checking for validation errors/warnings.

public class SomeValidator implements Validator {
   @Inject
   private DomainEditContext domainEditContext;

   public void validate(final FacesContext context, final UIComponent component, final Object value) {
       MyDomainModel validationModel = domainWorkContext.getDomainModel().clone();
       validationModel.setSomeValue(value);
       DomainValidationContext dvc = validationModel.getValidationContext();
       ...
   }
}

然而这是公平的容易出错和处理密集,因为每次验证都必须为域模型提供完整的深层副本。

However this is fairly error prone and processing intensive since a full deep copy of the domain model has to be made for every validation.

在模型更新/调用后是否有更好的方法来执行验证-application或渲染响应之前?

Is there any better way to perform validations post model update/invoke-application or prior to render-response?

任何其他想法都非常受欢迎。

Any other ideas are more than welcome.

推荐答案

查看 JsfWarn ,它会在模型​​更新和调用应用程序后执行验证。

Have a look at JsfWarn it performs validations after model-update and invoke application.

使您能够直接从更新的模型中检索验证上下文。
您的警告验证器将如下所示:

Enabling you to retrieve the validation context straight from the updated model. Your warning validator will look something like this:

public class SomeValidator implements WarningValidator {
   @Inject
   private DomainEditContext domainEditContext;

   public void validate(FacesContext context, UIComponent component, ValidationResult validationResult) {
       MyDomainModel myModel = domainEditContext.getMyDomainModel();
       DomainValidationContext dvc = myModel.getValidationContext();
       if(dvc.isSomeValueInvalid()) {
          validationResult.setFacesMessage(....);
       }
   }
}

与f:validator类似必须向目标组件添加jw:warning。

Similar to f:validator you have to add jw:warning to the targeted component.

<h:inputText id="someValue">
    <jw:warning validator="#{someValidator}" />
    <f:ajax event="change" render="..." />
</h:inputText>
<h:message for="someValue" />

这篇关于对域模型执行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆