业务逻辑验证模式&建议 [英] Business logic validation patterns & advices

查看:161
本文介绍了业务逻辑验证模式&建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有两层验证。首先是由bean验证API执行的实体验证(例如,必填字段)。
第二级是业务逻辑验证。例如,用户有一个帖子。用户只有在他是该帖子的创建者并且发布评级< 50.所以我必须这样做:

I have two layers of validation in my application. First is entity validation performed by bean validation API (e.g. required fields). The second level is business logic validation. For example, user has a post. User can delete a post only if he is a creator of this post and post rating < 50. So I have to do something like this:

if (post.getCreator().equals(session.getUser())) {
  if (post.getRating() < 50) {
    postRepository.delete(post);
  } else errors.add(400, "Cant delete post with rating 50 or higher")
} else errors add (400, "You should be owner of the post")

我不喜欢这种方式,因为这个条件被重用,我不得不重复代码。此外,如果条件数大于5,则读取和理解代码变得不真实。

I don't like this way as this conditionals are reused and I have to duplicate code. Moreover, if number of conditionals is greater than 5 or so it becomes unreal to read and understand the code.

此外,标准的Spring Validator对我来说不是很有帮助必须为不同操作的一个实体制作不同的验证(例如删除和更新)

Moreover, standard Spring Validator won't be very helpful as I have to maker different validation for one entity on different actions (delete and update for example)

所以我正在寻找一种更智能的方式(模式)也许)如果有人能给我一个提示,我将非常感激。

So I'm looking for a way to do this in a smarter way (pattern maybe) and I would be very grateful if someone could give me a hint.

提前感谢!

推荐答案

您可以使用策略模式

每个条件都可以建模为一个带帖子和会话的函数,可能会返回错误:

Each condition can be modeled as a function that takes a post and a session and might return an error:

Post -> PostContext -> Optional<String> 

您可以通过界面来表示:

You could represent this with an interface:

@FunctionalInterface
public interface ValidationCondition {

    Optional<String> validate(final Post post, final Session session);
}

例如:

public class CreatorValidation implements ValidationCondition {

    public Optional<String> validate(final Post post, final Session session) {
        if (post.getCreator().equals(session.getUser()) {
            return Optional.empty();
        }
        return Optional.of("You should be the owner of the post");
    }
}

然后,您可以将每个验证存储在列表中:

You can then store every validation in a list:

final List<ValidationCondition> conditions = new ArrayList<>();

conditions.add(new CreatorValidation());
conditions.add(new ScoreValidation());
// etc.

使用该列表,可以批量应用验证:

Using the list, validations can be applied in bulk:

final List<String> errors = new ArrayList<>();

for (final ValidationCondition condition : conditions) {
    final Optional<String> error = condition.validate(post, session);
    if (error.isPresent()) {
        errors.add(error.get());
    }
}

使用Java 8 lambdas,你可以声明这些内联:

Using Java 8 lambdas, you could declare these inline:

final ValidationCondition condition = (post, session) -> {
    // Custom logic
});

这篇关于业务逻辑验证模式&amp;建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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