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)); } })); }
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(); }
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))); }
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; })); }
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(); })); }
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(); })); }
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(); } })); }
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(); })); }
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; }
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)); } })); }
///<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(); } } })); }
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); } })); }
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; } })); }
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); } })); }
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); } })); }
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); }
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))); }
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))); }
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))); }
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)); }
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()); } })); }
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; } } })); }
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)); }
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)); }
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; }); })); }
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))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLogThrow(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect) { return(TrapLogThrow(aspect, Let.Logger())); }
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())); }
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))); }
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()))); }