Exemple #1
0
        public async Task ExecuteAsync(CancellationToken token = default(CancellationToken), Boolean shouldAutomaticallyDisposeAllDisposables = false)
        {
            ContractUtility.Requires <ArgumentOutOfRangeException>(_operationsQueue.IsNotNullOrEmpty(), "Atleast one operation must be there to be executed.");
            ContractUtility.Requires <NotSupportedException>(_operationsQueue.Any(y => y.AsyncOperation.IsNotNull()),
                                                             "If ExecuteAsync method is used,there needs to be atleast one async operation exceuted." +
                                                             "Please use Execute method(instead of ExecuteAsync) if there is not " +
                                                             "a single async operation.");

            while (_operationsQueue.Count > 0)
            {
                OperationData operationData = _operationsQueue.Dequeue();
                if (operationData.Operation.IsNotNull())
                {
                    operationData.Operation();
                }
                else if (operationData.AsyncOperation.IsNotNull())
                {
                    await operationData.AsyncOperation();
                }
            }

            if (_unitOfWorkData != null && _unitOfWorkData.UnitOfWork != null)
            {
                dynamic unitOfWork = _unitOfWorkData.UnitOfWork;
                await unitOfWork.CommitAsync(token, _unitOfWorkData.ShouldAutomaticallyRollBackOnTransactionException, _unitOfWorkData.ShouldThrowOnException);

                if (shouldAutomaticallyDisposeAllDisposables)
                {
                    unitOfWork.Dispose();
                }
            }
            if (shouldAutomaticallyDisposeAllDisposables && _repositoriesList.IsNotNullOrEmpty())
            {
                _repositoriesList.Select(x => x as IDisposable).Where(x => x.IsNotNull()).CleanUp();
            }
        }