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); } } }
private Task Service_EntityPropertyUpdate(IDomainExecutionContext context, EntityPropertyUpdateEventArgs <T> e) { if (e.Property.Type == System.ComponentModel.DataAnnotations.CustomDataType.Password) { if (e.Value != null && (string)e.Value != "") { // if (e.Entity.IsNewCreated) // return Task.CompletedTask; // else // throw new ArgumentNullException("“" + e.Property.Name + "”不能为空。"); e.Entity.SetPassword((string)e.Value); } e.IsHandled = true; } return(Task.CompletedTask); }