asp.netasp.net-mvc-4metadatamodelmetadata

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 ?


Solution

  • 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);
    }