Exemple #1
0
 public static AspectF RunAsync(this AspectF aspect)
 {
     return(aspect.Combine((work) => work.BeginInvoke(asyncresult =>
     {
         work.EndInvoke(asyncresult);
     }, null)));
 }
Exemple #2
0
 public static AspectF RunAsync(this AspectF aspect, Action completeCallback)
 {
     return(aspect.Combine((work) => work.BeginInvoke(asyncresult =>
     {
         work.EndInvoke(asyncresult); completeCallback();
     }, null)));
 }
Exemple #3
0
        public static AspectF CacheRetry <TReturnType>(this AspectF aspect,
                                                       ICache cacheResolver,
                                                       ILogger logger,
                                                       string key)
        {
            return(aspect.Combine((work) =>
            {
                try
                {
                    Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
                }
                catch (Exception x)
                {
                    logger.WriteLog(LogLevel.Error, x);
                    System.Threading.Thread.Sleep(1000);

                    //Retry
                    try
                    {
                        Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
                    }
                    catch (Exception ex)
                    {
                        logger.WriteLog(LogLevel.Error, ex);
                        throw ex;
                    }
                }
            }));
        }
Exemple #4
0
 public static AspectF Delay(this AspectF aspect, int milliseconds)
 {
     return(aspect.Combine((work) =>
     {
         System.Threading.Thread.Sleep(milliseconds);
         work();
     }));
 }
Exemple #5
0
 public static AspectF Cache <TReturnType>(this AspectF aspect,
                                           ICache cacheResolver, string key)
 {
     return(aspect.Combine((work) =>
     {
         Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
     }));
 }
Exemple #6
0
        public static AspectF Log(this AspectF aspect, ILogger logger,
                                  string logMessage, params object[] arg)
        {
            return(aspect.Combine((work) =>
            {
                logger.WriteLog(LogLevel.Info, string.Format("■" + logMessage, arg));

                work();
            }));
        }
Exemple #7
0
        public static AspectF Log(this AspectF aspect, ILogger logger, string[] categories,
                                  string logMessage, params object[] arg)
        {
            return(aspect.Combine((work) =>
            {
                logger.WriteLog(LogLevel.Info, categories, logMessage);

                work();
            }));
        }
Exemple #8
0
 public static AspectF While(this AspectF aspect, Func <bool> test)
 {
     return(aspect.Combine((work) =>
     {
         while (test())
         {
             work();
         }
     }));
 }
Exemple #9
0
        public static AspectF Log(this AspectF aspect, ILogger logger,
                                  string beforeMessage, string afterMessage)
        {
            return(aspect.Combine((work) =>
            {
                logger.WriteLog(LogLevel.Info, "■" + beforeMessage + "▼▼▼▼▼ ThreadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

                work();

                logger.WriteLog(LogLevel.Info, "■" + afterMessage + "▲▲▲▲▲ ThreadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            }));
        }
Exemple #10
0
        public static AspectF Log(this AspectF aspect, ILogger logger, string[] categories,
                                  string beforeMessage, string afterMessage)
        {
            return(aspect.Combine((work) =>
            {
                logger.WriteLog(LogLevel.Info, categories, "■" + beforeMessage);

                work();

                logger.WriteLog(LogLevel.Info, categories, "■" + afterMessage);
            }));
        }
Exemple #11
0
        public static AspectF Until(this AspectF aspect, Func <bool> test)
        {
            return(aspect.Combine((work) =>
            {
                while (!test())
                {
                    ;
                }

                work();
            }));
        }
Exemple #12
0
        public static AspectF CacheList <TItemType, TListType>(this AspectF aspect,
                                                               ICache cacheResolver, string listCacheKey, Func <TItemType, string> getItemKey)
            where TListType : IList <TItemType>, new()
        {
            return(aspect.Combine((work) =>
            {
                Func <TListType> workDelegate = aspect.WorkDelegate as Func <TListType>;

                // Replace the actual work delegate with a new delegate so that
                // when the actual work delegate returns a collection, each item
                // in the collection is stored in cache individually.
                Func <TListType> newWorkDelegate = () =>
                {
                    TListType collection = workDelegate();
                    foreach (TItemType item in collection)
                    {
                        string key = getItemKey(item);
                        cacheResolver.Set(key, item);
                    }
                    return collection;
                };
                aspect.WorkDelegate = newWorkDelegate;

                // Get the collection from cache or real source. If collection is returned
                // from cache, resolve each item in the collection from cache
                Cache <TListType>(aspect, cacheResolver, listCacheKey, work,
                                  cached =>
                {
                    // Get each item from cache. If any of the item is not in cache
                    // then discard the whole collection from cache and reload the
                    // collection from source.
                    TListType itemList = new TListType();
                    foreach (TItemType item in cached)
                    {
                        object cachedItem = cacheResolver.Get(getItemKey(item));
                        if (null != cachedItem)
                        {
                            itemList.Add((TItemType)cachedItem);
                        }
                        else
                        {
                            // One of the item is missing from cache. So, discard the
                            // cached list.
                            return default(TListType);
                        }
                    }

                    return itemList;
                });
            }));
        }
Exemple #13
0
 public static AspectF TrapLog(this AspectF aspect, ILogger logger)
 {
     return(aspect.Combine((work) =>
     {
         try
         {
             work();
         }
         catch (Exception x)
         {
             logger.WriteLog(LogLevel.Error, x);
         }
     }));
 }
Exemple #14
0
        public static AspectF WhenTrue(this AspectF aspect, params Func <bool>[] conditions)
        {
            return(aspect.Combine((work) =>
            {
                foreach (Func <bool> condition in conditions)
                {
                    if (!condition())
                    {
                        return;
                    }
                }

                work();
            }));
        }
Exemple #15
0
        public static AspectF MustBeNonNull(this AspectF aspect, params object[] args)
        {
            return(aspect.Combine((work) =>
            {
                for (int i = 0; i < args.Length; i++)
                {
                    object arg = args[i];
                    if (arg == null)
                    {
                        throw new ArgumentException(
                            string.Format("Parameter at index {0} is null", i));
                    }
                }

                work();
            }));
        }
Exemple #16
0
        public static AspectF HowLong(this AspectF aspect, ILogger logger,
                                      string invokerMessage = "", string startMessage = " 开始记时", string endMessage = " 结束记时 ★消耗时间为:{0}ms,{1}s,{2}M,{3}H★")
        {
            return(aspect.Combine((work) =>
            {
                DateTime start = DateTime.Now.ToUniversalTime();
                logger.WriteLog(LogLevel.Info, "■" + invokerMessage + startMessage + "ThreadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());

                work();

                DateTime end = DateTime.Now.ToUniversalTime();
                TimeSpan duration = end - start;

                logger.WriteLog(LogLevel.Info, "■" + invokerMessage + string.Format(endMessage, duration.TotalMilliseconds,
                                                                                    duration.TotalSeconds, duration.TotalMinutes, duration.TotalHours,
                                                                                    duration.TotalDays) + "ThreadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            }));
        }
Exemple #17
0
        public static AspectF MustBeNonDefault <T>(this AspectF aspect, params T[] args)
            where T : IComparable
        {
            return(aspect.Combine((work) =>
            {
                T defaultvalue = default(T);
                for (int i = 0; i < args.Length; i++)
                {
                    T arg = args[i];
                    if (arg == null || arg.Equals(defaultvalue))
                    {
                        throw new ArgumentException(
                            string.Format("Parameter at index {0} is null", i));
                    }
                }

                work();
            }));
        }
Exemple #18
0
 public static AspectF VerifyPermission(this AspectF aspect, ILogger logger, IAuth auth, Action AuthErrorHandler)
 {
     return(aspect.Combine((work) =>
     {
         try
         {
             if (auth.VerifyPermission())
             {
                 work();
             }
             else
             {
                 AuthErrorHandler();
             }
         }
         catch (Exception x)
         {
             logger.WriteLog(LogLevel.Error, x);
             throw;
         }
     }));
 }
Exemple #19
0
 public static AspectF Retry(this AspectF aspects, Action <IEnumerable <Exception> > failHandler, ILogger logger)
 {
     return(aspects.Combine((work) =>
                            Retry(1000, 1, (error) => DoNothing(error), x => DoNothing(), work, logger)));
 }
Exemple #20
0
 public static AspectF Retry(this AspectF aspects, int retryDuration,
                             int retryCount, Action <Exception> errorHandler, ILogger logger)
 {
     return(aspects.Combine((work) =>
                            Retry(retryDuration, retryCount, errorHandler, x => DoNothing(), work, logger)));
 }
Exemple #21
0
 public static AspectF Retry(this AspectF aspects, int retryDuration,
                             int retryCount, Action <Exception> errorHandler, Action <IEnumerable <Exception> > retryFailed, ILogger logger)
 {
     return(aspects.Combine((work) =>
                            Retry(retryDuration, retryCount, errorHandler, retryFailed, work, logger)));
 }
Exemple #22
0
 public static AspectF Retry(this AspectF aspects, int retryDuration, ILogger logger)
 {
     return(aspects.Combine((work) =>
                            Retry(retryDuration, 1, (error) => DoNothing(error), x => DoNothing(), work, logger)));
 }