/// <summary> /// Makes one or more potentially disparate operations atomic by allowing them easily to share the same transaction /// </summary> /// <param name="operations">An array of operations which should share the same transaction</param> public void MakeAtomic(TransactionedOperation[] operations) { #region Parameter checks if (null == operations) { throw new ArgumentNullException("operations"); } for (int i = 0; i < operations.Length; ++i) { TransactionedOperation operation = operations[i]; if (null == operation) { throw new ArgumentNullException("operation"); } if (null == operation.Execute) { throw new ArgumentNullException(String.Format("operation.Execute at index {0}", i)); } } #endregion #region Main functionality this.Connection.Open(); try { using (IDbTransaction transaction = this.Connection.BeginTransaction()) { try { /// Prepare, then call the operation /// Any exceptions will cause the transaction to be aborted /// /// All relevent objects have been checked for nullness prior to this /// foreach (TransactionedOperation operation in operations) { TransactionArgs args = new TransactionArgs(Connection, transaction, operation); operation.Execute(this, args); // TODO: Log the operation here if required. } transaction.Commit(); } catch (Exception exception) { transaction.Rollback(); // TODO: Log the error here throw; } } } finally { // Strictly speaking, there is no need for this close here because IDbConnection // implements IDisposable - the connection will be closed when the object is Garbage-Collected this.Connection.Close(); } #endregion }
/// <summary> /// Makes one or more potentially disparate operations atomic by allowing them easily to share the same transaction /// </summary> /// <param name="operations">An array of operations which should share the same transaction</param> public void MakeAtomic(TransactionedOperation[] operations) { #region Parameter checks if (null == operations) { throw new ArgumentNullException("operations"); } for (int i=0; i < operations.Length; ++i) { TransactionedOperation operation = operations[i]; if (null == operation) { throw new ArgumentNullException("operation"); } if (null == operation.Execute) { throw new ArgumentNullException(String.Format("operation.Execute at index {0}", i)); } } #endregion #region Main functionality this.Connection.Open(); try { using (IDbTransaction transaction = this.Connection.BeginTransaction()) { try { /// Prepare, then call the operation /// Any exceptions will cause the transaction to be aborted /// /// All relevent objects have been checked for nullness prior to this /// foreach (TransactionedOperation operation in operations) { TransactionArgs args = new TransactionArgs(Connection, transaction, operation); operation.Execute(this, args); // TODO: Log the operation here if required. } transaction.Commit(); } catch (Exception exception) { transaction.Rollback(); // TODO: Log the error here throw; } } } finally { // Strictly speaking, there is no need for this close here because IDbConnection // implements IDisposable - the connection will be closed when the object is Garbage-Collected this.Connection.Close(); } #endregion }