public virtual IQueryable <TEntity> Include(Expression <Func <TEntity, object> > subSelector) { CheckForObjectAlreadyDisposedOrNot(typeof(QueryableRepository <TEntity>).FullName); ContractUtility.Requires <ArgumentNullException>(subSelector.IsNotNull(), "subSelector instance cannot be null"); //TODO - proper exception handling compensating handler needs to be here return(ExceptionHandlingUtility.HandleExceptionWithNullCheck(() => _queryable.Include(subSelector), _exceptionHandler)); }
private void HandleRunQuery <TNextActionType>(Func <IQueryableRepository <TEntity>, TNextActionType> queryableRepositoryOperation, Action <TNextActionType> operationToExecuteBeforeNextOperation) { CheckForObjectAlreadyDisposedOrNot(typeof(QueryableRepository <TEntity>).FullName); ContractUtility.Requires <ArgumentNullException>(queryableRepositoryOperation.IsNotNull(), "queryableRepositoryOperation instance cannot be null"); Action operation = () => { TNextActionType queryReturnValue = _unitOfWork.IsNull() ? ExceptionHandlingUtility.HandleExceptionWithNullCheck( () => queryableRepositoryOperation(this), _exceptionHandler) : queryableRepositoryOperation(this); //TODO - proper exception handling compensating handler needs to be here if (operationToExecuteBeforeNextOperation.IsNotNull()) { operationToExecuteBeforeNextOperation(queryReturnValue); } }; if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperation(operation); } else { operation(); } }
public override void Delete(TEntity item, Action operationToExecuteBeforeNextOperation = null) { ContractUtility.Requires <ArgumentNullException>(item.IsNotNull(), "item instance cannot be null"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperation(() => ActualDelete(item, operationToExecuteBeforeNextOperation)); } else { //TODO - proper exception handling compensating handler needs to be here ExceptionHandlingUtility.HandleExceptionWithNullCheck(() => ActualDelete(item, operationToExecuteBeforeNextOperation), _exceptionHandler); } }
public override async Task DeleteAsync(TEntity item, CancellationToken token = default, Action operationToExecuteBeforeNextOperation = null) { ContractUtility.Requires <ArgumentNullException>(item.IsNotNull(), "item instance cannot be null"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperationAsync(async x => await ActualDeleteAsync(item, token == default ? x : token, operationToExecuteBeforeNextOperation).ConfigureAwait(false)); } else { //TODO - proper exception handling compensating handler needs to be here await ExceptionHandlingUtility.HandleExceptionWithNullCheck(async x => await ActualDeleteAsync(item, x, operationToExecuteBeforeNextOperation).ConfigureAwait(false), token, _exceptionHandler, null); } }
public override async Task UpdateAsync(IEnumerable <TEntity> items, CancellationToken token = default, Action operationToExecuteBeforeNextOperation = null) { ContractUtility.Requires <ArgumentNullException>(items.IsNotNull(), "items instance cannot be null"); ContractUtility.Requires <ArgumentOutOfRangeException>(items.IsNotEmpty(), "items count should be greater than 0"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperationAsync(async x => await ActualUpdateAsync(items, token == default ? x : token, operationToExecuteBeforeNextOperation).ConfigureAwait(false)); } else { //TODO - proper exception handling compensating handler needs to be here await ExceptionHandlingUtility.HandleExceptionWithNullCheck(async x => await ActualUpdateAsync(items, x, operationToExecuteBeforeNextOperation).ConfigureAwait(false), token, _exceptionHandler, null); } }
public override void Delete(IEnumerable <TEntity> items, Action operationToExecuteBeforeNextOperation = null) { ContractUtility.Requires <ArgumentNullException>(items.IsNotNull(), "items instance cannot be null"); ContractUtility.Requires <ArgumentOutOfRangeException>(items.IsNotEmpty(), "items count should be greater than 0"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperation(() => ActualDelete(items, operationToExecuteBeforeNextOperation)); } else { //TODO - proper exception handling compensating handler needs to be here ExceptionHandlingUtility.HandleExceptionWithNullCheck(() => ActualDelete(items, operationToExecuteBeforeNextOperation), _exceptionHandler); } }
public override void Update(TEntity item, Action operationToExecuteBeforeNextOperation = null) { CheckForObjectAlreadyDisposedOrNot(typeof(CommandRepository <TEntity>).FullName); ContractUtility.Requires <ArgumentNullException>(item.IsNotNull(), "item instance cannot be null"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperation(() => ActualUpdate(item, operationToExecuteBeforeNextOperation)); } else { //TODO - proper exception handling compensating handler needs to be here ExceptionHandlingUtility.HandleExceptionWithNullCheck(() => ActualUpdate(item, operationToExecuteBeforeNextOperation), _exceptionHandler); } }
public override async Task DeleteAsync(TEntity item, CancellationToken token = default(CancellationToken), Action operationToExecuteBeforeNextOperation = null) { CheckForObjectAlreadyDisposedOrNot(typeof(CommandRepository <TEntity>).FullName); ContractUtility.Requires <ArgumentNullException>(item.IsNotNull(), "item instance cannot be null"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperationAsync(async x => await ActualDeleteAsync(item, token == default(CancellationToken) ? x : token, operationToExecuteBeforeNextOperation)); } else { //TODO - proper exception handling compensating handler needs to be here await ExceptionHandlingUtility.HandleExceptionWithNullCheck(async x => await ActualDeleteAsync(item, x, operationToExecuteBeforeNextOperation), token, _exceptionHandler, null); } }
public override async Task BulkInsertAsync(IEnumerable <TEntity> items, CancellationToken token = default(CancellationToken), Action operationToExecuteBeforeNextOperation = null) { CheckForObjectAlreadyDisposedOrNot(typeof(CommandRepository <TEntity>).FullName); ContractUtility.Requires <ArgumentNullException>(items.IsNotNull(), "items instance cannot be null"); ContractUtility.Requires <ArgumentOutOfRangeException>(items.IsNotEmpty(), "items count should be greater than 0"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperationAsync(async x => await ActualBulkInsertAsync(items, token == default(CancellationToken) ? x : token, operationToExecuteBeforeNextOperation)); } else { //TODO - proper exception handling compensating handler needs to be here await ExceptionHandlingUtility.HandleExceptionWithNullCheck(async x => await ActualBulkInsertAsync(items, x, operationToExecuteBeforeNextOperation), token, _exceptionHandler, null); } }
public override void BulkUpdate(IEnumerable <TEntity> items, Action operationToExecuteBeforeNextOperation = null) { CheckForObjectAlreadyDisposedOrNot(typeof(CommandRepository <TEntity>).FullName); ContractUtility.Requires <ArgumentNullException>(items.IsNotNull(), "items instance cannot be null"); ContractUtility.Requires <ArgumentOutOfRangeException>(items.IsNotEmpty(), "items count should be greater than 0"); if (_unitOfWork.IsNotNull()) { _unitOfWork.AddOperation(() => ActualBulkUpdate(items, operationToExecuteBeforeNextOperation)); } else { //TODO - proper exception handling compensating handler needs to be here ExceptionHandlingUtility.HandleExceptionWithNullCheck(() => ActualBulkUpdate(items, operationToExecuteBeforeNextOperation), _exceptionHandler); } }
/// <summary> /// This method can run synchronous as well as asynchronous operations within a transaction /// in the order in which the operations are written in the consumer class.Here synchronous /// as well as asynchronous operations are hadnled since "Task" also inherently handles both /// synchronous and asynchronous scenarios. /// /// </summary> /// <param name="shouldCommitSynchronousOperationsFirst"></param> /// <param name="shouldAutomaticallyRollBackOnTransactionException"></param> /// <param name="shouldThrowOnException"></param> /// <returns></returns> public async Task CommitAsync(CancellationToken token = default(CancellationToken), bool shouldAutomaticallyRollBackOnTransactionException = true, bool shouldThrowOnException = true) { CheckForObjectAlreadyDisposedOrNot(typeof(UnitOfWork).FullName); ContractUtility.Requires <ArgumentOutOfRangeException>(_operationsQueue.Count > 0, "Atleast one operation must be there to be executed."); ContractUtility.Requires <NotSupportedException>(_operationsQueue.Any(x => x.AsyncOperation.IsNotNull()), "If CommitAsync method is used,there needs to be atleast one async operation exceuted." + "Please use Commit method(instead of CommitAsync) if there is not " + "a single async operation."); await ExceptionHandlingUtility.HandleExceptionWithNullCheck(async x => { _scope = TransactionUtility.GetTransactionScope(_isoLevel, _scopeOption, true); try { while (_operationsQueue.Count > 0) { #if TEST ThrowExceptionForRollbackCheck(); #endif OperationData operationData = _operationsQueue.Dequeue(); if (operationData.Operation.IsNotNull()) { operationData.Operation(); } else if (operationData.AsyncOperation.IsNotNull()) { await operationData.AsyncOperation(x); } } CompleteScope(() => { _scope.Complete(); // this just completes the transaction.Not yet committed here. _scope.Dispose(); // After everthing runs successfully within the transaction // and after completion, this should be called to actually commit the data // within the transaction scope. }, shouldAutomaticallyRollBackOnTransactionException, shouldThrowOnException); } catch (Exception ex) { //Although ex is not exactly a commit exception but still passing it to reuse Rollback method.Using the Rollback //method to reuse exception handling and dispose the transaction scope object(else it can cause issues for the //future transactions). Rollback(ex); } }, token, _exceptionHandler, null);//TODO - proper exception handling compensating handler needs to be here }
/// <summary> /// Comits all the data within this unit of work instance in an atomic way i.e. all or none get transacted. /// Order of operations of different instances of same type or different types needs to be handled at /// the Business or Service Layer. /// </summary> /// <param name="shouldAutomaticallyRollBackOnTransactionException">when set to true(default value) /// the RollBack method need not be called from the consumer class</param> public void Commit(bool shouldAutomaticallyRollBackOnTransactionException = true, bool shouldThrowOnException = true) { ContractUtility.Requires <ArgumentOutOfRangeException>(_operationsQueue.IsNotNullOrEmpty(), "Atleast one operation must be there to be executed."); ContractUtility.Requires <NotSupportedException>(_operationsQueue.All(x => x.AsyncOperation.IsNull()), "Async operations are not supported by Commit method.Use CommitAsync instead."); ExceptionHandlingUtility.HandleExceptionWithNullCheck(() => { _scope = TransactionUtility.GetTransactionScope(_isoLevel, _scopeOption); try { while (_operationsQueue.Count > 0) { #if TEST ThrowExceptionForRollbackCheck(); #endif OperationData operationData = _operationsQueue.Dequeue(); if (operationData.Operation.IsNotNull()) { operationData.Operation(); } } CompleteScope(() => { _scope.Complete(); // this just completes the transaction.Not yet committed here. _scope.Dispose(); // After everthing runs successfully within the transaction // and after completion, this should be called to actually commit the data // within the transaction scope. }, shouldAutomaticallyRollBackOnTransactionException, shouldThrowOnException); } catch (Exception ex) { //Although ex is not exactly a commit exception but still passing it to reuse Rollback method.Using the Rollback //method to reuse exception handling and dispose the transaction scope object(else it can cause issues for the //future transactions). Rollback(ex); } }, _exceptionHandler);//TODO - proper exception handling compensating handler needs to be here }
/// <summary> /// For Exception handling scenario, refer - /// http://stackoverflow.com/questions/9780908/using-unity-interception-to-solve-exception-handling-as-a-crosscutting-concern /// </summary> /// <param name="input"></param> /// <param name="getNext"></param> /// <returns></returns> public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { return(ExceptionHandlingUtility.HandleExceptionWithNullCheck(() => { ExecuteBeforeMethodInvocation(input); IMethodReturn methodReturn = getNext().Invoke(input, getNext); Exception methodReturnException = methodReturn.Exception; if (methodReturnException.IsNotNull()) { if (_exceptionHandler.IsNull()) { _logger.LogException(methodReturnException); } throw methodReturnException; } else { ExecuteAfterMethodInvocation(input, methodReturn); } return methodReturn; }, _exceptionHandler)); }