예제 #1
0
        /// <summary>
        /// Executes an action for each element in the collection transactionally and showing the progress in the Console
        /// </summary>
        public static void ProgressForeach <T>(this IEnumerable <T> collection,
                                               Func <T, string>?elementID      = null,
                                               Action <T>?action               = null,
                                               bool transactional              = true,
                                               bool showProgress               = true,
                                               LogWriter?writer                = null,
                                               ParallelOptions?parallelOptions = null,
                                               Type?disableIdentityFor         = null)
        {
            if (action == null)
            {
                throw new InvalidOperationException("no action specified");
            }

            if (elementID == null)
            {
                elementID = e => e !.ToString();
            }
            if (writer == null)
            {
                writer = GetConsoleWriter();
            }


            if (disableIdentityFor == null)
            {
                collection.ProgressForeachInternal(elementID, writer, parallelOptions, transactional, showProgress, action);
            }
            else
            {
                if (!transactional)
                {
                    throw new InvalidOperationException("disableIdentity has to be transactional");
                }

                Table table = Schema.Current.Table(disableIdentityFor);

                if (!table.IdentityBehaviour)
                {
                    throw new InvalidOperationException("Identity is false already");
                }

                table.IdentityBehaviour = false;
                try
                {
                    collection.ProgressForeachInternal(elementID, writer, parallelOptions, transactional, showProgress, action: item =>
                    {
                        using (Transaction tr = Transaction.ForceNew())
                        {
                            using (table.PrimaryKey.Default != null
                                ? null
                                : Administrator.DisableIdentity(table.Name))
                                action !(item);
                            tr.Commit();
                        }
                    });
                }
                finally
                {
                    table.IdentityBehaviour = true;
                }
            }
        }