Sitecore and Item transactions

As I have been doing a lot of work with bulk data insertion and manipulation within Sitecore, coupled with the belief that I don’t think Sitecore offers you the ability to use transactions, I have created my own stand in, simple transaction class.

It utilizes generics and .Net’s disposable interface http://msdn.microsoft.com/en-us/library/system.idisposable.aspx to achieve an easy to read transaction.

If you look at the following code, the first segment shows you how to use the Transaction.cs, the second segment is Transaction.cs. You can see we wrap the transaction in a using statement, which will call the rollback delegate if an exception is thrown at any point. Or alternatively we can call transaction.dispose() which will roll back the transaction also.

How to use Transaction.cs

    public class UtilityMethods
    {
        private Transaction<Item> transaction;

        public void AddChildItems(Item parentItem, string newChildName, TemplateID childTemplateID, int numberOfChildrenToAdd)
        {
            using(transaction = new Transaction<Item>(Rollback))
            {
                for (int i = 0; i < numberOfChildrenToAdd; i++)
                {
                    Item insertedChild = parentItem.Add(newChildName, childTemplateID);
                    transaction.TransactionableItems.Add(insertedChild);
                }
            }
        }

        public void Rollback()
        {
            using (new SecurityDisabler())
            {
                if (transaction != null && transaction.TransactionableItems.Count > 0)
                {
                    foreach (Item item in transaction.TransactionableItems)
                    {
                        try
                        {
                            item.Delete();
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }
            }
        }
    }

Transaction.cs

    /// <summary>
    /// This class models a transaction
    /// </summary>
    /// <example>
    /// using(Transaction transaction = new Transaction(// Provide delegate to handle rollback here))
    /// {
    ///     // Sitecore insert
    ///     // Sitecore update
    ///     // Sitecore insert
    ///     transaction.Commit();
    /// }
    /// </example>
    public class Transaction<T> : IDisposable
    {
        public delegate void RollBack();

        private readonly RollBack rollBack;
        private bool isCommited;
        private readonly List<T> transactionItems = new List<T>();

        /// <summary>
        /// Public constructor
        /// </summary>
        ///
<param name="rollBack">A method to use to rollback with</param>
        public Transaction(RollBack rollBack)
        {
            this.rollBack = rollBack;
        }

        /// <summary>
        /// Handles the dispose call
        /// </summary>
        public void Dispose()
        {
            // If the transaction is not commited
            // and object is disposing, rollback
            if (!isCommited && rollBack != null)
            {
                rollBack();
            }
        }

        /// <summary>
        /// Sets the commited flag to true,
        /// rollback cannot happen after commit is called
        /// </summary>
        public void Commit()
        {
            isCommited = true;

            transactionItems.Clear();
        }

        /// <summary>
        /// Gets the list of transactionable Items
        /// </summary>
        public List<T> TransactionableItems
        {
            get { return transactionItems; }
        }

        /// <summary>
        /// Returns whether or not the transaction was comitted
        /// </summary>
        public bool WasCommited
        {
            get { return isCommited; }
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s