Пример #1
0
 public static AspectF Retry(this AspectF aspects, int retryDuration,
                             int retryCount, Action <Exception> errorHandler, Action <IEnumerable <Exception> > retryFailed)
 {
     return(aspects.Combine(work =>
     {
         List <Exception> errors = null;
         do
         {
             try
             {
                 work();
                 return;
             }
             catch (Exception x)
             {
                 if (null == errors)
                 {
                     errors = new List <Exception>();
                 }
                 errors.Add(x);
                 if (errorHandler != null)
                 {
                     errorHandler(x);
                 }
                 System.Threading.Thread.Sleep(retryDuration);
             }
         } while (retryCount-- > 0);
         if (retryFailed != null)
         {
             retryFailed(errors);
         }
     }));
 }
Пример #2
0
 public static AspectF Delay(this AspectF aspect, int milliseconds)
 {
     return(aspect.Combine(work =>
     {
         System.Threading.Thread.Sleep(milliseconds);
         work();
     }));
 }
Пример #3
0
 public static TReturnType Use <TReturnType>(this AspectF aspect, TReturnType item, Action <TReturnType> action)
 {
     return(aspect.Return(() =>
     {
         action(item);
         return item;
     }));
 }
Пример #4
0
 public static AspectF While(this AspectF aspect, Func <bool> test)
 {
     return(aspect.Combine(work =>
     {
         while (test())
         {
             work();
         }
     }));
 }
Пример #5
0
 public static AspectF Transaction(this AspectF aspect)
 {
     return(aspect.Combine(work =>
     {
         using (var scope = new TransactionScope(TransactionScopeOption.Required))
         {
             work();
             scope.Complete();
         }
     }));
 }
Пример #6
0
 public static AspectF RunAsync(this AspectF aspect, Action completeCallback = null)
 {
     return(aspect.Combine(work => work.BeginInvoke(asyncresult =>
     {
         work.EndInvoke(asyncresult);
         if (completeCallback != null)
         {
             completeCallback();
         }
     }, null)));
 }
Пример #7
0
        public static AspectF Until(this AspectF aspect, Func <bool> test)
        {
            return(aspect.Combine(work =>
            {
                while (!test())
                {
                }

                work();
            }));
        }
Пример #8
0
        public static AspectF HowLong(this AspectF aspect, Action <TimeSpan> action)
        {
            return(aspect.Combine(work =>
            {
                DateTime start = DateTime.Now;

                work();

                DateTime end = DateTime.Now.ToUniversalTime();
                action(end - start);
            }));
        }
Пример #9
0
        public static AspectF WhenTrue(this AspectF aspect, params Func <bool>[] conditions)
        {
            return(aspect.Combine(work =>
            {
                if (conditions.Any(condition => !condition()))
                {
                    return;
                }

                work();
            }));
        }
Пример #10
0
 public static AspectF Expected <TException>(this AspectF aspect) where TException : Exception
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch (TException x)
         {
             Debug.WriteLine(x.ToString());
         }
     }));
 }
Пример #11
0
        public static AspectF MustBeNonNull(this AspectF aspect, params object[] args)
        {
            return(aspect.Combine(work =>
            {
                for (var i = 0; i < args.Length; i++)
                {
                    var arg = args[i];
                    if (arg == null)
                    {
                        throw new ArgumentException(
                            string.Format("Parameter at index {0} is null", i));
                    }
                }

                work();
            }));
        }
Пример #12
0
        public static AspectF MustBeNonDefault <T>(this AspectF aspect, params T[] args)
            where T : IComparable
        {
            return(aspect.Combine(work =>
            {
                for (var i = 0; i < args.Length; i++)
                {
                    T arg = args[i];
                    if (arg == null || arg.Equals(default(T)))
                    {
                        throw new ArgumentException(
                            string.Format("Parameter at index {0} is null", i));
                    }
                }

                work();
            }));
        }
Пример #13
0
 public static AspectF DelayAfter(this AspectF aspect, int timespan, Action <int> __记录耗时 = null)
 {
     return(aspect.Combine(work =>
     {
         var __秒表 = new Stopwatch();
         __秒表.Start();
         work();
         __秒表.Stop();
         var __休眠 = timespan - (int)__秒表.Elapsed.TotalMilliseconds;
         if (__记录耗时 != null)
         {
             __记录耗时((int)__秒表.Elapsed.TotalMilliseconds);
         }
         if (__休眠 > 0)
         {
             Thread.Sleep(__休眠);
         }
     }));
 }