C# 在 setter 方法上添加验证
我在 C# 中定义了几个变量:
public String firstName { get; set; }
public String lastName { get; set; }
public String organization { get; set; }
我想要的是在尝试设置值时向这些方法添加验证。假设您要为firstName设置一个值,i应该通过正则表达式来实际设置,否则应该抛出异常。是否可以使用这种“短语法”构建,或者我应该使用标准(如 JAVA 中的)getter 和 setter 并在那里验证数据?
I have a a couple of variables that i define in C# by:
public String firstName { get; set; }
public String lastName { get; set; }
public String organization { get; set; }
What i want is to add validation to these methods when you try to set a value. Lets say your going to set a value for firstName, the i should pass thrue a regexp to actuelly be set, otherwise an exception should be thrown. Is this possible to build with this "short syntax" or should I go for standard (like in JAVA) getters and setters and in there validate the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果要在设置属性时进行验证,则需要使用非自动属性(即手动定义的
get
和set
方法)。但另一种验证方法是将验证逻辑与域对象分开。
然后,您可以说:
这是我更喜欢的方法:
客户
不应该负责验证自己的数据,这是另一个责任,因此应该住在其他地方。If you want to validate when the property is set, you need to use non-auto properties (i.e., manually defined
get
andset
methods).But another way to validate is to have the validation logic separate from the domain object.
Then, you could say:
This is the approach I prefer:
Customer
should not responsible for validating its own data, that is another responsibility and therefore should live elsewhere.您现在拥有的称为“自动属性”,并且仅执行简单的“获取/设置”。为了自定义
get
或set
的行为,您需要将属性转换为字段支持的属性:请注意,我更改了
String
> 转换为string
并将属性名称大写,遵循公认的 C# 命名最佳实践。What you have now are called "auto-properties," and only perform a simple "get/set". In order to customize the behavior of the
get
orset
, you will need to convert the properties to field-backed properties:Note that I changed
String
tostring
and capitalized the property name, in following accepted C# naming best practices.我根本不会在
setter
中添加验证。相反,我会创建一个名为validate
的函数......这样您的所有验证代码都位于一个位置,而不是分散在您的setters
中。I wouldn't add validation in the
setter
at all. Rather, I would create a function calledvalidate
instead...that way all your validation code is in one spot rather scattered throughout yoursetters
.最佳实践是应用 SRP。在单独的类中设置验证。
您可以使用 FluentValidation
您可以通过继承
AbstractValidator为 Customer 类定义一组验证规则。
:示例:
要运行验证器,请实例化验证器对象并调用 Validate 方法,传入要验证的对象。
}
}
It's best practice to apply SRP. Set the validation in a separate class.
You can use FluentValidation
You would define a set of validation rules for Customer class by inheriting from
AbstractValidator<Customer>
:Example:
To run the validator, instantiate the validator object and call the Validate method, passing in the object to validate.
}
}
为此,您必须使用完整的属性语法。
You have to use the full property syntax for this.