Let's say I have an interface:
public interface ISomeInterface
{
bool SomeBool { get; set; }
string ValueIfSomeBool { get; set; }
}
And I have a number of classes that implement that. ie
public class ClassA : ISomeInterface
{
#region Implementation of ISomeInterface
public bool SomeBool { get; set; }
public string ValueIfSomeBool { get; set; }
#endregion
[NotNullValidator]
public string SomeOtherClassASpecificProp { get; set; }
}
And I have a Validation logic for the properties of this interface in a custom validator like so:
public class SomeInterfaceValidator : Validator<ISomeInterface>
{
public SomeInterfaceValidator (string tag)
: base(string.Empty, tag)
{
}
protected override string DefaultMessageTemplate
{
get { throw new NotImplementedException(); }
}
protected override void DoValidate(ISomeInterface objectToValidate, object currentTarget, string key, ValidationResults validationResults)
{
if (objectToValidate.SomeBool &&
string.IsNullOrEmpty(objectToValidate.ValIfSomeBool))
{
validationResults.AddResult(new ValidationResult("ValIfSomeBool cannot be null or empty when SomeBool is TRUE", currentTarget, key, string.Empty, null));
}
if (!objectToValidate.SomeBool &&
!string.IsNullOrEmpty(objectToValidate.ValIfSomeBool))
{
validationResults.AddResult(new ValidationResult("ValIfSomeBool must be null when SomeBool is FALSE", currentTarget, key, string.Empty, null));
}
}
}
And I have an attribute for applying this validator that I decorate ISomeInterface with.
[AttributeUsage(AttributeTargets.Interface)]
internal class SomeInterfaceValidatorAttribute : ValidatorAttribute
{
protected override Validator DoCreateValidator(Type targetType)
{
return new SomeInterfaceValidator(this.Tag);
}
}
When I call Validation.Validate it doesn't seem to be firing the validation in SomeInterfaceValidator. It does the validation specific to ClassA but not that of the interface ISomeInterface.
How do I get this to work?
EDIT: I found one way to get this to work and that is to do SelfValidation, where I cast to ISomeInterface and validate like so. This will suffice, but still leaving the question open to see if there are any other ways to accomplish this.
[SelfValidation]
public void DoValidate(ValidationResults results)
{
results.AddAllResults(Validation.Validate((ISomeInterface)this));
}
This is a limitation of the Validation Application Block. Here is an article that describes how to add Validator inheritance for VAB .
One approach to validate interfaces is to use the ValidationFactory
to create a Validator for the interface instead of using the Validator.Validate()
static facade or CreateValidator()
based on the concrete type. For example this should work given your approach:
var validator = ValidationFactory.CreateValidator<ISomeInterface>();
ValidationResults validationResults = validator.Validate(someInterfaceInstance);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.