public void Load(BinaryReader reader, Action <int, string> values, int count)
        {
            if (reader.ReadByte() != VERSION)
            {
                throw new Exception("Invalid StringIndexerPersist version.");
            }

            PersistMode mode = (PersistMode)reader.ReadByte();

            switch (mode)
            {
            case PersistMode.Raw:
            {
                new Raw().Load(reader, values, count);
            }
            break;

            case PersistMode.Dictionary:
            {
                string[] map = new string[reader.ReadInt32()];
                for (int i = 0; i < map.Length; i++)
                {
                    map[i] = reader.ReadString();
                }

                new Int32IndexerPersist().Load(reader, (idx, value) => { values(idx, value == NULL_ID ? null : map[value]); }, count);
            }
            break;

            default:
                throw new NotSupportedException(mode.ToString());
            }
        }
示例#2
0
 bool IDataPersistence.Persist(IPersistence persistence)
 {
     persistence.UpsertField(Constants.IDataPesistence_InitializeMode, InitializeMode.ToString());
     persistence.UpsertField(Constants.IDataPesistence_PersistMode, PersistMode.ToString());
     persistence.UpsertFieldArray(Constants.IDataPesistence_DataConnections, DataConnections.ToArray());
     return(true);
 }
示例#3
0
        private void DehydrateObjects()
        {
            List <PersistenceProperties> dehydrated = new List <PersistenceProperties>();

            // Examine each object
            foreach (KeyValuePair <string, object> obj in _persistentObjects)
            {
                if (obj.Value != null)
                {
                    // Notify object that it is about to be dehydrated
                    if (obj.Value is IPersistent && ApplicationMode == ApplicationMode.Deactivated)
                    {
                        (obj.Value as IPersistent).OnDeactivated();
                    }
                    else if (obj.Value is IPersistent && ApplicationMode == ApplicationMode.Closing)
                    {
                        (obj.Value as IPersistent).OnClosing();
                    }

                    IEnumerable <PropertyInfo> pi = null;

                    pi = obj.Value.GetType().GetProperties();


                    // Examine each property
                    foreach (PropertyInfo p in pi)
                    {
                        // Add any properties with Persist attribute
                        object[] atts = null;

                        atts = p.GetCustomAttributes(typeof(Persist), true);


                        if (atts != null && atts.Length > 0)
                        {
                            PersistMode mode = (atts[0] as Persist).PersistMode;
                            if ((this.ApplicationMode == ApplicationMode.Deactivated &&
                                 (mode == PersistMode.TombstonedOnly || mode == PersistMode.Both)) ||
                                (this.ApplicationMode == ApplicationMode.Closing &&
                                 (mode == PersistMode.ClosedOnly || mode == PersistMode.Both)))
                            {
                                var tsp = new PersistenceProperties(obj.Key, obj.Value.GetType().Name, p.Name, p.GetValue(obj.Value, null));

                                dehydrated.Add(tsp);
                            }
                        }
                    }
                }
            }

            dehydrated.TrimExcess();


            PersistenceStorage.StoreObjectToIsolated <List <PersistenceProperties> >(_persistenceProperties, dehydrated, true);
        }
        public void Store(BinaryWriter writer, Func <int, string> values, int count)
        {
            writer.Write(VERSION);

            int MAP_CAPACITY             = (int)((PERCENT / 100) * count);
            Dictionary <string, int> map = new Dictionary <string, int>(/*MAP_CAPACITY*/); //optimistic variant

            int ID = 0;

            int[]       indexes = new int[count];
            PersistMode mode    = PersistMode.Dictionary;

            for (int i = 0; i < count; i++)
            {
                var value = values(i);
                if (value == null)
                {
                    indexes[i] = NULL_ID;
                    continue;
                }

                int id;
                if (map.TryGetValue(value, out id))
                {
                    indexes[i] = id;
                    continue;
                }

                if (map.Count == MAP_CAPACITY)
                {
                    mode = PersistMode.Raw;
                    break;
                }

                map.Add(value, ID);
                indexes[i] = ID;
                ID++;
            }

            writer.Write((byte)mode);

            switch (mode)
            {
            case PersistMode.Raw:
            {
                new Raw().Store(writer, values, count);
            }
            break;

            case PersistMode.Dictionary:
            {
                writer.Write(map.Count);
                foreach (var kv in map.OrderBy(x => x.Value))
                {
                    writer.Write(kv.Key);
                }

                new Int32IndexerPersist().Store(writer, (idx) => { return(indexes[idx]); }, count);
            }
            break;

            default:
                throw new NotSupportedException(mode.ToString());
            }
        }
示例#5
0
 public Persist(PersistMode mode)
 {
     this.PersistMode = mode;
 }
 public Persist(PersistMode mode)
 {
     this.PersistMode = mode;
 }
示例#7
0
        private void Rehydrate(string uniqueName, object o)
        {
            try
            {
                IEnumerable <PropertyInfo> pi = null;

                pi = o.GetType().GetProperties();


                // Examine each property
                foreach (PropertyInfo p in pi)
                {
                    // Add any properties with Persist attribute
                    object[] atts = null;
                    atts = p.GetCustomAttributes(typeof(Persist), true);

                    if (atts != null && atts.Length > 0)
                    {
                        PersistMode mode = (atts[0] as Persist).PersistMode;
                        if (_rehydratedProperties != null &&
                            (this.ApplicationMode == ApplicationMode.Activated &&
                             (mode == PersistMode.TombstonedOnly || mode == PersistMode.Both)) ||
                            (this.ApplicationMode == ApplicationMode.Launching &&
                             (mode == PersistMode.ClosedOnly || mode == PersistMode.Both)))
                        {
                            // Get value
                            PersistenceProperties propertyPersisted = _rehydratedProperties.SingleOrDefault(r => r.UniqueName == uniqueName &&
                                                                                                            r.ClassName == o.GetType().Name&& r.PropertyName == p.Name);

                            if (propertyPersisted != null)
                            {
                                object value = propertyPersisted.Value;

                                Type nullableType = Nullable.GetUnderlyingType(p.PropertyType);
                                if (nullableType != null)
                                {
                                    if (nullableType.IsEnum)
                                    {
                                        value = Enum.ToObject(nullableType, value);
                                    }
                                    else
                                    {
                                        value = (value == null) ? null : Convert.ChangeType(value, nullableType, CultureInfo.CurrentCulture);
                                    }
                                }
                                else if (p.PropertyType.IsGenericType)
                                {
                                    value = Convert.ChangeType(value, p.PropertyType.GetGenericTypeDefinition(), CultureInfo.CurrentCulture);
                                }
                                else
                                {
                                    if (p.PropertyType.IsEnum)
                                    {
                                        value = Enum.ToObject(p.PropertyType, value);
                                    }
                                    else
                                    {
                                        value = Convert.ChangeType(value, p.PropertyType, CultureInfo.CurrentCulture);
                                    }
                                }

                                // Assign value
                                p.SetValue(o, value, null);

                                // remove from _rehydrated properties
                                _rehydratedProperties.Remove(propertyPersisted);
                            }
                        }
                    }
                }

                // Notify object that it has been hydrated
                //if (o is IPersistent)
                //{
                //    if (this.ApplicationMode == ApplicationMode.Activated)
                //    {
                //        (o as IPersistent).OnActivated(false, true);
                //    }
                //    else if (this.ApplicationMode == ApplicationMode.Launching)
                //    {
                //        (o as IPersistent).OnLaunched();
                //    }
                //}
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error setting value on reactivation " + e);
                // If anything has gone wrong notify the object to initialise normally
                //if (o is IPersistent)
                //    (o as IPersistent).OnLaunched();
            }
        }