英文:
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("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;
}
// 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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论