验证框架.NET中,可以做字段之间的编辑 [英] Validation Framework in .NET that can do edits between fields

查看:139
本文介绍了验证框架.NET中,可以做字段之间的编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的经验,在.NET中很多的验证框架,让您可以验证一个字段,在一个时间做这样的事情,确保一个字段是邮政code或者例如电子邮件地址。我通常把这些内场的编辑。

From my experience many validation frameworks in .NET allow you to validate a single field at a time for doing things like ensuring a field is a postal code or email address for instance. I usually call these within-field edits.

在我的项目中,我们经常要做的场之间,编辑虽然。举例来说,如果你有一类这样的:

In my project we often have to do between-field-edits though. For instance, if you have a class like this:

public class Range
{
    public int Min { get; set; }
    public int Max { get; set; }
}

您可能希望确保最大大于最小。您可能还希望做一些验证对外部对象。比如给你有一类这样的:

you might want to ensure that Max is greater than Min. You might also want to do some validation against an external object. For instance given you have a class like this:

public class Person
{
    public string PostalCode { get; set; }
}

以任何理由要确保邮政code存在于数据库或提供给您的文件。我有一个像其中一个用户提供了数据字典更复杂的例子,你想验证你的对象针对该数据字典。

and for whatever reason you want to ensure that Postal Code exists in a database or a file provided to you. I have more complex examples like where a user provides a data dictionary and you want to validate your object against that data dictionary.

我的问题是:我们可以使用任何现有的验证框架(TNValidate,NHibernate的验证)的.NET还是我们需要使用规则引擎还是什么?你怎么人们在现实世界中应对这种情况呢? : - )

My question is: can we use any of the existing validation frameworks (TNValidate, NHibernate Validator) for .NET or do we need to use a rules engine or what?? How do you people in the real world deal with this situation? :-)

推荐答案

只有一个验证框架,我清楚地知道,这是的企业库验证应用程序块或VAB的简称。我会回答你的问题,从VAB的背景下。

There's only one validation framework that I know well and that is Enterprise Library Validation Application Block, or VAB for short. I will answer your questions from the context of the VAB.

第一个问题:你能做到的状态(字段之间)验证的VAB

First question: Can you do state (between-field) validation in VAB?

当然可以。有多种方法可以做到这一点。您可以选择自我验证机制,具体如下:

Yes you can. There are multiple ways to do this. You can choose for the self validation mechanism, as follows:

[HasSelfValidation]
public class Range
{
    public int Min { get; set; }
    public int Max { get; set; }

    [SelfValidation]
    public void ValidateRange(ValidationResults results)
    {
        if (this.Max < this.Min)
        {
            results.AddResult(
                new ValidationResult("Max less than min", this, "", "", null));
        }
    }
}

我必须说,我个人不喜欢这种类型的验证,特别是验证我的域名实体的时候,因为我喜欢让我的验证,从验证逻辑分离(并保持我的域名逻辑避免提及任何验证框架) 。然而,他们需要相当少code比替代,这是写一个自定义的验证器类。这里有一个例子:

I must say I personally don't like this type of validations, especially when validating my domain entities, because I like to keep my validations separate from the validation logic (and keep my domain logic free from references to any validation framework). However, they need considerably less code than the alternative, which is writing a custom validator class. Here's an example:

[ConfigurationElementType(typeof(CustomValidatorData))]
public sealed class RangeValidator : Validator
{
    public RangeValidator(NameValueCollection attributes)
        : base(string.Empty, string.Empty) { }

    protected override string DefaultMessageTemplate
    {
        get { throw new NotImplementedException(); }
    }

    protected override void DoValidate(object objectToValidate,
        object currentTarget, string key, ValidationResults results)
    {
        Range range = (Range)currentTarget;

        if (range.Max < range.Min)
        {
            this.LogValidationResult(results,
                "Max less than min", currentTarget, key);
        }
    }
}

写这个类,你可以连接这个类在你的验证配置文件这样的后:

After writing this class you can hook this class up in your validation configuration file like this:

<validation>
  <type name="Range" defaultRuleset="Default" assemblyName="[Range Assembly]">
    <ruleset name="Default">
      <validator type="[Namespace].RangeValidator, [Validator Assembly]"
        name="Range Validator" />
    </ruleset>
  </type>
</validation> 

第二个问题:如何做复杂的验证可能出现的相互作用的数据库(与VAB)

Second question: How to do complex validations with possible interaction a database (with VAB).

是我给的第一个问题的例子也该使用。您可以使用相同的技术:自我验证和自定义验证。你的情况,你想在​​一个数据库来检查值实际上是一个简单的,因为你的目标的有效性不是基于上下文。你可以简单地检查对数据库对象的状态。它变得更复杂,当在其中一个对象居住的上下文获取重要的(但有可能与VAB)。想象一下,比如你想要写一个验证,以确保每一位客户,在给定的时刻,没有两个以上的未发货订单。这不仅意味着你必须检查数据库,但添加或订单的同一范围内删除或许新的订单。此问题不VAB具体的,你也会有同样的问题,你选择每一个框架。我已经写文章描述我们的复杂性在这些情况下,面临着(读取和寒战)。

The examples I give for the first question are also usable for this. You can use the same techniques: self validation and custom validator. Your scenario where you want to check a value in a database is actually a simple one, because the validity of your object is not based on its context. You can simply check the state of the object against the database. It gets more complicated when the context in which an object lives gets important (but it is possible with VAB). Imagine for instance that you want to write a validation that ensures that every customer, at a given moment in time, has no more than two unshipped orders. This not only means that you have to check the database, but perhaps new orders that are added or orders are deleted within that same context. This problem is not VAB specific, you will have the same problems with every framework you choose. I've written an article that describes the complexities we're facing with in these situations (read and shiver).

第三个问题:如何做你的人在现实世界中处理这种情况

Third question: How do you people in the real world deal with this situation?

我在生产中code做的这些类型的验证与VAB的。它的伟大工程,但VAB是不是很容易学。不过,我爱我们可以利用VAB做的,它只会变得更好时,V5.0出来。如果您想了解它,开始阅读ValidationHOL.pdf文档,你可以在<一个发现href="https://www.microsoft.com/downloads/details.aspx?FamilyID=2C34A9CB-17CF-4AEC-8DE6-EEACBBB74413&displaylang=en"相对=nofollow>动手实验室下载。

I do these types of validation with the VAB in production code. It works great, but VAB is not very easy to learn. Still, I love what we can do with VAB, and it will only get better when v5.0 comes out. When you want to learn it, start with reading the ValidationHOL.pdf document that you can found in the Hands-On Labs download.

我希望这有助于。

这篇关于验证框架.NET中,可以做字段之间的编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆