Esempio n. 1
0
        public void SaveToFile()
        {
            TracerHelper.Trace("[" + _fullFileName + "] invoked by: " + ReflectionHelper.GetFullCallingMethodName(2));

            lock (this)
            {
                if (File.Exists(_fullFileName))
                {// Rename the old file - keep it as archive.
                    TimeSpan span            = DateTime.Now - new DateTime(DateTime.Now.Year, 1, 1);
                    string   customTimeValue = "." + DateTime.Now.Year + "." + (int)span.TotalSeconds;
                    string   newFileName     = _fullFileName.ToLower().Replace("." + FileExtension, customTimeValue) + "." + FileExtension;

                    while (File.Exists(newFileName))
                    {
                        newFileName = newFileName.Replace("." + FileExtension, "X" + "." + FileExtension);
                    }

                    File.Move(_fullFileName, newFileName);
                }

                try
                {
                    FileInfo fi = new FileInfo(_fullFileName);
                    if (fi.Directory.Exists == false)
                    {
                        fi.Directory.Create();
                    }

                    using (FileStream stream = new FileStream(_fullFileName, FileMode.Create, FileAccess.Write))
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, _pendingSaveObjects);
                        stream.Close();
                    }
                }
                catch (Exception exception)
                {
                    System.Diagnostics.Debug.Fail("Stream error.");
                    TracerHelper.TraceError("Error occured while creating file stream [" + exception.Message + "].");
                }
            }
        }
Esempio n. 2
0
        public bool RestoreFromFile()
        {
            TracerHelper.Trace("[" + _fullFileName + "] invoked by: " + ReflectionHelper.GetFullCallingMethodName(2));

            lock (this)
            {
                _restoredObjects.Clear();

                if (File.Exists(_fullFileName) == false)
                {
                    return(false);
                }

                try
                {
                    using (FileStream stream = new FileStream(_fullFileName, FileMode.Open, FileAccess.Read))
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        _restoredObjects = (Dictionary <string, PersistentData>)formatter.Deserialize(stream);
                        stream.Close();
                    }

                    // Transfer the restored info in the pending info places, where no info exists at all, to persist objects
                    // that are not currently active and will be restorable in the furure.
                    foreach (string id in _restoredObjects.Keys)
                    {
                        if (_pendingSaveObjects.ContainsKey(id) == false)
                        {
                            _pendingSaveObjects.Add(id, _restoredObjects[id]);
                        }
                    }

                    return(true);
                }
                catch (Exception exception)
                {
                    System.Diagnostics.Debug.Fail("Stream error.");
                    TracerHelper.TraceError("Error occured while parsing file stream [" + exception.Message + "].");
                }
            }
            return(false);
        }
Esempio n. 3
0
        public void RegisterObject(IPersistentEx persistent)
        {
            lock (this)
            {
                if (persistent.PersistencyIsInitialzed == false)
                {
                    TracerHelper.Trace("[" + ActualPersistentId(persistent) + "] invoked by: " + ReflectionHelper.GetFullCallingMethodName(2));

                    persistent.InitializePersistency(this);

                    if (_pendingSaveObjects.ContainsKey(ActualPersistentId(persistent)) == false)
                    {
                        _pendingSaveObjects.Add(ActualPersistentId(persistent), new PersistentData());
                    }

                    SystemMonitor.CheckThrow(_verificationObjects.ContainsKey(ActualPersistentId(persistent)) == false);
                    _verificationObjects.Add(ActualPersistentId(persistent), persistent);
                }

                System.Diagnostics.Debug.Assert(_verificationObjects.ContainsKey(ActualPersistentId(persistent)));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Allows the user of this interface to force an update to an object.
        /// Also the object itself can raise an request to be updated (saved).
        /// </summary>
        public bool RestoreObjectState(IPersistentEx persistent)
        {
            TracerHelper.Trace("[" + ActualPersistentId(persistent) + "] invoked by :" + ReflectionHelper.GetFullCallingMethodName(3));

            lock (this)
            {
                string persistentName = ActualPersistentId(persistent);
                SystemMonitor.CheckThrow(_verificationObjects.ContainsKey(persistentName) == false || _verificationObjects[persistentName] == persistent);

                if (_restoredObjects.ContainsKey(persistentName) == false
                    /*|| _restoredObjects[persistentName].Values.Count == 0*/)
                {// Object restoration infromation not found.
                    return(false);
                }

                PersistentData dataCopy = _restoredObjects[persistentName].Clone();
                bool           result   = persistent.OnRestoreState(this, dataCopy);

                if (result)
                {// Object restored successfully, remove pending restoration information.
                    _restoredObjects.Remove(persistentName);
                }

                return(result);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Allows the user of this interface to force an update to an object.
        /// Also the object itself can raise an request to be updated (saved).
        /// </summary>
        public bool SaveObjectState(IPersistentEx persistent)
        {
            TracerHelper.Trace("[" + ActualPersistentId(persistent) + "] invoked by: " + ReflectionHelper.GetFullCallingMethodName(2));

            lock (this)
            {
                RegisterObject(persistent);
                PersistentData data = _pendingSaveObjects[ActualPersistentId(persistent)];
                if (persistent.OnSaveState(this, data) == false)
                {
                    return(false);
                }

                // Only if the receiver has confirmed the saving operation, replace the current data.
                _pendingSaveObjects[ActualPersistentId(persistent)] = data;
            }

            return(true);
        }