示例#1
0
        public void Execute(Boolean shouldAutomaticallyDisposeAllDisposables = false)
        {
            ContractUtility.Requires <ArgumentOutOfRangeException>(_operationsQueue.IsNotNullOrEmpty(), "Atleast one operation must be there to be executed.");

            ContractUtility.Requires <NotSupportedException>(_operationsQueue.All(y => y.AsyncOperation.IsNull()),
                                                             "Async operations are not supported by Execute method.Use ExecuteAsync instead.");
            while (_operationsQueue.Count > 0)
            {
                OperationData operationData = _operationsQueue.Dequeue();
                if (operationData.Operation.IsNotNull())
                {
                    operationData.Operation();
                }
            }
            if (_unitOfWorkData != null && _unitOfWorkData.UnitOfWork != null)
            {
                dynamic unitOfWork = _unitOfWorkData.UnitOfWork;
                unitOfWork.Commit(_unitOfWorkData.ShouldAutomaticallyRollBackOnTransactionException, _unitOfWorkData.ShouldThrowOnException);
                if (shouldAutomaticallyDisposeAllDisposables)
                {
                    unitOfWork.Dispose();
                }
            }
            if (shouldAutomaticallyDisposeAllDisposables && _repositoriesList.IsNotNullOrEmpty())
            {
                _repositoriesList.Select(x => x as IDisposable).Where(x => x.IsNotNull()).CleanUp();
            }
        }
示例#2
0
        private void ProcessOperation(OperationData data)
        {
            // Time and run the operation's delegate
            data.Start = DateTimeOffset.Now;
            if (data.Context != null)
            {
                ExecutionContext.Run(data.Context.CreateCopy(), op => ((OperationData)op).Operation(), data);
            }
            else
            {
                data.Operation();
            }

            data.End = DateTimeOffset.Now;

            // Raise the operation completed event
            OnOperationCompleted(data);

            // Signal to all that depend on this operation of its
            // completion, and potentially launch newly available
            lock (_stateLock)
            {
                if (_dependenciesFromTo.TryGetValue(data.Id, out var toList))
                {
                    foreach (var targetId in toList)
                    {
                        var targetData = _operations[targetId];
                        if (--targetData.NumRemainingDependencies == 0)
                        {
                            QueueOperation(targetData);
                        }
                    }
                }

                _dependenciesFromTo.Remove(data.Id);
                if (--_remainingCount == 0)
                {
                    _doneEvent.Set();
                }
            }
        }
示例#3
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();
            }
        }