예제 #1
0
        /// <summary>
        /// Process the current contents of the pipeline. Puts the result back into the pipeline
        /// </summary>
        /// <param name="action">the method to process the current contents of the pipeline</param>
        /// <returns>the modification of the data for the next pipeline segment in the pipeline </returns>
        public PipeResult <T> Process(Func <T, T> action, string label = null)
        {
            if (ShortCircuitOnError && IsError)
            {
                return(this);
            }
            try
            {
                Result = action(Result);
            }
            catch (Exception e)
            {
                if (!IgnoreErrors)
                {
                    throw;
                }
                Errors.Add(label ?? action.Method.ToString(), e);
                if (OnErrorReturn != null)
                {
                    Result = OnErrorReturn.Invoke(Result, e);
                }
            }

            return(this);
        }
예제 #2
0
        /// <summary>
        /// Run any function taking in T but ignores its result returning rather the prior result instead
        /// </summary>
        /// <param name="action"></param>
        /// <param name="throwIfErrors">Throw if any errors where encountered in pipeline before runnings action</param>
        /// <returns>Returns the result prior to the then</returns>
        public T ThenUse(Action <T> action, bool throwIfErrors = false)
        {
            if (throwIfErrors)
            {
                throw new AggregateException(Errors.Values);
            }
            if (ShortCircuitOnError)
            {
                return(Result);
            }
            try
            {
                action(Result);
            }
            catch (Exception e)
            {
                if (!IgnoreErrors)
                {
                    throw;
                }
                Errors.Add(action.Method.ToString(), e);
                if (OnErrorReturn != null)
                {
                    Result = OnErrorReturn.Invoke(Result, e);
                }
            }

            return(Result);
        }
예제 #3
0
        public PipeResult <T> Processes(IEnumerable <Func <T, T> > actions)
        {
            if (ShortCircuitOnError && IsError)
            {
                return(this);
            }

            var prevValue = default(T);

            foreach (var action in actions)
            {
                try
                {
                    prevValue = action(prevValue.Equals(default(T))
                            ? Result
                            : prevValue);
                }
                catch (Exception e)
                {
                    if (!IgnoreErrors)
                    {
                        throw;
                    }
                    // Haven't got a way to supply multiple lables for mltiple stages yet
                    // So use the executing stage method string as error context
                    Errors.Add(action.Method.ToString(), e);

                    if (OnErrorReturn == null)
                    {
                        continue;
                    }
                    Result = OnErrorReturn.Invoke(Result, e);

                    if (ShortCircuitOnError)
                    {
                        return(this);
                    }

                    prevValue = Result;
                }
            }

            Result = prevValue;

            return(this);
        }
예제 #4
0
        /// <summary>
        /// Run an any action but ignores its result and returns prior result instead
        /// </summary>
        /// <param name="action"></param>
        /// <returns>returns the result prior to the then</returns>
        public T ThenUse(Func <T, T> action)
        {
            if (ShortCircuitOnError && IsError)
            {
                return(Result);
            }
            try
            {
                return(action(Result));
            }
            catch (Exception e)
            {
                Errors.Add(action.Method.ToString(), e);
                if (OnErrorReturn != null)
                {
                    Result = OnErrorReturn.Invoke(Result, e);
                }
            }

            return(Result);
        }