public void BeginSession()
        {
            if (CurrentSession != null)
                throw new InvalidOperationException("A session is still in progress. End the active session before beginning a new session.");

            CurrentSession = new HistorySession();
        }
        public void EndSession()
        {
            if (CurrentSession != null)
            {
                CurrentSession.Close();
                _RedoSessions.Clear();
                _UndoSessions.Add(CurrentSession);
                while (_UndoSessions.Count > MaxSessionCount)
                    _UndoSessions.RemoveAt(0);

                CurrentSession = null;
            }
        }
        public void Redo()
        {
            if (_RedoSessions.Count == 0)
                return;

            var lastSession = _RedoSessions.Pop();

            var undoSession = new HistorySession();
            _UndoSessions.Add(undoSession);

            while (lastSession.Entries.Count > 0)
            {
                var currentEntry = lastSession.Entries.Pop();
                if (currentEntry.Action == HistoryAction.PropertiesChanged)
                {
                    var entity = EntityService.Instance.GetEntity(currentEntry.ChangedEntityId);
                    if (entity != null)
                    {
                        undoSession.SaveOldPropertyValues(entity.EntityId, entity.GetPropertyValues());
                        entity.SetPropertyValues(currentEntry.ChangedPropertyValues);
                    }
                }
                else if (currentEntry.Action == HistoryAction.EntityDeleted)
                {
                    EntityService.Instance.ReInsert(currentEntry.DeletedEntity, undoSession);
                }
                else if (currentEntry.Action == HistoryAction.EntityInserted)
                {
                    EntityService.Instance.ReInsert(currentEntry.InsertedEntity, undoSession);
                }
            }

            undoSession.Close();
        }