예제 #1
0
 public static AspectF RunAsync(this AspectF aspect, Action completeCallback)
 {
     return(aspect.Combine((work) => work.BeginInvoke(asyncresult =>
     {
         work.EndInvoke(asyncresult); completeCallback();
     }, null)));
 }
예제 #2
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;
                    }
                }
            }));
        }
예제 #3
0
 public static AspectF RunAsync(this AspectF aspect)
 {
     return(aspect.Combine((work) => work.BeginInvoke(asyncresult =>
     {
         work.EndInvoke(asyncresult);
     }, null)));
 }
예제 #4
0
        private static void Cache <TReturnType>(AspectF aspect, ICache cacheResolver,
                                                string key, Action work, Func <TReturnType, TReturnType> foundInCache)
        {
            object cachedData = cacheResolver.Get(key);

            if (cachedData == null)
            {
                GetListFromSource <TReturnType>(aspect, cacheResolver, key);
            }
            else
            {
                // Give caller a chance to shape the cached item before it is returned
                TReturnType cachedType = foundInCache((TReturnType)cachedData);
                if (cachedType == null)
                {
                    GetListFromSource <TReturnType>(aspect, cacheResolver, key);
                }
                else
                {
                    aspect.WorkDelegate = new Func <TReturnType>(() => cachedType);
                }
            }

            work();
        }
예제 #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);
     }));
 }
예제 #6
0
 public static AspectF Delay(this AspectF aspect, int milliseconds)
 {
     return(aspect.Combine((work) =>
     {
         System.Threading.Thread.Sleep(milliseconds);
         work();
     }));
 }
예제 #7
0
        private static void GetListFromSource <TReturnType>(AspectF aspect, ICache cacheResolver, string key)
        {
            Func <TReturnType> workDelegate = aspect.WorkDelegate as Func <TReturnType>;
            TReturnType        realObject   = workDelegate();

            cacheResolver.Add(key, realObject);
            workDelegate        = () => realObject;
            aspect.WorkDelegate = workDelegate;
        }
예제 #8
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();
            }));
        }
예제 #9
0
 public static AspectF While(this AspectF aspect, Func <bool> test)
 {
     return(aspect.Combine((work) =>
     {
         while (test())
         {
             work();
         }
     }));
 }
예제 #10
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();
            }));
        }
예제 #11
0
        public static AspectF Until(this AspectF aspect, Func <bool> test)
        {
            return(aspect.Combine((work) =>
            {
                while (!test())
                {
                    ;
                }

                work();
            }));
        }
예제 #12
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);
            }));
        }
예제 #13
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());
            }));
        }
예제 #14
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;
                });
            }));
        }
예제 #15
0
 public static AspectF TrapLog(this AspectF aspect, ILogger logger)
 {
     return(aspect.Combine((work) =>
     {
         try
         {
             work();
         }
         catch (Exception x)
         {
             logger.WriteLog(LogLevel.Error, x);
         }
     }));
 }
예제 #16
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();
            }));
        }
예제 #17
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();
            }));
        }
예제 #18
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());
            }));
        }
예제 #19
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();
            }));
        }
예제 #20
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;
         }
     }));
 }
예제 #21
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)));
 }
예제 #22
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)));
 }
예제 #23
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)));
 }
예제 #24
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)));
 }