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); } } }
//处理头像上传到本站 protected override async Task UpdateProperty(Member entity, IPropertyMetadata propertyMetadata) { if (propertyMetadata.CustomType == "SingleImage") { var oldImage = this.UpSingleImage(entity); if (oldImage != entity.Avatar) { propertyMetadata.SetValue(entity, oldImage); } } else { await base.UpdateProperty(entity, propertyMetadata); } }
/// <summary> /// Update property for entity. /// </summary> /// <param name="entity">Entity to update.</param> /// <param name="propertyMetadata">Property metadata.</param> /// <returns></returns> public async Task UpdateProperty(TEntity entity, IPropertyMetadata propertyMetadata) { if (propertyMetadata.EditRoles.Count() > 0 && !(propertyMetadata.AuthenticationRequiredMode == AuthenticationRequiredMode.All ? propertyMetadata.EditRoles.All(r => Controller.User.IsInRole(r)) : propertyMetadata.EditRoles.Any(r => Controller.User.IsInRole(r)))) { return; } if (propertyMetadata.IsFileUpload) { #region File Path Value if (!Controller.Request.Files.AllKeys.Contains(propertyMetadata.ClrName)) { return; } if (!(Controller is IFileController <TEntity>)) { if (propertyMetadata.ClrType != typeof(byte[])) { throw new NotSupportedException("Controller doesn't support upload file."); } if (Controller.Request.Files.AllKeys.Contains(propertyMetadata.ClrName)) { var file = Controller.Request.Files[propertyMetadata.ClrName]; MemoryStream stream = new MemoryStream(); await file.InputStream.CopyToAsync(stream); propertyMetadata.SetValue(entity, stream.ToArray()); stream.Close(); stream.Dispose(); } } else { await((IFileController <TEntity>)Controller).SaveFileToProperty(entity, propertyMetadata, Controller.Request.Files[propertyMetadata.ClrName]); } #endregion } else { if (!Controller.Request.Form.AllKeys.Contains(propertyMetadata.ClrName)) { return; } string value = Controller.Request.Form[propertyMetadata.ClrName]; TypeConverter converter = EntityValueConverter.GetConverter(propertyMetadata); if (converter == null) { if (propertyMetadata.ClrType.IsGenericType && propertyMetadata.ClrType.GetGenericTypeDefinition() == typeof(ICollection <>)) { converter = new Converter.CollectionConverter(); } else if (propertyMetadata.ClrType.IsEnum) { converter = new EnumConverter(propertyMetadata.ClrType); } else { converter = TypeDescriptor.GetConverter(propertyMetadata.ClrType); if (converter == null && propertyMetadata.Type != ComponentModel.DataAnnotations.CustomDataType.Password) { throw new NotSupportedException("Type of \"" + propertyMetadata.ClrName + "\" converter not found."); } } } if (propertyMetadata.Type == ComponentModel.DataAnnotations.CustomDataType.Password && entity is IPassword) { object v = propertyMetadata.GetValue(entity); if (v == null || value != v.ToString()) { ((IPassword)entity).SetPassword(value); } } else { EntityValueConverterContext context = new EntityValueConverterContext(EntityBuilder.DescriptorContext, propertyMetadata); object cvalue; try { cvalue = converter.ConvertFrom(context, null, value); } catch (Exception ex) { Controller.ViewBag.ErrorMessage = ex.Message; return; } if (converter.GetType() == typeof(Converter.CollectionConverter)) { object collection = propertyMetadata.GetValue(entity); ((dynamic)collection).Clear(); var addMethod = collection.GetType().GetMethod("Add"); object[] array = (object[])cvalue; for (int a = 0; a < array.Length; a++) { addMethod.Invoke(collection, new object[] { array[a] }); } } else { propertyMetadata.SetValue(entity, cvalue); } } } }