Exemple #1
0
        protected virtual async Task UpdateProperty(IValueProvider valueProvider, T entity, IPropertyMetadata property)
        {
            bool   handled  = false;
            bool   hasValue = valueProvider.ContainsKey(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);
                }
                var arg = new EntityPropertyUpdateEventArgs <T>(entity, valueProvider, property, value);
                await RaiseAsyncEvent(EntityPropertyUpdateEvent, 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 DomainServiceException(new ArgumentException(string.Join(",", error.Select(t => t.ErrorMessage))));
                }
                if (hasValue)
                {
                    property.SetValue(entity, value);
                }
            }
        }