예제 #1
0
        /// <summary>
        /// Use this method to set the properties on the wrapped entity
        /// </summary>
        /// <param name="propertiesFromViewState"></param>
        /// <param name="wrapper"></param>
        /// <param name="overwriteSameValue"></param>
        internal void SetAllProperties(Dictionary <string, object> propertiesFromViewState, bool overwriteSameValue,
                                       ref Dictionary <string, Exception> propertySettingExceptionsCaught)
        {
            // We aggregate the reference descriptors rather than setting them directly
            // to account for compound keys (we need all components of the key to create
            // an EntityKey that can be set on the EntityReference)
            var referenceList = new List <KeyValuePair <EntityDataSourceReferenceKeyColumn, object> >();

            foreach (EntityDataSourceWrapperPropertyDescriptor descriptor in _collection.AllPropertyDescriptors)
            {
                // figure out which display name to match for this descriptor
                string displayName = descriptor.Column.DisplayName;

                // if we have a controlling column, use its display name instead
                if (descriptor.Column.ControllingColumn != null)
                {
                    displayName = descriptor.Column.ControllingColumn.DisplayName;
                }

                object value;
                if (propertiesFromViewState.TryGetValue(displayName, out value))
                {
                    // get all changed ReferencePropertyDescriptor from ViewState
                    EntityDataSourceReferenceKeyColumn referenceColumn = descriptor.Column as EntityDataSourceReferenceKeyColumn;

                    // convert the value as needed
                    object adjustedValue = EntityDataSourceUtil.ConvertType(value, descriptor.PropertyType, descriptor.DisplayName);

                    if (null != referenceColumn)
                    {
                        referenceList.Add(new KeyValuePair <EntityDataSourceReferenceKeyColumn, object>(
                                              referenceColumn, adjustedValue));
                        continue;
                    }

                    if (overwriteSameValue || adjustedValue != descriptor.GetValue(this))
                    {
                        if (EntityDataSourceUtil.NullCanBeAssignedTo(descriptor.PropertyType) || null != adjustedValue)
                        {
                            try
                            {
                                descriptor.SetValue(this, adjustedValue);
                            }
                            catch (Exception e)
                            {
                                // The property descriptor uses reflection to set the property. Therefore, the inner exception contains the actual message.
                                Exception exceptionToThrow = e;
                                if (e.InnerException != null)
                                {
                                    exceptionToThrow = e.InnerException;
                                }
                                if (null == propertySettingExceptionsCaught)
                                {
                                    propertySettingExceptionsCaught = new Dictionary <string, Exception>();
                                }
                                propertySettingExceptionsCaught.Add(descriptor.DisplayName, exceptionToThrow);
                            }
                        }
                    }
                }
            }

            // aggregate setting for EntityKey
            SetEntityKeyProperties(referenceList, overwriteSameValue);
        }