开发者

Enterprise Library validate all rulesets

I have a question validating objects using Microsoft Enterprise Library.

I have some validators for different fields. But I need the different fields in the object to be in different rulesets (like in this example):

public class Category
{
[NotNullValidator(MessageTemplate="The category name cannot be null", Ruleset="NameRuleset")]
[StringLengthValidator(1, 200, MessageTemplate = "The category name must be between {3} and {5} characters", Ruleset = "NameRuleset")]
public string Name { get; set; }

[NotNullValidator(MessageTemplate = "The category description cannot be null", Ruleset = "DescriptionRuleset")]
[StringLengthValidator(1, 2000, MessageTemplate = "The category description must be between {3} and {5} characters", Ruleset = "DescriptionRuleset")]
public string Description { get; set; }
}

The question is how do I validate the object so that all the rulesets are taken into consideration?

One solution would be this, but I don't want to pass the rulesets as parameters all the time.

v开发者_如何学Pythonar result = Validation.Validate<Category>(category, new string[] { "NameRuleset", "DescriptionRuleset" });

And this will only validate the default ruleset (for my example, the object will always be valid, even if its name is longer than 200 characters)

var result = Validation.Validate<Category>(category);

So is there any way to validate all the rulesets without manually specifying them for each call of Validation.Validate()?


You can have multiple rulesets on your properties. Thus, it is posible to introduce a CombinedRuleset which you use for both Name and Description properties on top of your existing rulesets. This, however, requires duplicating all your validation rules, which is a code smell.

If you would like a ruleset joining feature to be added to EntLib in a future release, suggest it here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜