Esempio n. 1
0
        public virtual void Delete()
        {
            // If it's not in the database in the first place,
            // deleting is a no-op.
            if (IsNew)
            {
                return;
            }

            // Store the ModificationCount
            var modCount = DataModification.Count;

            // Store a pre-deletion copy. This will be used in the modification events so that the Id can be captured by code that isn't in this table.
            var preDelete = this.FieldwiseClone();

            var stopwatch = Stopwatch.StartNew();
            var succeeded = false;

            // Make the database change. If an exception is thrown, FullFlush then rethrow it.
            try
            {
                // Delete from the database.
                NrdoTransactedScope.MaybeBeginTransaction(DataBase);
                using (var scope = new NrdoScope(DataBase))
                {
                    scope.ExecuteSql(DeleteStatement, setPkeyOnCmd);
                }
                isNew     = true;
                succeeded = true;
            }
            catch
            {
                DataModification.RaiseFullFlush();
                throw;
            }
            finally
            {
                stopwatch.Stop();
                Nrdo.DebugLog(() => Nrdo.DebugArgs(stopwatch, "db-delete", typeof(T).FullName, "Delete", null));
                if (!succeeded)
                {
                    deletionCache.Value.HitInfo.updateStats(stats => stats.WithFailure(stopwatch.Elapsed), stats => stats.WithFailure(stopwatch.Elapsed));
                }
                else
                {
                    deletionCache.Value.HitInfo.updateStats(stats => stats.WithModification(stopwatch.Elapsed), stats => stats.WithSingleCacheNonHit(stopwatch.Elapsed, false));
                }
            }

            // Lock again. If the modification count is not equal to what it was, FullFlush. Otherwise raise
            // the relevant event.
            lock (Nrdo.LockObj)
            {
                if (DataModification.Count != modCount)
                {
                    DataModification.RaiseFullFlush();
                }
                else
                {
                    DataModification.RaiseDelete(preDelete);
                }
            }
        }
Esempio n. 2
0
        public virtual void UpdateThis()
        {
            // If we can skip it, skip it
            if (!IsNew && IsUnchanged)
            {
                Nrdo.DebugLog(() => Nrdo.DebugArgs("db-update-skipped", typeof(T).FullName, "Update", null));
                return;
            }

            // Store the ModificationCount
            var modCount = DataModification.Count;

            // Make the database change. If an exception is thrown, FullFlush then rethrow it.
            var  wasNew    = IsNew;
            var  stopwatch = Stopwatch.StartNew();
            bool succeeded = false;

            try
            {
                // Insert or update in the database.
                string cmdText = IsNew ? InsertStatement : UpdateStatement;
                if (cmdText != null)
                {
                    NrdoTransactedScope.MaybeBeginTransaction(DataBase);
                    using (var scope = new NrdoScope(DataBase))
                    {
                        scope.ExecuteSql(cmdText, cmd =>
                        {
                            setDataOnCmd(cmd);
                            setPkeyOnCmd(cmd);
                        });
                        if (IsNew)
                        {
                            getPkeyFromSeq(scope);
                        }
                        isNew     = false;
                        succeeded = true;
                    }
                }
            }
            catch
            {
                DataModification.RaiseFullFlush();
                throw;
            }
            finally
            {
                stopwatch.Stop();
                Nrdo.DebugLog(() => Nrdo.DebugArgs(stopwatch, wasNew ? "db-insert" : "db-update", typeof(T).FullName, "Update", null));

                var cache = wasNew ? insertionCache.Value : updateCache.Value;
                if (!succeeded)
                {
                    cache.HitInfo.updateStats(stats => stats.WithFailure(stopwatch.Elapsed), stats => stats.WithFailure(stopwatch.Elapsed));
                }
                else
                {
                    cache.HitInfo.updateStats(stats => stats.WithModification(stopwatch.Elapsed), stats => stats.WithSingleCacheNonHit(stopwatch.Elapsed, false));
                }
            }

            // Lock again. If the modification count is not equal to what it was, FullFlush. Otherwise raise
            // the relevant event.
            lock (Nrdo.LockObj)
            {
                if (DataModification.Count != modCount)
                {
                    DataModification.RaiseFullFlush();
                }
                else if (wasNew)
                {
                    DataModification.RaiseInsert((T)this);
                }
                else
                {
                    DataModification.RaiseUpdate((T)this);
                }
            }
        }