类数据验证的实现
我有一个代表某些空气隧道物理特性的对象:
public class Tunnel
{
public double Length { get; set; }
public double CrossSectionArea { get; set; }
public double AirDensity { get; set; }
public double Pressure { get; set; }
//...
}
我需要检查参数的正确性:例如,长度必须为 > 0
,压力>= 0
等等。第一个想法只是对属性访问器进行检查并对无效数据抛出异常:
public class Tunnel
{
private double length;
public double Length
{
get { return length; }
set
{
if (value <= 0)
throw new TunnelParametersException("Invalid data");
length = value;
}
//...
}
但我有一个此类对象的集合,它将被序列化到 XML 文件/从 XML 文件反序列化。所以问题是它不适用于序列化(如果我没有记错的话)。用户可以编辑文件并输入他想要的任何内容,但我将无法捕获它。
因此,据我了解,需要创建一些函数(在 Tunnel 类或另一个类中),我将调用该函数来检查所有值是否正确。但这里还有另一个问题:Tunnel
可能有很少的无效参数。我应该如何返回错误并处理它们?程序必须在一次调用中返回所有发现的错误。而且这种方式只适合我自己使用类。我可能不能强迫其他程序员在每次编辑数据后都使用验证。
请给我一个建议,如何更正确地实现此类值检查并解决我的问题 - 也许是另一种灵活的方式,以便将来易于管理和改进此代码。谢谢。
编辑:回到我的第一个想法,有没有办法在序列化期间或之后验证数据?
I have an object that represents physics characteristics of some air tunnel:
public class Tunnel
{
public double Length { get; set; }
public double CrossSectionArea { get; set; }
public double AirDensity { get; set; }
public double Pressure { get; set; }
//...
}
I need to check correctness of parameters: for example, Length must be > 0
, Pressure >= 0
and so on. The first idea was just to put checking to property accessor and throw exception on invalid data:
public class Tunnel
{
private double length;
public double Length
{
get { return length; }
set
{
if (value <= 0)
throw new TunnelParametersException("Invalid data");
length = value;
}
//...
}
But I have a collection of such object and it will be serialized/deserialized to/from XML-file. So the problem is that it will not work with serialization (if I'm not mistaken). User can edit file and enter whatever he want and I will not able to catch it.
So, as I understand, need to create some function (in Tunnel
class or in another one) that I will call to check that all values are correct. But here another problem: Tunnel
can have few invalid parameters. How should I return errors and process them? The program must return all found errors in one call. And this way is good for only my own use of classes. I probably can't obligate another programmer to use validation after every editing of data.
Give me please an advice how would be more correct to implement such checking of values and solve my problem - maybe some another flexible way so would be easy to manage and improve this code in the future. Thank you.
EDIT: Returning to my first idea, is there any way to validate data during or after serialization?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法:
Simplest possible way:
我想到的是这个。
我会保留只读 IsValid 属性。在 getter 上,我会进行所有验证并判断是真是假。
在任何使用该对象的地方,我都会检查 object.IsValid 是否为 true。
What comes into my mind is this.
I would keep a readonly IsValid property. On the getter I would do all my validation and say true or false.
In any place where I use the object I would would check to see if the object.IsValid is true.
尝试让您的文件每次都通过包装函数,并使磁盘上的文件对普通用户只读,即,如果用户必须编辑文件,他只能通过您的程序进行编辑,当用户完成编辑您的传递时通过你的函数检查所有数据,看看整个数据是否仍然有效,
try making your files pass through a wrapper function everytime, and making the files on disk read-only for normal user,i.e, if the user has to edit the file he does so only through your program, when the user is finished editing u pass all the data through your function to see if the whole data is still valid or not,
对于编辑:使序列化流通过某种缓冲函数,该函数在序列化之前或期间处理数据,具体取决于哪种方式更容易实现,如果您选择前者,则数据将首先被验证(返回类型和参数类型将相同),然后进行序列化,否则将在转换时检查数据。
For the edit : make the serialization streams pass through a sort-of buffer function,that processes the data before serialization or during it, depending on which way is easier to implement, if you chose the former, the data will first be validated(the return type and parameter type will be same) and then be serialized, otherwise the data will be checked as it is converted..