Beispiel #1
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.LogException(x);
                System.Threading.Thread.Sleep(1000);

                //Retry
                try
                {
                    Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
                }
                catch (Exception ex)
                {
                    logger.LogException(ex);
                    throw;
                }
            }
        }));
    }
Beispiel #2
0
 public static AspectF RunAsync(this AspectF aspect, Action compAspectFeCallback)
 {
     return(aspect.Combine(work => work.BeginInvoke(asyncresult =>
     {
         work.EndInvoke(asyncresult); compAspectFeCallback();
     }, null)));
 }
Beispiel #3
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.IsNull())
            {
                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.IsNull())
                {
                    GetListFromSource <TReturnType>(aspect, cacheResolver, key);
                }
                else
                {
                    aspect.m_WorkDelegate = new Func <TReturnType>(() => cachedType);
                }
            }

            work();
        }
Beispiel #4
0
 public static AspectF Delay(this AspectF aspect, int milliseconds)
 {
     return(aspect.Combine(work =>
     {
         Thread.Sleep(milliseconds);
         work();
     }));
 }
Beispiel #5
0
 public static TReturnType Use <TReturnType>(this AspectF aspect, TReturnType item, Action <TReturnType> action)
 {
     return(aspect.Return(() =>
     {
         action(item);
         return item;
     }));
 }
        public static AspectF GreaterOrEqual(this AspectF aspect, string parameterName, int actualValue, int fromValue)
        {
            if (actualValue < fromValue)
            {
                throw new Exception(@"{0}({1}) must be greather or equal to {2}".FormatWith(parameterName, actualValue, fromValue));
            }

            return(aspect);
        }
Beispiel #7
0
        public static AspectF NotNullOrEmpty(this AspectF aspect, string @object, string parameterName)
        {
            if (@object.IsNullOrEmpty())
            {
                throw new ArgumentNullException(parameterName);
            }

            return(aspect);
        }
Beispiel #8
0
        private static void GetListFromSource <TReturnType>(AspectF aspect, ICache cacheResolver, string key)
        {
            Func <TReturnType> workDelegate = (Func <TReturnType>)aspect.m_WorkDelegate;
            TReturnType        realObject   = workDelegate();

            cacheResolver.Add(key, realObject);
            workDelegate          = () => realObject;
            aspect.m_WorkDelegate = workDelegate;
        }
        public static AspectF GreaterOrEqual(this AspectF aspect, string parameterName, int actualValue, int fromValue)
        {
            if (actualValue < fromValue)
            {
                throw new Exception(@"{parameterName}({actualValue}) must be greather or equal to {fromValue}");
            }

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

            work();
        }));
    }
Beispiel #11
0
    public static AspectF Log(this AspectF aspect, ILogger logger,
                              string logMessage, params object[] arg)
    {
        return(aspect.Combine(work =>
        {
            logger.Log(string.Format(logMessage, arg));

            work();
        }));
    }
Beispiel #12
0
 public static AspectF While(this AspectF aspect, Func <bool> test)
 {
     return(aspect.Combine(work =>
     {
         while (test())
         {
             work();
         }
     }));
 }
Beispiel #13
0
 public static AspectF Timer(this AspectF aspect, string title)
 {
     return(aspect.Combine(work =>
     {
         Stopwatch start = Stopwatch.StartNew();
         work();
         start.Stop();
         Console.Out.WriteLine("{0}: {1}", title, start.Elapsed);
     }));
 }
Beispiel #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 =>
        {
            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.Set(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 (object cachedItem in cached.Select(item => 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;
            });
        }));
    }
Beispiel #15
0
 public static AspectF Until(this AspectF aspect, Func <bool> test)
 {
     return(aspect.Combine(work =>
     {
         while (!test())
         {
             ;
         }
         work();
     }));
 }
Beispiel #16
0
        public static AspectF WhenTrue(this AspectF aspect, params Func <bool>[] conditions)
        {
            return(aspect.Combine(work =>
            {
                if (conditions.Any(condition => !condition()))
                {
                    return;
                }

                work();
            }));
        }
Beispiel #17
0
    public static AspectF Log(this AspectF aspect, ILogger logger,
                              string beforeMessage, string afterMessage)
    {
        return(aspect.Combine(work =>
        {
            logger.Log(beforeMessage);

            work();

            logger.Log(afterMessage);
        }));
    }
Beispiel #18
0
 public static AspectF IgnoreExceptions(this AspectF aspect)
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch
         {
         }
     }));
 }
Beispiel #19
0
 public static AspectF IgnoreException <T>(this AspectF aspect) where T : Exception
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch (T)
         {
         }
     }));
 }
Beispiel #20
0
    private static void GetListFromSource <TReturnType>(AspectF aspect, ICache cacheResolver,
                                                        string key)
    {
        var workDelegate = aspect.WorkDelegate as Func <TReturnType>;

        if (workDelegate != null)
        {
            TReturnType realObject = workDelegate();
            cacheResolver.Add(key, realObject);
            workDelegate = () => realObject;
        }
        aspect.WorkDelegate = workDelegate;
    }
Beispiel #21
0
 public static AspectF TrapLog(this AspectF aspect, ILogger logger)
 {
     return(aspect.Combine(work =>
     {
         try
         {
             work();
         }
         catch (Exception x)
         {
             logger.LogException(x);
         }
     }));
 }
Beispiel #22
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());
         }
     }));
 }
Beispiel #23
0
 public static AspectF WriteLock(this AspectF aspect, ReaderWriterLockSlim @lock)
 {
     return(aspect.Combine(work =>
     {
         @lock.EnterWriteLock();
         try
         {
             work();
         }
         finally
         {
             @lock.ExitWriteLock();
         }
     }));
 }
Beispiel #24
0
 public static AspectF ReadLockUpgradable(this AspectF aspect, ReaderWriterLockSlim @lock)
 {
     return(aspect.Combine(work =>
     {
         @lock.EnterUpgradeableReadLock();
         try
         {
             work();
         }
         finally
         {
             @lock.ExitUpgradeableReadLock();
         }
     }));
 }
Beispiel #25
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.IsNull())
                    {
                        throw new ArgumentException(string.Format("Parameter at index {0} is null", i));
                    }
                }

                work();
            }));
        }
Beispiel #26
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.IsNull() || arg.Equals(defaultvalue))
                    {
                        throw new ArgumentException(string.Format("Parameter at index {0} is null", i));
                    }
                }

                work();
            }));
        }
Beispiel #27
0
    public static AspectF HowLong(this AspectF aspect, ILogger logger,
                                  string startMessage, string endMessage)
    {
        return(aspect.Combine(work =>
        {
            DateTime start = DateTime.Now;
            logger.Log(startMessage);

            work();

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

            logger.Log(string.Format(endMessage, duration.TotalMilliseconds,
                                     duration.TotalSeconds, duration.TotalMinutes, duration.TotalHours,
                                     duration.TotalDays));
        }));
    }
        public static AspectF Between(this AspectF aspect, string parameterName, int actualValue, int fromValue, int toValue)
        {
            if (fromValue > toValue)
            {
                throw new ArgumentOutOfRangeException(nameof(fromValue), @"From value cannot be less than to value");
            }

            if (actualValue < fromValue)
            {
                throw new Exception($"{parameterName}({actualValue}) must be greather or equal to {fromValue}");
            }

            if (actualValue > toValue)
            {
                throw new Exception($"{parameterName}({actualValue}) must be less or equal to {toValue}");
            }

            return(aspect);
        }
Beispiel #29
0
        public static AspectF CacheRetry <TReturnType>(this AspectF aspect,
                                                       ICache cacheResolver,
                                                       string key)
        {
            return(aspect.Combine(work =>
            {
                try
                {
                    Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
                }
                catch
                {
                    Thread.Sleep(1000);

                    //Retry
                    Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached);
                }
            }));
        }
Beispiel #30
0
        public static AspectF Between(this AspectF aspect, string parameterName, int actualValue, int fromValue, int toValue)
        {
            if (fromValue > toValue)
            {
                throw new ArgumentOutOfRangeException("fromValue", @"From value cannot be less than to value");
            }

            if (actualValue < fromValue)
            {
                throw new Exception(@"{0}({1}) must be greather or equal to {2}".FormatWith(parameterName, actualValue, fromValue));
            }

            if (actualValue > toValue)
            {
                throw new Exception(@"{0}({1}) must be less or equal to {2}".FormatWith(parameterName, actualValue, toValue));
            }

            return(aspect);
        }