在服务方法中以更清晰的方式验证用户输入

huangapple go评论94阅读模式
英文:

Validating user inputs on service methods in a cleaner way

问题

在一个项目中,我们使用服务方法,在这里,我们必须验证某些必需的输入以使我们的业务逻辑正常工作:

public void exampleMethod(final UserDto user, String sthElse) {
  if (user.getId() == null) {
    throw new CustomHandledException("example");
  }
  if (sthElse == null) {
    throw a CustomHandledException("example");
  }

  GetUserResponseDto getUserResponse = client.getUserById(user.getId(), country);

  if (getUserResponse == null) {
    if (user.getGivenName() == null) {
      throw new CustomHandledException("example");
    }
    if (user.getEmail() == null) {
      throw new CustomHandledException("example");
    }
    CreateUserRequestDto createUserBody = UserMapper.toCreateBody(user);
    client.createUser(createUserBody);
    return;
  }

  if (user.getLocale().equals(getUserResponse.getUser().getProfile().getLocale())) {
    return;
  }

  client.updateUser(user);
}

尽管代码不一定是错误的,并且相当容易理解(对我来说),但我觉得其中有一些奇怪的地方,您是否对更清晰的代码有任何建议?

我尝试编写清晰的代码,但感觉它并不够清晰。

英文:

In a project, we use service methods, there, we have to validate certain required inputs for our business logic to work:

public void exampleMethod(final UserDto user, String sthElse) {
  if (user.getId() == null) {
    throw new CustomHandledException("example");
  }
  if (sthElse == null) {
    throw new CustomHandledException("example");
  }

  GetUserResponseDto getUserResponse = client.getUserById(user.getId(), country);

  if (getUserResponse == null) {
    if (user.getGivenName() == null) {
      throw new CustomHandledException("example");
    }
    if (user.getEmail() == null) {
      throw new CustomHandledException("example");
    }
    CreateUserRequestDto createUserBody = UserMapper.toCreateBody(user);
    client.createUser(createUserBody);
    return;
  }

  if (user.getLocale().equals(getUserResponse.getUser().getProfile().getLocale())){
      return;
  }

  client.updateUser(user);
}

Even though the code isn't necessarily wrong and is quite understandable (for me) I feel like there is something odd there, do you have any suggestions for a cleaner code?

I tried to write clean code, however, it doesn't feel like it is clean.

答案1

得分: 0

我们可以通过AOP解决方案或按照以下方法提取验证来处理:

public void exampleMethod(final UserDto user, String sthElse) {
    validateUser(user);

    if (sthElse == null) {
        throw new CustomHandledException("example");
    }

    GetUserResponseDto getUserResponse = client.getUserById(user.getId(), country);

    if (getUserResponse == null) {
        CreateUserRequestDto createUserBody = UserMapper.toCreateBody(user);
        client.createUser(createUserBody);
        return;
    }

    if (user.getLocale().equals(getUserResponse.getUser().getProfile().getLocale())) {
        return;
    }
    // 遵循惯例,更新之前也需要进行验证
    // 因为在插入时已经进行了验证
    client.updateUser(user);
}

private boolean validateUser(UserDto userDto) {

    // 这些声明可以移动到类字段或常量中
    Predicate<UserDto> customDtoCheck = user -> user.getGivenName().startsWith("text");
    Predicate<String> emailValidation = RequestValidator::validateEmail;
    Predicate notNull = RequestValidator::notNull;

    boolean validUser = notNull.test(userDto) &&
                        notNull.test(userDto.getId()) &&
                        notNull.test(userDto.getGivenName()) &&
                        notNull.test(userDto.getEmail()) &&
                        notNull.test(userDto.getLocale()) &&
                        emailValidation.test(userDto.getEmail()) &&
                        customDtoCheck.test(userDto);
    if (!validUser)
        throw new CustomHandledException("example");

    return validUser;
}
英文:

We could go by AOP solutions or by extracting validations to a method as below

public void exampleMethod(final UserDto user, String sthElse) {
validateUser(user);
if (sthElse == null) {
throw new CustomHandledException(&quot;example&quot;);
}
GetUserResponseDto getUserResponse = client.getUserById(user.getId(), country);
if (getUserResponse == null) {
CreateUserRequestDto createUserBody = UserMapper.toCreateBody(user);
client.createUser(createUserBody);
return;
}
if (user.getLocale().equals(getUserResponse.getUser().getProfile().getLocale())){
return;
}
// validations needs to be done before updating as well by convention
// since validations were done while inserting
client.updateUser(user);
}
private boolean validateUser(UserDto userDto) {
//these declarations could be moved at class fields or constant
Predicate&lt;UserDto&gt; customDtoCheck = user -&gt;user.getGivenName().startsWith(&quot;text&quot;);
Predicate&lt;String&gt; emailValidation = RequestValidator::validateEmail;
Predicate notNull = RequestValidator::notNull;
boolean validUser = notNull.test(userDto) &amp;&amp;
notNull.test(userDto.getId()) &amp;&amp;
notNull.test(userDto.getGivenName()) &amp;&amp;
notNull.test(userDto.getEmail()) &amp;&amp;
notNull.test(userDto.getLocale()) &amp;&amp;
emailValidation.test(userDto.getEmail()) &amp;&amp;
customDtoCheck.test(userDto);
if (!validUser)
throw new CustomHandledException(&quot;example&quot;);
return validUser;
}

huangapple
  • 本文由 发表于 2023年8月4日 03:17:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定