IDataErrorInfo and validation block

Topics: Validation Application Block
Apr 26, 2009 at 9:27 PM
Hi all,

I encouter a problem with implementing the IDataErrorInfo using the validation block.
1. I declared a base class that implements IDataErrorInfo interfase.
2. In the indexer method I call a method on interface witch makes the validation.

Here is the Base calss :

 

public class BusinessObjectsBaseClass IDataErrorInfo{
[
Dependency]
public IValidator Validator { get; set; }

#region

 

IDataErrorInfo Members
public string Error
{
get { return string.Empty; }
}

 

 

public string this[string columnName]
{
get { return ValidateOverride(columnName, this); }
}

 

 

protected virtual string ValidateOverride(string propertyName, object instance)
{
return Validator.Validate(propertyName, instance);
}
#endregion
}

 

I have also a class witch implements the IValidator and uses the validation block as follows:

public

 

class EnterpriseLibraryValidator : IValidator{

 

#region

 

IValidator Members
public string Validate(string propertyName, object instance)
{
ValidationResults results = Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate(instance);
var validationResult = results.Where(v => v.Key == propertyName);
if (validationResult.Count() != 0)
return validationResult.First().Message;
else
return string.Empty;
}
#endregion
}

 

When I pass any instanse of my business object witch have validation attributes , the validation doesn't work. If I define strongle the type in the Validate method then It works, like
ValidationResults results = Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate<MyObject>(instance as MyObject);

Alsoi tried to use generic type and the same problem.

Any suggestions.
Thanks...

 

Apr 27, 2009 at 10:03 AM
Hi,

I think it wont work because the Validation Application Block will try to create a validator for the type object and not the specific type that you expect. Replace the 

ValidationResults results = Microsoft.Practices.EnterpriseLibrary.Validation.Validation.Validate(instance);

with

 

Validator validator = ValidationFactory.CreateValidator(instance.GetType());
ValidationResults results = validator.Validate(instance);

This works fine for me, please see if this also works for you.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Apr 27, 2009 at 12:34 PM
Thanks you very much. It works !!!