private void InterceptAsync(IInvocation invocation, TransactionalAttribute attribute)
        {
            var transaction = _provider.BeginTransaction(attribute.IsolationLevel);

            try
            {
                invocation.Proceed();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }

            if (invocation.Method.ReturnType == typeof(Task))
            {
                invocation.ReturnValue = InterceptAsync((Task)invocation.ReturnValue, transaction);
            }
            else //Task<TResult>
            {
                invocation.ReturnValue = typeof(TransactionInterceptor)
                                         .GetMethod(nameof(InterceptWitResultAsync),
                                                    BindingFlags.NonPublic | BindingFlags.Static)
                                         ?.MakeGenericMethod(invocation.Method.ReturnType.GenericTypeArguments[0])
                                         .Invoke(null, new[] { invocation.ReturnValue, transaction });
            }
        }
Exemplo n.º 2
0
 public static void ExecuteInTransaction(this ITransactionProvider transactionProvider, Action action)
 {
     using (ITransaction transaction = transactionProvider.BeginTransaction())
     {
         action();
         transaction.Commit();
     }
 }
Exemplo n.º 3
0
        //public void Create_ThrowExceptionAfterSecondSaveChangesWithScope(Guid key)
        //{
        //    using (TransactionScope scope = new TransactionScope())
        //    {
        //        try
        //        {
        //            _repo1.CreateWithNoTransaction(key);
        //            _repo2.Create_ThrowExceptionAfterSaveChangesWithNoTransaction(key);

        //            scope.Complete();
        //        }
        //        catch (Exception)
        //        {
        //            throw;
        //        }
        //    }
        //}

        public void Create_ThrowExceptionAfterSecondSaveChangesWithScope(Guid key)
        {
            _transactionProvider.BeginTransaction();

            try
            {
                _repo1.CreateWithNoTransaction(key);
                _repo2.Create_ThrowExceptionAfterSaveChangesWithNoTransaction(key);

                _transactionProvider.CommitTransaction();
            }
            catch (Exception)
            {
                _transactionProvider.RollbackTransaction();
                throw;
            }
            finally
            {
                _transactionProvider.DisposeTransaction();
            }
        }
Exemplo n.º 4
0
        public async Task InsertAsync(UserViewModel model)
        {
            if (!_eventPublisher.Publish(new BeforeEntityInsert <UserViewModel>(model)))
            {
                return;
            }

            var entity = _mapper.Map <User>(model);

            entity.Id = Guid.NewGuid();
            if (model.UserRolesSelectedIds != null)
            {
                entity.UserRoles = model.UserRolesSelectedIds.Select(s => new UserRole {
                    Id = Guid.NewGuid(), UserId = entity.Id, RoleId = s
                }).ToArray();
            }
            var salt = "";

            entity.PasswordHash = _passwordService.HashPassword(model.Password, ref salt);
            entity.Salt         = salt;
            using (var transaction = _transactionProvider.BeginTransaction())
            {
                if (!_eventPublisher.Publish(new BeforeEntityInsertTransaction <User>(entity)))
                {
                    return;
                }
                await _userService.InsertAsync(entity);

                if (!_eventPublisher.Publish(new AfterEntityInsertTransaction <User>(entity)))
                {
                    return;
                }
                transaction.Commit();
            }
            if (!_eventPublisher.Publish(new AfterEntityInsert <User>(entity)))
            {
                return;
            }
            _notificationService.Success("Uzytkownik został dodany");
        }
Exemplo n.º 5
0
        public async Task InsertAsync(RoleViewModel model)
        {
            if (!_eventPublisher.Publish(new BeforeEntityInsert <RoleViewModel>(model)))
            {
                return;
            }

            var entity = _mapper.Map <Role>(model);

            entity.Id = Guid.NewGuid();
            if (model.RoleUsersSelectedIds != null)
            {
                entity.RoleUsers = model.RoleUsersSelectedIds.Select(s => new UserRole {
                    Id = Guid.NewGuid(), RoleId = entity.Id, UserId = s
                }).ToArray();
            }

            using (var transaction = _transactionProvider.BeginTransaction())
            {
                if (!_eventPublisher.Publish(new BeforeEntityInsertTransaction <Role>(entity)))
                {
                    return;
                }
                await _roleService.InsertAsync(entity);

                if (!_eventPublisher.Publish(new AfterEntityInsertTransaction <Role>(entity)))
                {
                    return;
                }
                transaction.Commit();
            }
            if (!_eventPublisher.Publish(new AfterEntityInsert <Role>(entity)))
            {
                return;
            }
            _notificationService.Success("Encja została dodana");
        }