数据验证设计模式[英] Data Validation Design Patterns

本文是小编为大家收集整理的关于数据验证设计模式的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如果我有一个数据库表的集合(例如,在访问文件中),并且需要针对本集合中的每个表验证每个表中的每个表,该规则集都在所有表中都具有共同的规则,以及一个特定于一个或特定的单个规则或一部分桌子,有人可以推荐一个良好的设计模式来研究吗?

具体来说,我想避免使用类似的代码:

void Main()
{
    ValidateTable1();
    ValidateTable2();
    ValidateTable3();
}

private void ValidateTable1()
{
    //Table1 validation code goes here
}

private void ValidateTable2()
{
    //Table2 validation code goes here
}

private void ValidateTable3()
{
    //Table3 validation code goes here
}

另外,我决定使用log4net记录所有错误和警告,以便可以声明void>,并且不需要返回任何方法.这是一个好主意,还是最好创建某种ValidationException捕获所有异常并将它们存储在List<ValidationException>中之前,然后将它们全部打印出来?

?

我确实找到了推荐答案

只是一个更新:我决定选择 docorator图案.也就是说,我有一个实现IValidateableTable接口的"通用"表类(其中包含validate()方法).然后,我创建了几个验证装饰器(implement IValidateableTable),我可以在我试图验证的每个桌子上包裹.

因此,代码最终看起来像这样:

IValidateableTable table1 = new GenericTable(myDataSet);
table1 = new NonNullNonEmptyColumnValidator(table1, "ColumnA");
table1 = new ColumnValueValidator(table1, "ColumnB", "ExpectedValue");

然后,我要做的就是调用table1.Validate(),它可以通过调用所有必要验证的装饰器放松身心.到目前为止,尽管我仍然接受建议,但它似乎真的很好.

其他推荐答案

我将根据您要结构的方式返回每种类型的验证...或iList.

您也可以选择做一些这样的魔术:

using(var validation = new ValidationScope())
{
   ValidateTable1();
   ValidateTable2();
   ValidateTable3();

   if(validation.Haserrors)
   {
       MessageBox.Show(validation.ValidationSummary);
       return;
   }

   DoSomethingElse();
}

然后,有效的物质将进入当前范围,如这样:

ValidationScope.Current.AddError("col1", "Col1 should not be NULL");

这种效果.

其他推荐答案

两种方法:

  1. csla 在业务对象上使用匿名方法进行验证.
  2. 阅读 jp booodhoo's 他已经实施了一个规则引擎,并具有非常详细的帖子和详细的帖子和详细的帖子和详细的帖子和详细的帖子和示例代码发布.您还可以看到他在 dnr tv 情节值得一看.

本文地址:https://www.itbaoku.cn/post/627751.html

问题描述

If I have a collection of database tables (in an Access file, for example) and need to validate each table in this collection against a rule set that has both common rules across all tables as well as individual rules specific to one or a subset of tables, can someone recommend a good design pattern to look into?

Specifically, I would like to avoid code similar to:

void Main()
{
    ValidateTable1();
    ValidateTable2();
    ValidateTable3();
}

private void ValidateTable1()
{
    //Table1 validation code goes here
}

private void ValidateTable2()
{
    //Table2 validation code goes here
}

private void ValidateTable3()
{
    //Table3 validation code goes here
}

Also, I've decided to use log4net to log all of the errors and warnings, so that each method can be declared void and doesn't need to return anything. Is this a good idea or would it be better to create some sort of ValidationException that catches all exceptions and stores them in a List<ValidationException> before printing them all out at the end?

I did find this, which looks like it may work, but I'm hoping to actually find some code samples to work off of. Any suggestions? Has anyone done something similar in the past?

For some background, the program will be written in either C# or VB.NET and the tables will more than likely be stored in either Access or SQL Server CE.

推荐答案

Just an update on this: I decided to go with the Decorator pattern. That is, I have one 'generic' table class that implements an IValidateableTable interface (which contains validate() method). Then, I created several validation decorators (that also implement IValidateableTable) which I can wrap around each table that I'm trying to validate.

So, the code ends up looking like this:

IValidateableTable table1 = new GenericTable(myDataSet);
table1 = new NonNullNonEmptyColumnValidator(table1, "ColumnA");
table1 = new ColumnValueValidator(table1, "ColumnB", "ExpectedValue");

Then, all I need to do is call table1.Validate() which unwinds through the decorators calling all of the needed validations. So far, it seems to work really well, though I am still open to suggestions.

其他推荐答案

I'd return some type of ValidationSummary for each one... or an IList depending on how you want to structure it.

you could also opt to do some magic like this:

using(var validation = new ValidationScope())
{
   ValidateTable1();
   ValidateTable2();
   ValidateTable3();

   if(validation.Haserrors)
   {
       MessageBox.Show(validation.ValidationSummary);
       return;
   }

   DoSomethingElse();
}

then the ValidateTable would just reach into the current scope, like this:

ValidationScope.Current.AddError("col1", "Col1 should not be NULL");

something to that effect.

其他推荐答案

Two approaches:

  1. CSLA where anonymous methods on business objects are used for validation.
  2. Read JP Boodhoo's blog where he has implemented a rules engine and has very detailed posts and sample code published. You can also see him at work on DNR Tv episode that's well worth watching.