コード例 #1
0
        /// <summary>
        /// Makes an operation atomic by allowing all data updates it contains easily to share the same transaction
        /// </summary>
        /// <param name="operations">An operation which should use a single transaction</param>
        public void MakeAtomic(TransactionedOperation operation)
        {
            #region Parameter checks
            if (null == operation)
            {
                throw new ArgumentNullException("operation");
            }
            #endregion

            MakeAtomic(new TransactionedOperation[] { operation });
        }
コード例 #2
0
        /// <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
        }
コード例 #3
0
 public TransactionArgs(IDbConnection connection, IDbTransaction transaction, TransactionedOperation operation)
 {
     this.Connection  = connection;
     this.Transaction = transaction;
     this.Operation   = operation;
 }
        /// <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 an operation atomic by allowing all data updates it contains easily to share the same transaction
        /// </summary>
        /// <param name="operations">An operation which should use a single transaction</param>
        public void MakeAtomic(TransactionedOperation operation)
        {
            #region Parameter checks
            if (null == operation)
            {
                throw new ArgumentNullException("operation");
            }
            #endregion

            MakeAtomic(new TransactionedOperation[] {operation});
        }
 public TransactionArgs(IDbConnection connection, IDbTransaction transaction, TransactionedOperation operation)
 {
     this.Connection = connection;
     this.Transaction = transaction;
     this.Operation = operation;
 }