Beispiel #1
0
        /// <summary>
        /// Runs commands queue
        /// </summary>
        public void Save()
        {
            if (_actions.IsRunning)
            {
                throw new Exception(".SaveChanges cannot be called within post-save action");
            }
            if (_finallyActions.IsRunning)
            {
                throw new Exception(".SaveChanges cannot be called within finally action");
            }


            Exception thrown = null;

            try
            {
                _serviceManager.OnSave();

                CommandsDispatcher dispatcher = new CommandsDispatcher(_mx, _aux.TraceCollector, _tranManager);
                dispatcher.Dispatch(_pipeline, _actions);

                _serviceManager.OnFinally();
                _finallyActions.Run();
                dispatcher.Dispatch(_pipeline, _actions);
            }
            catch (Exception ex)
            {
                if (_exceptionHandler == null || !_exceptionHandler(ex))
                {
                    throw;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Runs async commands queue
        /// </summary>
        /// <returns></returns>
        public async Task SaveAsync()
        {
            if (_actions.IsRunning)
            {
                throw new Exception(".SaveAsync cannot be called within post-save action");
            }
            if (_finallyActions.IsRunning)
            {
                throw new Exception(".SaveAsync cannot be called within finally action");
            }
            Exception thrown = null;

            try
            {
                await _serviceManager.OnSaveAsync();

                CommandsDispatcher dispatcher = new CommandsDispatcher(_mx, _aux.TraceCollector, _tranManager);
                await dispatcher.DispatchAsync(_pipeline, _actions);

                await _serviceManager.OnFinallyAsync();

                await _finallyActions.RunAsync();

                await dispatcher.DispatchAsync(_pipeline, _actions);
            }
            catch (Exception ex)
            {
                if (_exceptionHandler == null || !_exceptionHandler(ex))
                {
                    throw;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Runs async commands queue
        /// </summary>
        /// <returns></returns>
        public async Task SaveAsync(OuterTransactionMode transaction     = OuterTransactionMode.None,
                                    OuterTransactionIsolationLevel level = OuterTransactionIsolationLevel.Chaos)
        {
            if (_actions.IsRunning)
            {
                throw new Exception(".SaveAsync cannot be called within post-save action");
            }
            if (_finallyActions.IsRunning)
            {
                throw new Exception(".SaveAsync cannot be called within finally action");
            }
            IOuterTransaction tran   = ObtainTransaction(transaction, level);
            Exception         thrown = null;

            try
            {
                await _serviceManager.OnSaveAsync();

                CommandsDispatcher dispatcher = new CommandsDispatcher(_mx, _aux.TraceCollector);
                await dispatcher.DispatchAsync(_pipeline, _actions);

                await _serviceManager.OnFinallyAsync();

                await _finallyActions.RunAsync();

                await dispatcher.DispatchAsync(_pipeline, _actions);

                tran?.Commit();
                //CleanupAfterSave();
            }
            catch (Exception ex)
            {
                _exceptionHandler?.Invoke(ex);
                thrown = ex;
            }
            finally
            {
                try
                {
                    tran?.Dispose();
                }
                catch (Exception)
                {
                    if (thrown == null)
                    {
                        throw;
                    }
                }
                if (thrown != null)
                {
                    throw thrown;
                }
            }
        }
        /// <summary>
        /// Runs async commands queue
        /// </summary>
        /// <returns></returns>
        public async Task SaveAsync(CancellationToken token = default)
        {
            if (_actions.IsRunning)
            {
                throw new Exception(".SaveAsync cannot be called within post-save action");
            }
            if (_finallyActions.IsRunning)
            {
                throw new Exception(".SaveAsync cannot be called within finally action");
            }
            Exception          thrown     = null;
            CommandsDispatcher dispatcher = new CommandsDispatcher(_mx, _aux.TraceCollector, _tranManager);

            try
            {
                await _serviceManager.OnSaveAsync(token);

                await dispatcher.DispatchAsync(_pipeline, _actions, token);
            }
            catch (Exception ex)
            {
                thrown = ex;
            }
            finally
            {
                Exception thrown2 = null;
                try
                {
                    if (_tc != null)
                    {
                        _tc.Command(new Comment()
                        {
                            Annotation = "<<< Finally block >>>",
                            Channel    = typeof(NoChannel),
                            IsExecuted = true
                        });
                    }

                    await _serviceManager.OnFinallyAsync(thrown, token);

                    await _finallyActions.RunAsync(token);

                    if (_pipeline.HasEffects)
                    {
                        await dispatcher.DispatchAsync(_pipeline, null, token);
                    }
                }
                catch (Exception finException)
                {
                    thrown2 = finException;
                }
                finally
                {
                    _tc?.Command(new Comment()
                    {
                        Annotation = "<<< End of Finally block >>>",
                        Channel    = typeof(NoChannel),
                        IsExecuted = true
                    });
                }

                if (thrown2 != null)
                {
                    if (thrown == null)
                    {
                        thrown = thrown2;
                    }
                    else
                    {
                        thrown = new AggregateException(thrown, thrown2);
                    }
                }

                if (thrown != null)
                {
                    if (_exceptionHandler == null || !_exceptionHandler(thrown))
                    {
                        throw thrown;
                    }
                }
            }
        }
        /// <summary>
        /// Runs commands queue
        /// </summary>
        public void Save()
        {
            if (_actions.IsRunning)
            {
                throw new Exception(".SaveChanges cannot be called within post-save action");
            }
            if (_finallyActions.IsRunning)
            {
                throw new Exception(".SaveChanges cannot be called within finally action");
            }

            Exception          thrown     = null;
            CommandsDispatcher dispatcher = new CommandsDispatcher(_mx, _aux.TraceCollector, _tranManager);

            try
            {
                _serviceManager.OnSave();
                dispatcher.Dispatch(_pipeline, _actions);
            }
            catch (Exception ex)
            {
                thrown = ex;
            }
            finally
            {
                Exception thrown2 = null;
                try
                {
                    if (_tc != null)
                    {
                        _tc.Command(new Comment()
                        {
                            Annotation = "<<< Finally block >>>",
                            Channel    = typeof(NoChannel),
                            IsExecuted = true
                        });
                    }

                    _serviceManager.OnFinally(thrown);
                    _finallyActions.Run();
                    if (_pipeline.HasEffects)
                    {
                        dispatcher.Dispatch(_pipeline, null);
                    }
                }
                catch (Exception finException)
                {
                    thrown2 = finException;
                }
                finally
                {
                    _tc?.Command(new Comment()
                    {
                        Annotation = "<<< End of Finally block >>>",
                        Channel    = typeof(NoChannel),
                        IsExecuted = true
                    });
                }

                if (thrown2 != null)
                {
                    if (thrown == null)
                    {
                        thrown = thrown2;
                    }
                    else
                    {
                        thrown = new AggregateException(thrown, thrown2);
                    }
                }

                if (thrown != null)
                {
                    if (_exceptionHandler == null || !_exceptionHandler(thrown))
                    {
                        throw thrown;
                    }
                }
            }
        }