private void commitTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try {
                FileLock.WaitOne();
                T [] entityCache = EntityCache;
                if (entityCache == null)
                {
                    return;
                }

                if (DatabaseCategory != null)
                {
                    System.Diagnostics.Debug.WriteLine("Delayed writing changes in xml entities to database.");
                    CustomConfig.CommitChanges(PrepareToDatabase(entityCache), DatabaseCategory, databaseProfile);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("Delayed writing changes in xml entities to file {0}.", EntitiesFile));
                    DataProvider.SaveAllObjectsToXML(EntitiesFile, entityCache);
                }
            } catch (Exception ex) {
                ErrorHandling.LogException(ex);
            } finally {
                FileLock.ReleaseMutex();
            }
        }
        protected void FlushCache()
        {
            if (!LazyCommit || CommitTimer == null)
            {
                return;
            }

            try {
                FileLock.WaitOne();

                if (CommitTimer.Enabled)
                {
                    CommitTimer.Stop();
                    commitTimer_Elapsed(null, null);
                }
            } finally {
                FileLock.ReleaseMutex();
            }
        }
        public virtual T CommitChanges()
        {
            if (!IsDirty)
            {
                return((T)this);
            }

            try {
                FileLock.WaitOne();
                List <T>           allObjects = new List <T> (GetAllEntities(false));
                DbColumnManager [] managers   = DataProvider.GetDBManagers(typeof(T));
                T   thisObject = (T)this;
                int index      = DataProvider.FindObjectIndexByField(allObjects, XmlEntityFields.Id, id, managers);

                if (index < 0)
                {
                    if (!DataProvider.CreateNewObjectId(allObjects, managers, thisObject, XmlEntityFields.Id))
                    {
                        return((T)this);
                    }

                    IsDirty = false;
                    allObjects.Add((T)thisObject.Clone());
                }
                else
                {
                    allObjects.RemoveAt(index);
                    IsDirty = false;
                    allObjects.Insert(index, (T)thisObject.Clone());
                }

                DoCommitChanges(allObjects.ToArray());
#if !DEBUG
            } catch (InvalidOperationException) {
#endif
            } finally {
                FileLock.ReleaseMutex();
            }

            return((T)this);
        }
        protected void DeleteEntity(long entId)
        {
            try {
                FileLock.WaitOne();
                List <T> allObjects = new List <T> (GetAllEntities(false));
                int      index      = DataProvider.FindObjectIndexByField(allObjects, XmlEntityFields.Id, entId);

                if (index < 0)
                {
                    return;
                }

                allObjects.RemoveAt(index);

                DoCommitChanges(allObjects.ToArray());
#if !DEBUG
            } catch (InvalidOperationException) {
#endif
            } finally {
                FileLock.ReleaseMutex();
            }
        }