Example #1
0
        void IMobileObject.SetState(SerializationInfo info)
        {
            string type = (string)info.Values["_businessObjectType"].Value;
            Type   businessObjecType = Type.GetType(type);

            SetPropertyList(businessObjecType);
            _fieldData = new IFieldData[_propertyList.Count];

            foreach (IPropertyInfo property in _propertyList)
            {
                if (info.Values.ContainsKey(property.Name))
                {
                    SerializationInfo.FieldData value = info.Values[property.Name];

                    IFieldData data = GetOrCreateFieldData(property);
                    data.Value = value.Value;
                    if (!value.IsDirty)
                    {
                        data.MarkClean();
                    }
                }
            }

            OnSetState(info);
        }
Example #2
0
        /// <summary>
        /// Override this method to retrieve your field values
        /// from the MobileFormatter serialzation stream.
        /// </summary>
        /// <param name="info">
        /// Object containing the data to serialize.
        /// </param>
        /// <param name="mode">
        /// The StateMode indicating why this method was invoked.
        /// </param>
        protected override void OnSetState(SerializationInfo info, StateMode mode)
        {
            string type = (string)info.Values["_businessObjectType"].Value;
            Type   businessObjecType = Csla.Reflection.MethodCaller.GetType(type);

            SetPropertyList(businessObjecType);

            if (mode == StateMode.Serialization)
            {
                _stateStack.Clear();
                if (info.Values.ContainsKey("_stateStack"))
                {
                    //string xml = info.GetValue<string>("_stateStack");
                    byte[] xml = info.GetValue <byte[]>("_stateStack");
                    MobileList <SerializationInfo> list   = (MobileList <SerializationInfo>)MobileFormatter.Deserialize(xml);
                    SerializationInfo[]            layers = list.ToArray();
                    Array.Reverse(layers);
                    foreach (SerializationInfo layer in layers)
                    {
                        _stateStack.Push(layer);
                    }
                }
            }

            // Only clear this list on serialization, otherwise you'll lose
            // your children during an undo.
            if (mode == StateMode.Serialization)
            {
                _fieldData = new IFieldData[_propertyList.Count];
            }

            foreach (IPropertyInfo property in _propertyList)
            {
                if (info.Values.ContainsKey(property.Name))
                {
                    SerializationInfo.FieldData value = info.Values[property.Name];

                    IFieldData data = GetOrCreateFieldData(property);
                    if (value.Value != null &&
                        mode == StateMode.Undo &&
                        typeof(IMobileObject).IsAssignableFrom(Nullable.GetUnderlyingType(property.Type) ?? property.Type) &&
                        !typeof(IUndoableObject).IsAssignableFrom(Nullable.GetUnderlyingType(property.Type) ?? property.Type))
                    {
                        data.Value = MobileFormatter.Deserialize((byte[])value.Value);
                    }
                    else
                    {
                        data.Value = value.Value;
                    }

                    if (!value.IsDirty)
                    {
                        data.MarkClean();
                    }
                }
                else if (mode == StateMode.Undo && !((property.RelationshipType & RelationshipTypes.PrivateField) == RelationshipTypes.PrivateField))
                {
                    IFieldData data = GetFieldData(property);
                    if (data != null)
                    {
                        if (!info.Values.ContainsKey("child_" + property.Name) || !info.GetValue <bool>("child_" + property.Name))
                        {
                            _fieldData[property.Index] = null;
                        }

                        // We don't want to reset children during an undo.
                        else if (!typeof(IMobileObject).IsAssignableFrom(data.Value.GetType()))
                        {
                            data.Value = property.DefaultValue;
                        }
                    }
                }
            }

            base.OnSetState(info, mode);
        }
Example #3
0
        /// <summary>
        /// Override this method to retrieve your field values
        /// from the MobileFormatter serialzation stream.
        /// </summary>
        /// <param name="info">
        /// Object containing the data to serialize.
        /// </param>
        /// <param name="mode">
        /// The StateMode indicating why this method was invoked.
        /// </param>
        protected override void OnSetState(SerializationInfo info, StateMode mode)
        {
            string type = (string)info.Values["_businessObjectType"].Value;
            Type   businessObjecType = Csla.Reflection.MethodCaller.GetType(type);

            SetPropertyList(businessObjecType);

            if (mode == StateMode.Serialization)
            {
                _stateStack.Clear();
                if (info.Values.ContainsKey("_stateStack"))
                {
                    var stackArray = info.GetValue <byte[][]>("_stateStack");
                    foreach (var item in stackArray.Reverse())
                    {
                        _stateStack.Push(item);
                    }
                }

                _fieldData = new IFieldData[_propertyList.Count];
            }

            foreach (IPropertyInfo property in _propertyList)
            {
                if (info.Values.ContainsKey(property.Name))
                {
                    SerializationInfo.FieldData value = info.Values[property.Name];

                    IFieldData data = GetOrCreateFieldData(property);
                    if (value.Value != null &&
                        mode == StateMode.Undo &&
                        typeof(IMobileObject).IsAssignableFrom(Nullable.GetUnderlyingType(property.Type) ?? property.Type) &&
                        !typeof(IUndoableObject).IsAssignableFrom(Nullable.GetUnderlyingType(property.Type) ?? property.Type))
                    {
                        data.Value = SerializationFormatterFactory.GetFormatter().Deserialize((byte[])value.Value);
                    }
                    else
                    {
                        data.Value = value.Value;
                    }

                    if (!value.IsDirty)
                    {
                        data.MarkClean();
                    }
                }
                else if (mode == StateMode.Undo && !((property.RelationshipType & RelationshipTypes.PrivateField) == RelationshipTypes.PrivateField))
                {
                    IFieldData data = GetFieldData(property);
                    if (data != null)
                    {
                        if (!info.Values.ContainsKey("child_" + property.Name) || !info.GetValue <bool>("child_" + property.Name))
                        {
                            _fieldData[property.Index] = null;
                        }

                        // We don't want to reset children during an undo.
                        else if (!typeof(IMobileObject).IsAssignableFrom(data.Value.GetType()))
                        {
                            data.Value = property.DefaultValue;
                        }
                    }
                }
            }
            base.OnSetState(info, mode);
        }