简体   繁体   中英

Extending CachedDataAnnotationsModelMetadataProvider to not cache some DataAnnotations

I'm trying to extend CachedDataAnnotationsModelMetadataProvider to not cache Custom ValidationAttribute . How can i achieve this? I tried looking in aspnetwebstack, but it is too complicated to get answer; what do i need to override, as protected override

protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(
            CachedDataAnnotationsModelMetadata prototype,
            Func<object> modelAccessor)

and

protected override CachedDataAnnotationsModelMetadata CreateMetadataPrototype(
            IEnumerable<Attribute> attributes,
            Type containerType,
            Type modelType,
            string propertyName)

And CachedAssociatedMetadataProvider<TModelMetadata> method

protected sealed override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)

is sealed. Any ideas ?

My problem was that client validations were not updated due to initialization of properties in constructor. So I moved property loading into

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
    var userSettings = ServiceLocator.Resolve<UserSettings>();
    if (userSettings != null)
    {
        // this was the required field to update
        this.StringLengthAttribute.MinimumLength = userSettings.MinRequiredPasswordLength;
    }

    yield return new ModelClientValidationStringLengthRule(this.ErrorMessage, this.StringLengthAttribute.MinimumLength, this.StringLengthAttribute.MaximumLength);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM