0

Helllo..... I am quite new in Microsoft Enterprise Library Validation Framework. My question is that I want same validation condition in two different RuleSet. Is it possible to put two rule set with in the same Validator like below

/// <summary>
/// 
/// </summary>
[StringLengthValidator(1,25,Ruleset="DetailRuleSet",Ruleset="MainRuleSet",Tag="First Name")]
public string FirstName
{
    get { return firstName; }
    set { firstName = value; }
}

or I have mentioned it by writing the same in two time with different ruleset name like below

    /// <summary>
    /// 
    /// </summary>
    [StringLengthValidator(1,25,Ruleset="DetailRuleSet",Tag="First Name")]
    [StringLengthValidator(1, 25, Ruleset = "MainRuleSet", Tag = "First Name")]
    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

Any help would be appreciated!!

Jatin Khurana
  • 1,155
  • 8
  • 15
  • Enterprise Library doesn't allow the first one so if i have to put one validator on the 10 ruleset then i have to write the validator 10 times............ Is there any other way to do that?? – Jatin Khurana May 19 '13 at 05:14
  • did you check this link: http://msdn.microsoft.com/en-us/library/ff650174.aspx – Saravanan May 19 '13 at 08:19
  • @saravanan I am talking about the Setting Validation attribute not about the Validate method. I want to know Is there any way to write multiple ruleset in the same Validator(note-: in validator not in validate method as I mentioned above) – Jatin Khurana May 19 '13 at 16:50

1 Answers1

0

First a disclaimer: I haven't worked so much on the Enterprise Library Application Validation Block, however, having been a programmer for over a decade and a half, and having used Validation models from ASP.NET to MVC Data Annotations, I can tell you that the API for validation in Enterprise Library is pretty similar. It took me about 20 minutes to download the Enterprise Library source code and look up the answer to this question. So, here's my answer.

Yes, you can apply more than one validation attribute to a given model property, each validation attribute specifying a different rule set.

However, in such a case, you will have to explicitly invoke the validator on the model type for that particular rule set.

If you do not do that, the Enterprise Library will execute the validator for the default rule-set.

In the context of your example, you can say:

StringLengthValidator(1,25,Ruleset="DetailRuleSet",Tag="First Name")]
    [StringLengthValidator(1, 25, Ruleset = "MainRuleSet", Tag = "First Name")]
    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

However, in this case, you have to specifically invoke one of the rule-sets for validation, like so:

var yourModelObjectValidator = 
    yourValidatorFactory.CreateValidator<YourModelClass>("yourRuleSetName");

var yourModelObject = 
    new YourModelClass { Foo = "foo", Bar = "bar", Gar = 2 };

var results = 
    yourModelObjectValidator.Validate(yourModelObject);

if (!results.IsValid)
{
   foreach(var result in results) 
    { 
        /* run the state machine, do whatever, print */ 
    }
}

If you do not specify the rule set name like we did above, the Enterprise Library will execute your validations in the context of a default rule set which has no name, and hence none of the two rules you specified above using the validation attributes will get executed.

UPDATE

Based on your comment, I see what your real question is.

Your question then really is: Can I specify more than a single rule-set in a single validation attribute declaration?

The answer is as simple as the question: No. Because the property RuleSet is declared simply as string and not as IEnumerable<string> in the BaseValidationAttribute class, the mother of all ValidatorAttribute classes in the EntLib source code.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • Cooler............. yes u r right........ but above in my code both the Validator is doing the same thing........ why I add it two times....... If I want same validator in 20 ruleset the I have to write it 20 times above a property....... Is there any way I have to write it once and put all the ruleset in that – Jatin Khurana May 20 '13 at 18:28
  • Yes........ u r right... we can't do that but I think Microsoft should provide that otherwise One have to write a lot of repetitive code(a lot of repetitive attribute) – Jatin Khurana May 21 '13 at 07:42
  • You're right. It should. For instance, there's no class in the EntLib that represents a RuleSet, and hence there is no way to traverse or navigate from a rule-set to its associated validators. No one likes EntLib. Stop using EntLib. Use Data Annotations provided by the Entity Framework instead. http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx – Water Cooler v2 May 21 '13 at 11:11
  • You're right. It should. For instance, there's no class in the EntLib that represents a RuleSet, and hence there is no way to traverse or navigate from a rule-set to its associated validators. No one like EntLib. Stop using EntLib. Use Data Annotations provided by the Entity Framework instead. http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx – Water Cooler v2 May 21 '13 at 11:12