开发者

SelfValidation problem

I have a MyBO class which contains:

...
[RangeValidator(typeof(byte), "0", RangeBoundaryType.Inclusive, "20", Ran开发者_运维技巧geBoundaryType.Inclusive, Ruleset="validate_x")]
    public byte x
    {
        get;
        set;
    }

    [IgnoreNulls]
    [RangeValidator(typeof(byte), "0", RangeBoundaryType.Inclusive, "50", RangeBoundaryType.Inclusive, Ruleset = "validate_y")]
    public byte y
    {
        get;
        set;
    }

    [SelfValidation(Ruleset="validate_xy")]
    public void VerifyXY(ValidationResults results)
    {
        if (x < y)
        {
            results.AddResult(new ValidationResult("X cannot be < than Y!", this, "Verify", null, null));
        }
    }

The problem is that if in a test class i have:

    [TestMethod()]
    public void MyBOConstructorTest()
    {
        MyBO target = new MyBO() { x = 20, y = 23 };
        ValidationResults vr = Validation.Validate(target, "validate_xy");
        Assert.IsTrue(vr.IsValid);
    }

the test does not fail. Why? Because X is 20 and Y 23. So as you can see i'm using SelfValidation in the BO class.

Thanls


The most logical explanation I can think of is that you didn't decorate your MyBO class with the [HasSelfValidation] attribute. Without this attribute the VerifyXY method will not be called:

[HasSelfValidation]
public class MyBO
{
    // implementation
}

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜