public async Task <bool> SaveAsync(IVehicleData vehicle) { var vehicleValue = vehicle.Value; var pocoValue = _dataMapper.Map <VehicleViewObject, VehiclePoco>(vehicleValue); VEHICULO1 vehiculo1 = _dataMapper.Map <VehiclePoco, VEHICULO1>(pocoValue); VEHICULO2 vehiculo2 = _dataMapper.Map <VehiclePoco, VEHICULO2>(pocoValue); vehiculo2.CODIINT = vehiculo1.CODIINT; bool retValue = true; using (IDbConnection connection = _sqlExecutor.OpenNewDbConnection()) { using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { if (connection.IsPresent <VEHICULO1>(vehiculo1)) { retValue = await connection.UpdateAsync(vehiculo1).ConfigureAwait(false); retValue = retValue && await connection.UpdateAsync(vehiculo2).ConfigureAwait(false); } else { retValue = retValue && await connection.InsertAsync(vehiculo1) > 0; retValue = retValue && await connection.InsertAsync(vehiculo2) > 0; } retValue = retValue && (await SavePictures(vehicle, connection).ConfigureAwait(false)); transactionScope.Complete(); return(retValue); } catch (TransactionException ex) { // this is an antipattern for exception handling. string message = "Transaction Scope Exception in Vehicle Insertion. Reason: " + ex.Message; var dataLayer = new DataLayerException(message); throw dataLayer; } catch (System.Exception other) { string message = "Error in a Vehicle Insertion. Reason: " + other.Message; var dataLayer = new DataLayerException(message); throw dataLayer; } } } }
/// <summary> /// This method save the vehicle. Throws a DataLayerExecution exception in case the tr /// </summary> /// <returns>Returns true if we have saved the changes.</returns> public async Task <bool> SaveChanges() { VEHICULO1 vehiculo1 = _vehicleMapper.Map <VehiclePoco, VEHICULO1>(_vehicleValue); VEHICULO2 vehiculo2 = _vehicleMapper.Map <VehiclePoco, VEHICULO2>(_vehicleValue); bool retValue = false; try { // TODO: Reverse. using (IDbConnection connection = _sqlExecutor.OpenNewDbConnection()) { using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // here we shall already have the correct change in the VehiclePoco. It shall already validated. // now we have to add the new connection. try { retValue = await connection.UpdateAsync(vehiculo1); retValue = retValue && await connection.UpdateAsync(vehiculo2); transactionScope.Complete(); } catch (TransactionException ex) { string message = "Transaction Scope Exception in Vehicle Update. Reason: " + ex.Message; DataLayerExecutionException dataLayer = new DataLayerExecutionException(message); throw dataLayer; } catch (System.Exception other) { string message = "Save Exception in Vehicle Update. Reason: " + other.Message; DataLayerExecutionException dataLayer = new DataLayerExecutionException(message, other); throw dataLayer; } } } } catch (System.Exception other2) { string message = "Connection Exception in Vehicle Update. Reason: " + other2.Message; DataLayerExecutionException dataLayer = new DataLayerExecutionException(message, other2); throw dataLayer; } return(retValue); }
/// <summary> /// Save asynchronous data /// </summary> /// <returns></returns> public async Task <bool> Save() { var vehicleDto = Value; _vehicleValue = _vehicleMapper.Map <VehicleDto, VehiclePoco>(vehicleDto); VEHICULO1 vehiculo1 = _vehicleMapper.Map <VehiclePoco, VEHICULO1>(_vehicleValue); VEHICULO2 vehiculo2 = _vehicleMapper.Map <VehiclePoco, VEHICULO2>(_vehicleValue); vehiculo2.CODIINT = vehiculo1.CODIINT; Contract.Requires(vehiculo1.CODIINT != null, "Null PrimaryKey before Saving"); Contract.Requires(vehiculo2.CODIINT != null, "Null PrimaryKey before Saving"); bool retValue = false; using (IDbConnection connection = _sqlExecutor.OpenNewDbConnection()) { using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { int ret = await connection.InsertAsync(vehiculo1); retValue = ret > 0; ret = await connection.InsertAsync(vehiculo2); transactionScope.Complete(); retValue = retValue && (ret > 0); } catch (TransactionException ex) { // this is an antipattern for exception handling. string message = "Transaction Scope Exception in Vehicle Insertion. Reason: " + ex.Message; _logger.Log(LogLevel.Error, message); DataLayerExecutionException dataLayer = new DataLayerExecutionException(message); throw dataLayer; } catch (System.Exception other) { string message = "Error in a Vehicle Insertion. Reason: " + other.Message; _logger.Log(LogLevel.Error, message); DataLayerExecutionException dataLayer = new DataLayerExecutionException(message); throw dataLayer; } } } return(retValue); }
public async Task <bool> DeleteAsync(IVehicleData data) { bool retValue = true; using (IDbConnection connection = _sqlExecutor.OpenNewDbConnection()) { using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { // delete all pictures. var pics = CollectPictures(data.PicturesDtos); if (pics != null) { retValue = await connection.DeleteCollectionAsync(pics).ConfigureAwait(false); } VEHICULO1 vehiculo1 = new VEHICULO1(); vehiculo1.CODIINT = data.Value.CODIINT; VEHICULO2 vehiculo2 = new VEHICULO2(); vehiculo2.CODIINT = data.Value.CODIINT; retValue = await connection.DeleteAsync <VEHICULO1>(vehiculo1).ConfigureAwait(false); if (retValue) { retValue = await connection.DeleteAsync <VEHICULO2>(vehiculo2).ConfigureAwait(false); } transactionScope.Complete(); } catch (System.Exception e) { return(false); } } } return(retValue); }
/// <summary> /// Delete asynchronous data. For now it doesnt do nothing. /// </summary> /// <returns></returns> public async Task <bool> DeleteAsyncData() { bool retValue = true; using (IDbConnection connection = _sqlExecutor.OpenNewDbConnection()) { using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { try { // delete all pictures. PICTURES pic = _pictureResult.FirstOrDefault(); if (pic != null) { retValue = await connection.DeleteAsync <PICTURES>(pic); } VEHICULO1 vehiculo1 = _vehicleMapper.Map <VehiclePoco, VEHICULO1>(_vehicleValue); VEHICULO2 vehiculo2 = _vehicleMapper.Map <VehiclePoco, VEHICULO2>(_vehicleValue); retValue = await connection.DeleteAsync <VEHICULO1>(vehiculo1); if (retValue) { retValue = await connection.DeleteAsync <VEHICULO2>(vehiculo2); } transactionScope.Complete(); } catch (System.Exception e) { _logger.Log(LogLevel.Error, "Delete error in the Vehicle module" + e.Message); return(false); } } } return(retValue); }