protected virtual async Task UpdateProperty(IValueProvider valueProvider, T entity, IPropertyMetadata property) { bool handled = false; bool hasValue = valueProvider.Keys.Contains(property.ClrName); object value; if (hasValue) { if (property.IsFileUpload) { value = valueProvider.GetValue <ISelectedFile>(property.ClrName); } else if (property.Type == CustomDataType.Password) { value = valueProvider.GetValue <string>(property.ClrName); } else { value = valueProvider.GetValue(property.ClrName, property.ClrType); } if (EntityPropertyUpdate != null) { var arg = new EntityPropertyUpdateEventArgs <T>(entity, valueProvider, property, value); await EntityPropertyUpdate(Context, arg); handled = arg.IsHandled; } } else { value = property.GetValue(entity); } if (!handled) { if (value != null && !property.ClrType.IsAssignableFrom(value.GetType())) { throw new NotImplementedException("未处理的属性“" + property.Name + "”。"); } ValidationContext validationContext = new ValidationContext(entity, Context.DomainContext, null); validationContext.MemberName = property.ClrName; validationContext.DisplayName = property.Name; var error = property.GetAttributes <ValidationAttribute>().Select(t => t.GetValidationResult(value, validationContext)).Where(t => t != ValidationResult.Success).ToArray(); if (error.Length > 0) { throw new ArgumentException(string.Join(",", error.Select(t => t.ErrorMessage))); } if (hasValue) { property.SetValue(entity, value); } } }
public IEnumerable <ValidationResult> Validate(IEntity entity, IEnumerable <IElement> relatedElements, IServiceProvider serviceProvider) { var validationAttributes = _property.GetAttributes <RelationValidationAttribute>(); var validationContext = new ValidationContext(entity, serviceProvider, default) { MemberName = _property.PropertyName }; return(validationAttributes .Select(attr => attr.IsValid(entity, relatedElements, validationContext)) .SelectNotNull(result => result as ValidationResult)); } }