public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (!_transaction.IsActive)
            {
                return;
            }

            if (filterContext.Exception == null)
            {
                try
                {
                    filterContext.Controller.TempData["StatusAlert"] = "Karate chop!!";

                    _transaction.Commit();
                }
                catch (StaleObjectStateException)
                {
                    // Handle stale update here
                    filterContext.Controller.TempData["StatusAlert"] = "The changes could not be applied since this record has been updated by another user.";

                    // Redirect back to self. This is required in case the
                    // action is attempting to redirect to another action.
                    filterContext.Result = new RedirectResult(filterContext.HttpContext.Request.RawUrl);

                    // Allow rollback
                    _transaction.Dispose();
                }
            }
            else
            {
                _transaction.Dispose();
            }
        }
Ejemplo n.º 2
0
        protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation,
                                                                         IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            MethodInfo           methodInfo           = invocation.MethodInvocationTarget;
            TransactionAttribute transactionAttribute = TransactionUtils.FindTransactionAttribute(methodInfo);
            bool bindingRequired = transactionAttribute != null;

            if (bindingRequired)
            {
                using (SessionWrapper sessionWrapper = sessionService.GetCurrentSession(transactionAttribute.ReadOnly))
                {
                    using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(
                               transactionAttribute.ReadOnly, transactionAttribute.IsolationLevel))
                    {
                        TResult retObject = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                        await transactionWrapper.Commit().ConfigureAwait(false);

                        return(retObject);
                    }
                }
            }

            {
                TResult retObject = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                return(retObject);
            }
        }
Ejemplo n.º 3
0
 public void Commit()
 {
     try
     {
         transaction.Commit();
     }
     catch
     {
         Rollback();
         throw;
     }
 }
Ejemplo n.º 4
0
        public virtual async Task Delete(long id)
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper())
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction())
                {
                    await entityRepository.Delete(id);

                    await transactionWrapper.Commit();
                }
            }
        }
Ejemplo n.º 5
0
        public virtual async Task <TEntity> Update(TEntity entity)
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper())
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction())
                {
                    TEntity updatedEntity = await entityRepository.Update(entity);

                    await transactionWrapper.Commit();

                    return(updatedEntity);
                }
            }
        }
Ejemplo n.º 6
0
        public virtual async Task <TEntity> Get(long id, bool updateLater = false)
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper(true))
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(true))
                {
                    TEntity entity = await entityRepository.Get(id, updateLater);

                    await transactionWrapper.Commit();

                    return(entity);
                }
            }
        }
Ejemplo n.º 7
0
        public virtual async Task <IList <TEntity> > LoadAll()
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper(true))
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(true))
                {
                    IQueryable <TEntity> entityQuery = entityRepository.GetQueryable(sessionWrapper);
                    List <TEntity>       entities    = await entityQuery.ToListAsync();

                    await transactionWrapper.Commit();

                    return(entities);
                }
            }
        }
Ejemplo n.º 8
0
 public void CommitTransaction()
 {
     _httpContextAccessor.HttpContext.Response.Headers.Add(TransactionIsolationLevel, _transaction.IsolationLevel.ToString());
     _httpContextAccessor.HttpContext.Response.Headers.Add(TransactionState, "committed");
     _transaction?.Commit();
 }