コード例 #1
0
        public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF HowLong(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger,
                                                                                Action invokeThisAfterLogedStartMessage, string startMessage, string endMessage)
        {
            return(aspect.Combine(work =>
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                logger.Info(startMessage);

                aspect.TrapLog().Do(invokeThisAfterLogedStartMessage);

                try
                {
                    work();
                }
                finally
                {
                    watch.Stop();
                    TimeSpan duration = watch.Elapsed;

                    logger.Info(string.Format(endMessage, duration.TotalMilliseconds,
                                              duration.TotalSeconds, duration.TotalMinutes, duration.TotalHours,
                                              duration.TotalDays));
                }
            }));
        }
コード例 #2
0
        private static void Cache <TReturnType>(LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ICacheWrapper cacheResolver,
                                                string key, Action work, Func <TReturnType, TReturnType> foundInCache)
        {
            TReturnType cachedData = cacheResolver.Get <TReturnType>(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(cachedData);
                if (cachedType == null)
                {
                    GetListFromSource <TReturnType>(aspect, cacheResolver, key);
                }
                else
                {
                    aspect.WorkDelegate = new Func <TReturnType>(() => cachedType);
                }
            }

            work();
        }
コード例 #3
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF RunAsync(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Action completeCallback)
 {
     return(aspect.Combine(work => work.BeginInvoke(asyncresult =>
     {
         work.EndInvoke(asyncresult); completeCallback();
     }, null)));
 }
コード例 #4
0
 public static TReturnType Use <TReturnType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, TReturnType item, Action <TReturnType> action)
 {
     return(aspect.Return(() =>
     {
         action(item);
         return item;
     }));
 }
コード例 #5
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Delay(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, int milliseconds)
 {
     return(aspect.Combine(work =>
     {
         System.Threading.Thread.Sleep(milliseconds);
         work();
     }));
 }
コード例 #6
0
        public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Log(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger,
                                                                            string logMessage, params object[] arg)
        {
            return(aspect.Combine(work =>
            {
                logger.Info(string.Format(logMessage, arg));

                work();
            }));
        }
コード例 #7
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF While(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Func <bool> test)
 {
     return(aspect.Combine(work =>
     {
         while (test())
         {
             work();
         }
     }));
 }
コード例 #8
0
        public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF WhenTrue(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, params Func <bool>[] conditions)
        {
            return(aspect.Combine(work =>
            {
                if (conditions.Any(condition => !condition()))
                {
                    return;
                }

                work();
            }));
        }
コード例 #9
0
        private static void GetListFromSource <TReturnType>(LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ICacheWrapper cacheResolver,
                                                            string key)
        {
            var workDelegate = aspect.WorkDelegate as Func <TReturnType>;

            if (workDelegate != null)
            {
                TReturnType realObject = workDelegate();
                cacheResolver.Save(key, realObject);
                workDelegate = () => realObject;
            }
            aspect.WorkDelegate = workDelegate;
        }
コード例 #10
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLog(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger, Action <Exception> errorhandler)
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch (Exception x)
         {
             Let.Us.TrapLog().Do(() => errorhandler.Invoke(x));
         }
     }));
 }
コード例 #11
0
 ///<summary>
 /// Transaction
 /// (The "Distributed Transaction Coordinator" service must be started)
 ///</summary>
 ///<param name="aspect"></param>
 ///<param name="transactionScopeOption"></param>
 ///<param name="transactionOptions"></param>
 ///<param name="complete"></param>
 ///<returns></returns>
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Transaction(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, TransactionScopeOption transactionScopeOption, TransactionOptions transactionOptions, bool complete = true)
 {
     return(aspect.Combine(work =>
     {
         using (var scope = new TransactionScope(transactionScopeOption, transactionOptions))
         {
             work();
             if (complete)
             {
                 scope.Complete();
             }
         }
     }));
 }
コード例 #12
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLog(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger)
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch (Exception x)
         {
             logger.Error(x);
         }
     }));
 }
コード例 #13
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLogThrow(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Action <Exception> errorhandler)
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch (Exception x)
         {
             errorhandler.Invoke(x);
             throw;
         }
     }));
 }
コード例 #14
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Log(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger,
                                                                     Action beforeAction, Action afterAction)
 {
     return(aspect.Combine(work =>
     {
         try
         {
             Let.Us.TrapLog().Do(beforeAction);
             Let.Us.TrapLogThrow().Do(work);
         }
         finally
         {
             Let.Us.TrapLog().Do(afterAction);
         }
     }));
 }
コード例 #15
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Log(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger,
                                                                     string beforeMessage, string afterMessage)
 {
     return(aspect.Combine(work =>
     {
         bool hasError = true;
         try
         {
             logger.Info(beforeMessage);
             Let.Us.Do(work);
             hasError = false;
         }
         finally
         {
             afterMessage = hasError
                                                          ? afterMessage + "(Error Occured !!!)"
                                                          : afterMessage;
             logger.Info(afterMessage);
         }
     }));
 }
コード例 #16
0
        public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF HowLong(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Action <TimeSpan> onSuccessHandler, Action <TimeSpan, Exception> onFailHandler, Action <Exception> onErrorhandler = null)
        {
            var retValue = aspect.Combine(work =>
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                var errorOccured = false;
                Exception exc    = null;
                try
                {
                    work();
                }
                catch (Exception exception)
                {
                    exc          = exception;
                    errorOccured = true;

                    onErrorhandler?.Invoke(exception);
                }
                finally
                {
                    watch.Stop();
                    TimeSpan duration = watch.Elapsed;

                    if (errorOccured)
                    {
                        onFailHandler(duration, exc);
                    }
                    else
                    {
                        onSuccessHandler(duration);
                    }
                }
            });

            return(retValue);
        }
コード例 #17
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF RunAsync(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect)
 {
     return(aspect.Combine(work => work.BeginInvoke(work.EndInvoke, null)));
 }
コード例 #18
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryDuration, ILogger logger)
 {
     return(aspects.Combine(work =>
                            Retry(retryDuration, 1, error => DoNothing(error), x => DoNothing(), work, logger)));
 }
コード例 #19
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryDuration,
                                                                       int retryCount, Action <Exception> errorHandler, ILogger logger)
 {
     return(aspects.Combine(work =>
                            Retry(retryDuration, retryCount, errorHandler, x => DoNothing(), work, logger)));
 }
コード例 #20
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLog(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Action <Exception> errorhandler)
 {
     return(TrapLog(aspect, Let.Logger(), errorhandler));
 }
コード例 #21
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Expected <TException>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect)
     where TException : Exception
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch (TException x)
         {
             Debug.WriteLine(x.ToString());
         }
     }));
 }
コード例 #22
0
        public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CacheRetry <TReturnType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect,
                                                                                                 ICacheWrapper cacheResolver,
                                                                                                 ILogger logger,
                                                                                                 string key)
        {
            return(aspect.Combine(work =>
            {
                try
                {
                    Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
                }
                catch (Exception x)
                {
                    logger.Error(x);
                    System.Threading.Thread.Sleep(1000);

                    //Retry
                    try
                    {
                        Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                }
            }));
        }
コード例 #23
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CacheRetry <TReturnType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect,
                                                                                          string key)
 {
     return(CacheRetry <TReturnType>(aspect, Let.Cache(), Let.Logger(), key));
 }
コード例 #24
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CacheList <TItemType, TListType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect,
                                                                                                  string listCacheKey, Func <TItemType, string> getItemKey)
     where TListType : IList <TItemType>, new()
 {
     return(CacheList <TItemType, TListType>(aspect, Let.Cache(), listCacheKey, getItemKey));
 }
コード例 #25
0
        public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CacheList <TItemType, TListType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect,
                                                                                                         ICacheWrapper cacheResolver, string listCacheKey, Func <TItemType, string> getItemKey)
            where TListType : IList <TItemType>, new()
        {
            return(aspect.Combine(work =>
            {
                var 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 = () =>
                {
                    if (workDelegate != null)
                    {
                        TListType collection = workDelegate();
                        foreach (TItemType item in collection)
                        {
                            string key = getItemKey(item);
                            cacheResolver.Save(key, item);
                        }
                        return collection;
                    }
                    return default(TListType);
                };
                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.
                    var itemList = new TListType();
                    foreach (TItemType cachedItem in cached.Select(item => cacheResolver.Get <TItemType>(getItemKey(item))))
                    {
                        if (null != cachedItem)
                        {
                            itemList.Add(cachedItem);
                        }
                        else
                        {
                            // One of the item is missing from cache. So, discard the
                            // cached list.
                            return default(TListType);
                        }
                    }

                    return itemList;
                });
            }));
        }
コード例 #26
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Cache <TReturnType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect,
                                                                                     ICacheWrapper cacheResolver, string key)
 {
     return(aspect.Combine(work => Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached)));
 }
コード例 #27
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLogThrow(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect)
 {
     return(TrapLogThrow(aspect, Let.Logger()));
 }
コード例 #28
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryDuration,
                                                                       int retryCount, Action <Exception> errorHandler)
 {
     return(Retry(aspects, retryDuration, retryCount, errorHandler, Let.Logger()));
 }
コード例 #29
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.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)));
 }
コード例 #30
0
 public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryCount, int retryDuration = 500)
 {
     return(aspects.Combine(work =>
                            Retry(retryDuration, retryCount, x => DoNothing(), x => DoNothing(), work, Let.Logger())));
 }