我正在开发一个应用程序,试图遵循六边形架构和干净架构的建议。将应用程序划分为多个层:基础架构、应用程序和模型。所有这些都在关注我导入的包,并且只有基础架构层中的框架存在依赖关系。
疑问来自于模型中的实现。大多数情况下,如果验证字符串的最大长度或最大值和数字,则可以使用常量,如果构造函数中不满足条件,则会抛出异常:
@Value
public class UserName {
String value;
public UserName(String value) {
validate(value);
this.value = value;
}
private void validate(String value) {
if (value.length() > 100) {
throw new IllegalArgumentException();
}
}
}问题是,如果我想参数化这些验证的最大值,或者在属性或数据库文件中,例如。该机制将与加载应用程序框架使用的数据的方式相耦合。应该在什么时候进行验证?
通过属性服务加载用例(应用层)中的值。例如:
public class CreateUser() {
private PropertiesService propertiesService;
public User create(String nameValue) {
UserName userName = new UserName(nameValue, propertiesService.getMaxNameLegth());
//...
}
}
@Value
public class UserName {
String value;
public UserName(String value, int maxValue) {
validate(value);
this.value = value;
}
private void validate(String value) {
if (value.length() > maxValue) {
throw new IllegalArgumentException();
}
}
}或者创建一个验证器,将类传递到该验证器并验证其所有属性:
@Value
public class UserName {
String value;
public UserName(String value, int maxValue) {
this.value = value;
}
}
public interface UserNameValidator {
void validate(UserName userName);
}
public class UserNameValidatorImpl {
private PropertiesService propertiesService;
public void validate(UserName userName); {
if (userName.getValue().length() > propertiesService.getMaxNameLegth()) {
throw new IllegalArgumentException();
}
// validate all atributes
}
}
public class CreateUser() {
private UserNameValidator userNameValidator;
public User create(String nameValue) {
UserName userName = new UserName(nameValue);
userNameValidator.validate(userName);
//...
}
}最佳解决方案是什么?最干净的?其他更好的可能性?谢谢。
发布于 2021-06-09 13:30:39
模型中的实现带来了疑问。大多数情况下,如果验证字符串的最大长度或最大值和数字,则可以使用常量,如果构造函数中不满足条件,则会抛出异常:
您是否因为域规则或数据库约束(如列长度)而验证名称的长度?
也许验证属于数据库层,而不是域层。
通过属性服务加载用例(应用层)中的值。..。或者..。创建一个验证器,类将被传递到该验证器,并对其所有属性进行验证
如果验证规则是域规则,并且应该在运行时加载它们,则应该像处理任何其他持久数据一样处理它们。
+----------------+ +----------------+ +-----------------------+
| User | -------> | UserValidation | <--------- | ValidationRepository |
+----------------+ +----------------+ +-----------------------+
^
|
============================================================================|=============
|
+----------+------+
| |
+------------+ +--------+
| Properties | | DB |
+------------+ +--------+将ValidationRepository传递给您的CreateUser
public class CreateUser {
private ValidationRepository validationRepository;
public User create(String nameValue) {
UserValidation userValidation = validationRepository.getUserValidation();
UserName userName = new UserName(nameValue, userValidation);
//...
}
}当应该创建User时,您可以从ValidationRepository获取UserValidation并将其传递给User或UserName。
public class UserValidation {
// should be set (or passed as constructor arg) by the repository implementation
private int useNameMaxValue;
public String validateUserName(String userName){
if (userName.length() > maxValue) {
throw new IllegalArgumentException();
}
}
}User或UserName只使用UserValidator。
public class UserName {
String value;
UserName(String value, UserValidator validator) {
this.value = validator.validateUserName(value);
}
}https://stackoverflow.com/questions/67874718
复制相似问题