Example #1
0
        /// <summary>
        /// For the specified Type and original state map, this method returns a state map containing
        /// only values that should be roundtripped (based on RoundtripOriginal member attribution).
        /// </summary>
        /// <param name="type">The Type the state map is for.</param>
        /// <param name="state">The original state map.</param>
        /// <returns>The state map containing only values that should be rountripped.</returns>
        internal static IDictionary <string, object> ExtractRoundtripState(Type type, IDictionary <string, object> state)
        {
            MetaType metaType = MetaType.GetMetaType(type);
            Dictionary <string, object> resultRoundtripState = new Dictionary <string, object>();

            foreach (MetaMember metaMember in metaType.DataMembers)
            {
                if (!metaMember.IsRoundtripMember)
                {
                    // only copy RTO state
                    continue;
                }

                if (!metaMember.IsComplex)
                {
                    // for simple members, just copy the state over directly
                    resultRoundtripState.Add(metaMember.Name, state[metaMember.Name]);
                }
                else
                {
                    // if the member is complex we need to preprocess and apply values recursively
                    if (!metaMember.IsCollection)
                    {
                        IDictionary <string, object> originalState = (IDictionary <string, object>)state[metaMember.Name];
                        if (originalState != null)
                        {
                            IDictionary <string, object> roundtripState = ExtractRoundtripState(metaMember.PropertyType, originalState);
                            resultRoundtripState.Add(metaMember.Name, roundtripState);
                        }
                    }
                    else
                    {
                        IEnumerable originalCollection = (IEnumerable)state[metaMember.Name];
                        if (originalCollection != null)
                        {
                            Type  elementType   = TypeUtility.GetElementType(metaMember.PropertyType);
                            IList newCollection = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType));

                            // Create a copy collection and copy elements recursively. Since Entity Extract/Apply state isn't
                            // deep through complex type collections, we have to recursively do the RTO filtering here.
                            foreach (object element in originalCollection)
                            {
                                IDictionary <string, object> originalState = ObjectStateUtility.ExtractState(element);
                                if (originalState != null)
                                {
                                    IDictionary <string, object> roundtripState = ExtractRoundtripState(elementType, originalState);
                                    object newInstance = Activator.CreateInstance(elementType);
                                    ObjectStateUtility.ApplyState(newInstance, roundtripState);
                                    newCollection.Add(newInstance);
                                }
                            }

                            resultRoundtripState.Add(metaMember.Name, newCollection);
                        }
                    }
                }
            }

            return(resultRoundtripState);
        }
Example #2
0
 /// <summary>
 /// Called whenever a data member on the instance is modified, to allow
 /// the session to take a state snapshot.
 /// </summary>
 public void OnDataMemberUpdate()
 {
     if (this._snapshot == null)
     {
         // we're currently in an Edit session, so we need to take a snapshot
         this._snapshot = ObjectStateUtility.ExtractState(this._instance);
     }
 }
        internal static Entity GetRoundtripEntity(Entity entity)
        {
            Entity roundtripEntity = (Entity)Activator.CreateInstance(entity.GetType());
            IDictionary <string, object> roundtripState = ObjectStateUtility.ExtractRoundtripState(entity.GetType(), entity.OriginalValues);

            roundtripEntity.ApplyState(roundtripState);

            return(roundtripEntity);
        }
Example #4
0
            /// <summary>
            /// Cancels the edit session, reverting all changes made to the
            /// entity since the session began.
            /// </summary>
            public void Cancel()
            {
                // revert any data member modifications
                if (this._snapshot != null)
                {
                    ObjectStateUtility.ApplyState(this._instance, this._snapshot);
                }

                // Revert the validation errors and notify our parent
                ValidationUtilities.ApplyValidationErrors(this._instance, this._validationErrors);
                this._instance.NotifyParentMemberValidationChanged(null, this._validationErrors);
            }
Example #5
0
 /// <summary>
 /// Applies the specified state to the specified object.
 /// </summary>
 /// <param name="o">The object to apply state to.</param>
 /// <param name="stateToApply">The state dictionary.</param>
 internal static void ApplyState(object o, IDictionary <string, object> stateToApply)
 {
     ObjectStateUtility.ApplyState(o, stateToApply, null, LoadBehavior.RefreshCurrent);
 }