如何向我的班级用户表明验证要求?
我正在实现一个类,该类使用非常严格定义的模式封装 xml 文档。 我不控制架构。
类中的属性之一用于模式指示必须与特定正则表达式匹配的元素值。 在属性的设置器中,如果字符串与表达式不匹配,我将引发异常。
我的问题是,如何才能更好地向我班级的用户传达该领域的要求? 有我可以使用的属性吗? Xml 注释(所以它显示在智能感知中)? 除了抛出异常之外,我还应该做些什么吗? 我还有什么其他选择?
I'm implementing a class that wraps around an xml document with a very strictly defined schema. I don't control the schema.
One of the properties in the class is for an element value that the schema indicates must match a certain regular expression. In the setter for the property, if a string doesn't match the expression I'm throwing an exception.
My question is, how can I better communicate to users of my class the requirements for this field? Is there an attribute I can use? Xml comments (so it shows up in intellisense)? Should I do something other than thrown an exception? What other options do I have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您随程序集一起提供 XmlComments 可能会有所帮助,但我想说,如果不满足要求,最好不要抛出异常,并使异常消息尽可能详细。 如果当用户调用依赖于该属性的方法/属性时未满足要求,我也会抛出异常(同样有很多细节)。
您实际上无法采取任何措施来阻止使用该代码的人第一次犯错误,但是当错误确实发生时,您应该尽可能清楚地了解如何纠正它。
XmlComments may help if you ship them with your assembly, but I would say that you are best off throwing exceptions if the requirements are not met, and making the exception message as detailed as possible. I would also throw exceptions (again with lots of detail) if the requirement is not met when the user calls and methods/properties the rely on the property.
There isn't really much you can do to keep someone using the code from making the mistake the first time, but you should be as clear as possible when the mistake does occur about how to correct it.
您的代码文档应满足要求,或者模式文档应解释要求。 对于那些不愿意研究他们将要使用的代码的人,你无能为力。
Either your code's documentation should address requirements, or the documentation for the schema should explain the requirements. You can't do anything for someone who doesn't bother to research the code they're about to use.
感谢您的建议。
我在考虑这个问题时想到的一个想法是创建一个名为 MatchedString 之类的新类来强制执行约束。
它有一个需要正则表达式字符串的构造函数,构造后表达式将仅通过只读属性向用户公开。 然后它会有一个用户可以设置的 value 属性,该属性将根据 setter 中的表达式进行检查。
我的想法是,我还可以为不同的行为创建选项,以便在枚举中验证失败时使用,并让用户指定他们想要的选项:
此外,我认为这将允许我的类用户进行一些基本测试,而不必在自己的代码中复制 RegEx 对象。 添加与字符串类型之间的隐式转换,对于类用户来说应该是直观的。
对此有什么想法吗?
Thanks for the advice.
One idea I had while thinking about this was creating a new class named something like MatchedString to enforce the constraint.
It'd have a constructor that required a regex string, and after construction the expression would only be exposed to users via a read-only property. Then it would have a value property that users could set that would check against the expression in the setter.
My thought was that I could then also create options for different behaviors to use when the validation failed in an enum, and let the user specify which they want:
Also, I was thinking that this would allow my class user to do some basic tests without having to duplicate the RegEx object in their own code. Throw in implicit conversions to/from the string type, and it should be intuitive to class users.
Any thoughts on this?
无论我是否使用它,实现 MatchedString 类看起来都很有趣。 所以这里是:
Whether or not I use it, implementing a MatchedString class looked like fun. So here it is:
将其记录在 XML 注释中,并引发异常。 使消息明确:
Document it in the XML comments, and throw an exception. Make the message explicit: