How to Validate a Method through Validation Application Block

Topics: General discussion, Validation Application Block
Nov 25, 2010 at 10:39 AM

How we can validate a method in our business logic. I find two methods to validate a method these are

 

1. Self Validation

2. Method Return Type Validator

 

Nothing else.

So can anybody tells me how to validate a method or the parameters passed in the Method call. This parameter is a Business Object.

Nov 25, 2010 at 11:45 PM

Could you provide an example?  And you say you know you can do it using the two options you mentioned, why don't you use either one of them?

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 4:18 AM
[HasSelfValidation]
public class Customer
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    
    [SelfValidation]
    public void Validate(ValidationResults results)
    {
        if (string.IsNullOrEmpty(_name))
        {
            results.AddResult(
                new ValidationResult(
                    "Name cannot be null.",
                    this,
                    "Name",
                    null,
                    null
                )
            );
        }
    }
}
Nov 26, 2010 at 4:28 AM
Edited Nov 26, 2010 at 6:33 AM

This Example Has been Removed.

Nov 26, 2010 at 5:32 AM

You posted the code but didn't provide any explanation.  Sorry but I really don't understand what exactly is the problem.  The latest code you posted is messed up having a lot of syntax error and it seems you're applying validation attributes in incorrectly.  If you could provide less code and explain what is it which you are trying to achieve that would be great.  I can understand the first code you posted but I don't know what's giving you trouble in that one.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 5:36 AM

Sarah in the first example you will see that i am applying [HasSelfValidation] Attribute on class and [SelfValidation] attribute

on the method. In this case i have to write my own validation logic in the function.

But what i needed is that i will pass a BO in method and have some validation on the BO at method level and the validation will be done automatically by just calling the validation function.

Nov 26, 2010 at 6:30 AM

I see, so you want to validate at some point during method execution the parameter that was passed and not before it enters the method, is this correct?  If that's the case then is there any reason why you don't want to do this inside your method?

ValidationResults results = Validation.Validate(bo);

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 6:36 AM

Yes it is correct. Actually if we have a BO and we have some validation rule for that lets suppose It should not be null. But in the Method

i want to check whether it is in a particular range or not or something like that. So for that i have to check that into Method because it is specific to that method.

So how will i do it from the method through VAB.

Nov 26, 2010 at 7:17 AM

So you're only after validating a specific property in that BO object?   Does that validation have to be based on the validator defined for that property or you're looking for something like manually creating a validator?  For example:

StringLengthValidator validator = new StringLengthValidator(5, 20);

ValidationResults results = validator.Validate(bo.FirstName);

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 26, 2010 at 7:20 AM

No i am looking for validating a specific property. And is there any other way to do that other than you specified in your post.

And if i have to validate the whole parameter passed in the method so how could i do that?

Nov 26, 2010 at 7:25 AM

If you need to validate the parameter as a whole, then this one which I posted below will work:

ValidationResults results = Validation.Validate(bo);

If you need to validate a specific property based on the validation rules defined on it, you can refer to the code from this thread.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com