I’m wondering if anyone knows if xVal will work as expected if I define my system.componentmodel.dataannotations attributes on interfaces that are implemented by my model classes, instead of directly on the concrete model classes.
public interface IFoo
{
[Required] [StringLength(30)]
string Name { get; set; }
}
and then in my model class there wouldn’t be any validation attributes…
public class FooFoo : IFoo
{
public string Name { get; set; }
}
If I try to validate a FooFoo with xVal, will it use the attribs from its interface?
At the moment the
xVal.RuleProviders.DataAnnotationsRuleProvideronly looks at properties defined on the model class itself. You can see this in the methodGetRulesFromPropertyin the rule provider base classPropertyAttributeRuleProviderBase:The
propertyDescriptorparameter represents a property in your model class and itsAttributesproperty represents only the attributes defined directly on the property itself.However, you could of course extend
DataAnnotationsRuleProviderand override the appropriate method to make it do what you want: extract validation attributes from implemented interfaces. You then register your rule provider with xVal:To get attributes from properties in implemented interfaces, you should extend
DataAnnotationsRuleProviderand overrideGetRulesFromTypeCore. It gets a parameter of typeSystem.Typethat has a methodGetInterfaces.