验证命名约定? C#

发布于 2024-09-16 06:23:47 字数 109 浏览 3 评论 0原文

非常简单的问题,但我想开始对验证方法使用一致的命名约定,但我想不出最好的方法!

人们倾向于使用 IsDataValid() 风格吗?或者还有其他更具描述性和意义的吗?

干杯

Very simple question, but I want to start using a consistent naming convention for validation methods and I can't think of the best way!

Do people tend to use IsDataValid() style? or are there any others that are more descriptive and meaningful?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒七 2024-09-23 06:23:47

这取决于您的验证方法的作用。

如果它返回一个布尔值,那么可能以 Is 开头并以 Valid 结尾是一个不错的起点。使用 is 进行布尔调用通常会导致 if 语句中的代码可读。

如果您的验证方法引发异常,那么我通常会以 Check 之类的内容开始方法名称。

然而,还值得考虑的是(因为方法通常应该使用动词)方法名称以 Validate 开头。 Is 样式通常更适用于属性。

It depends what your validation method does.

If it returns a Boolean, then probably starting with Is and ending with Valid is a good place to start. Using is for Boolean calls generally leads to readable code in if statements.

If your validation method throws an exception, then I'd usually start the method name with something like Check instead.

However, also worth considering (as methods should usually use verbs) is beginning the method name with Validate. The Is style is generally more applicable to properties.

别把无礼当个性 2024-09-23 06:23:47

与涉及命名约定的任何事情一样,不存在正确的答案,但是验证方法存在很多常见问题,这些问题适合某种方法,即:

  • 如果一切正常,您通常只需要验证的布尔状态。
  • 如果出现问题,您通常需要了解问题的详细信息。
  • 您通常希望对象具有类似的验证方法。

我发现一种有用的方法是为我想要验证的每个模型对象提供一个单独的验证器类,该验证器类实现了通用的 IValidator 接口,通常使用以下方法:

  • 采用对象的构造函数 名为 IsValid() 的属性用于验证
  • 对象,返回一个布尔值,但将特定错误存储在私有变量中,因此当您确实需要错误时,不需要重新计算验证。
  • 名为 ErrorMessages 的属性,用于验证对象(如果尚未验证),并返回该对象的错误列表。

这允许在您的业务逻辑中非常自然地使用:

BusinessObject obj = new BusinessObject();

// populate fields

BusinessObjectValidator objValidator = obj.GetValidator();

if (objValidator.IsValid) {
    obj.Save();
} else {
    foreach (var errorMessage in objValidator.ErrorMessages) {
        // output message
    }
}

As with anything involving naming conventions, there's no such thing as a right answer, but there's a lot of common problems with validation methods that lend themselves towards a certain approach, namely:

  • If everything is OK, you typically only need the boolean status of the validation.
  • If there are problems, you usually need to know details about the problem.
  • You usually want objects to have similar approaches to validation.

One approach I've found to be useful is to have a seperate validator class for each model object I want to validate that implements a common IValidator interface, usually with the following methods:

  • A constructor that takes the object to be validated in.
  • A property named IsValid(), that validates the object, returns a boolean, but stores specific errors in private variables so validation doesn't need to be recalculated when you do want the errors.
  • A property named ErrorMessages, that validates the object (if it hasn't been validated yet), and returns a list of errors with the object.

This allows a pretty natural usage within your business logic:

BusinessObject obj = new BusinessObject();

// populate fields

BusinessObjectValidator objValidator = obj.GetValidator();

if (objValidator.IsValid) {
    obj.Save();
} else {
    foreach (var errorMessage in objValidator.ErrorMessages) {
        // output message
    }
}
﹏雨一样淡蓝的深情 2024-09-23 06:23:47

人们倾向于使用 IsDataValid() 风格吗?

当方法返回单个布尔值时,我通常使用“Is”MethodName 样式。就命名而言,这是完全可以接受的。很多时候,数据验证是在属性集而不是方法中完成的,因此在这种情况下,您不需要更改属性名称来指示它验证其上的数据集。

以下链接提供了一些您可能也会感兴趣的通用命名指南:

命名指南:
http://msdn.microsoft.com/en -us/library/xzf533w0(v=vs.71).aspx

Do people tend to use IsDataValid() style?

I typically use the 'Is' MethodName style when the method returns a single Boolean value. It is perfectly acceptable in terms of naming. A lot of times data validation is done within the Set of a Property rather than a method so in this case you don't need to change the property name to indicate it validates the data set on it.

Here is a link that gives some general naming guidlines which you might find interesting as well:

Naming Guidelines:
http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文