我如何指示验证要求我的类的用户? [英] How do I indicate a validation requirement to users of my class?
问题描述
我实现了环绕一个XML文档具有非常严格定义的架构类。我不控制模式。
I'm implementing a class that wraps around an xml document with a very strictly defined schema. I don't control the schema.
在一个类中的属性是元素值,该架构指示必须符合一定的规律EX pression。在制定者的财产,如果一个字符串不匹配前pression我抛出异常。
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.
我的问题是,我怎么能更好地传达给用户我班的这方面的要求?是否有一个属性可以使用吗? XML注释(所以它显示在智能感知)?我应该做的事情比抛出一个异常等?我有什么其他选择?
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?
推荐答案
谢谢你的建议。
我有一个想法一边想着,这是创建一个新类名字类似MatchedString强制约束。
One idea I had while thinking about this was creating a new class named something like MatchedString to enforce the constraint.
这将会有一个构造函数,需要一个正则表达式的字符串,以及施工后的前pression将只能通过只读属性暴露给用户。然后,它有一个用户可以设置,将检查对前pression在二传手的值属性。
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:
- 设置为空字符串
- 设置为空字符串,并抛出异常
- 设置错误值反正
- 设置错误值反正扔excpetion
- 在刚刚抛出异常
- 在做什么
另外,我在想,这将让我的类的用户做一些基本的测试,而无需重复的正则表达式对象在自己的code。再加上隐式转换为/从字符串类型,它应该是直观类用户。
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.
在这个有什么想法?
这篇关于我如何指示验证要求我的类的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!