Пример #1
0
        /// <summary>
        /// Updates the extendable object with values from the custom attribute collection.
        /// </summary>
        /// <param name="extendableToUpdate">The extendable to update.</param>
        /// <param name="customAttributeDetails">The custom attribute details.</param>
        /// <returns>The updated Extendable object</returns>
        /// <exception cref="CustomAttributeException">Unknown AttributeType for AttributeKey: {0}</exception>
        public IExtendable ValidateAndUpdateExtendable(IExtendable extendableToUpdate, IEnumerable <CustomAttributeDetail> customAttributeDetails, string updatedByUser)
        {
            if (customAttributeDetails == null || !customAttributeDetails.Any())
            {
                return(extendableToUpdate);
            }

            foreach (var customAttribute in customAttributeDetails)
            {
                try
                {
                    switch (customAttribute.Type)
                    {
                    case CustomAttributeType.Numeric:
                        extendableToUpdate.ValidateAndSetAttributeValue <Decimal>(customAttribute, customAttribute.Value == null ? 0 : Convert.ToDecimal(customAttribute.Value), updatedByUser);
                        break;

                    case CustomAttributeType.String:
                        extendableToUpdate.ValidateAndSetAttributeValue <String>(customAttribute, customAttribute.Value == null ? string.Empty : customAttribute.Value.ToString(), updatedByUser);
                        break;

                    case CustomAttributeType.Selection:
                        extendableToUpdate.ValidateAndSetAttributeValue <Int32>(customAttribute, customAttribute.Value == null ? 0 : Convert.ToInt32(customAttribute.Value), updatedByUser);
                        break;

                    case CustomAttributeType.DateTime:
                        extendableToUpdate.ValidateAndSetAttributeValue <DateTime>(customAttribute, customAttribute.Value == null || String.IsNullOrEmpty(customAttribute.Value.ToString()) ? DateTime.MinValue : Convert.ToDateTime(customAttribute.Value), updatedByUser);
                        break;

                    default:
                        throw new CustomAttributeException("Unknown AttributeType for AttributeKey: {0}", customAttribute.AttributeKey);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"Error setting attribute value for {customAttribute.AttributeKey} with error {ex.Message}");
                }
            }

            return(extendableToUpdate);
        }