2

我一直在与这个问题作斗争,我最初认为这可能与影响我的验证的多态性/继承有关,但我把它缩小到这个......

这是类结构..

public class Employee {

    [ObjectValidator(Ruleset = "A")]
    public EmployeeName Name { get; set; }

    public Employee()
    {
        Name = new EmployeeName();
    }
}

public class EmployeeName
{
    [StringLengthValidator(1,20,Ruleset = "A")]
    public string First { get; set; }

    public string Last { get; set; }

    public EmployeeName()
    {
        First = string.Empty;
        Last = string.Empty;
    }
}

考试:

[TestFixture]
public class ObjectValidationWithRulesets
{
    [Test]
    public void wont_validate_with_a_ruleset()
    {
        var person = new Employee()
        {
            Name = new EmployeeName()
            {
                First = string.Empty, 
                Last = string.Empty
            }
        };

        var ruleSetValidator =
            ValidationFactory.CreateValidator<Employee>("A");

        var validationResults = ruleSetValidator.Validate(person);

        Assert.That(!validationResults.IsValid,
            "Validation with rulsets failed");
    }
}

如果我删除规则集的内容,则此测试通过。但是一旦应用了规则集,我就无法让对象正确验证。

任何人都可以对此有所了解吗?

干杯,

4

1 回答 1

2

我也有这个问题,但是我没有在配置文件中定义targetRuleSet。我通过更正声明 ObjectValidator 属性的方式解决了这个问题。对我有用的正确语法如下

// Correct
[ObjectValidator("RuleSetA", Ruleset = "RuleSetA")]

在我的代码中,我错误地声明如下

// Wrong syntax
[ObjectValidator(Ruleset = "RuleSetA")]
于 2010-08-12T11:13:19.570 回答