Exemplo n.º 1
0
        /// <summary>
        /// Run Common If Code Schema with Validation
        /// Schema : Init --> Pre --> Validate -->
        /// ((Process or ValidFalse or ValidNull) or Fail)
        /// --> Post --> End.
        /// Check methods Init and End methods return bool? results and run CheckTrue/CheckFalse/CheckNull methods if available
        /// or return default on not true
        /// </summary>
        public static ResT I <ParamT, ResT>(Pr <ParamT, ResT> ProcessToRun, Cx CurrCodeType = null)
        {
            if (ProcessToRun.V == null && ProcessToRun.Vn == null)
            {
                return(ProcessToRun.D);
            }

            Pr <ParamT, ResT> Res = ProcessToRun;

            Res.Vt = (param, code) =>
            {
                bool?Validated = true;
                ResT Result    = ProcessToRun.D;
                if (ProcessToRun.V != null)
                {
                    Validated = ProcessToRun.V(param, CurrCodeType);
                }

                if (Validated == null)
                {
                    if (ProcessToRun.Vn != null)
                    {
                        Result = ProcessToRun.Vn(param, CurrCodeType);
                    }
                    else
                    if (ProcessToRun.F != null)
                    {
                        Result = ProcessToRun.F(param, CurrCodeType);
                    }
                }
                else
                if (Validated == false)
                {
                    if (ProcessToRun.Vf != null)
                    {
                        Result = ProcessToRun.Vf(param, CurrCodeType);
                    }
                    else
                    if (ProcessToRun.F != null)
                    {
                        Result = ProcessToRun.F(param, CurrCodeType);
                    }
                }
                else
                if (ProcessToRun.Vt != null)
                {
                    Result = ProcessToRun.Vt(param, CurrCodeType);
                }
                else
                if (ProcessToRun.F != null)
                {
                    Result = ProcessToRun.F(param, CurrCodeType);
                }
                return(Result);
            };

            return(F(Res, CurrCodeType));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Run Common Try Code Schema Block With Errors And Performance Logging based on CurrCodeType.
        /// Schema : Init --> Pre --> Validate -->
        /// try[CustomUseTryCatch or CurrCodeType]
        /// ((Process or ValidFalse or ValidNull) or Fail) with Error OnErrorCatch
        /// --> Post --> End.
        /// Check methods Init and End methods return bool? results and run CheckTrue/CheckFalse/CheckNull methods if available
        /// or return default on not true
        /// </summary>
        public static ResT Ti <ParamT, ResT>(Pr <ParamT, ResT> ProcessToRun, Cx CurrCodeType = null)
        {
            bool UseTryCatch = CurrCodeType != null && CurrCodeType.T;

            Pr <ParamT, ResT> Res = ProcessToRun;

            Res.Vt = (param, code) =>
            {
                bool?Validated = true;
                ResT Result    = ProcessToRun.D;
                if (ProcessToRun.V != null)
                {
                    Validated = ProcessToRun.V(param, CurrCodeType);
                }

                if (Validated == null)
                {
                    if (ProcessToRun.Vn != null)
                    {
                        Result = ProcessToRun.Vn(param, CurrCodeType);
                    }
                    else
                    if (ProcessToRun.F != null)
                    {
                        Result = ProcessToRun.F(param, CurrCodeType);
                    }
                }
                else
                if (Validated == false)
                {
                    if (ProcessToRun.Vf != null)
                    {
                        Result = ProcessToRun.Vf(param, CurrCodeType);
                    }
                    else
                    if (ProcessToRun.F != null)
                    {
                        Result = ProcessToRun.F(param, CurrCodeType);
                    }
                }
                else
                if (ProcessToRun.Vt != null)
                {
                    Result = ProcessToRun.Vt(param, CurrCodeType);
                }
                else
                if (ProcessToRun.F != null)
                {
                    Result = ProcessToRun.F(param, CurrCodeType);
                }
                return(Result);
            };

            return(T(Res, CurrCodeType));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create new Code Context for current Settings with UseTryCatch, LogError and Elevated setting
        /// </summary>
        public static Cx X(this Sls Settings, bool UseTryCatch = true, bool LogError = true, bool Elevated = false)
        {
            var Res = new Cx()
            {
                S = new Sls()
            };

            Res.T = UseTryCatch;
            Res.S = Settings;
            Res.L = LogError;
            Res.E = Elevated;

            return(Res);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Run Common Sync Try FuncToRunSync With Errors And Performance Logging based on CurrCodeType.
        /// Logging is saved to CustomLogToAddRecords or default static logs (_.D.L)
        /// </summary>
        public static T r <T>(this Lg CurrLog, f <T> FuncToRunSync, Cx CustomCodeType = null, Mx CustomMethodContext = null, T ReturnOnError = default)
        {
            if (FuncToRunSync == null)
            {
                return(ReturnOnError);
            }

            var t       = CustomCodeType;
            Prf PerfLog = null;
            var ef      = false;

            if (t.S != null && t.S.P == true)
            {
                PerfLog = CurrLog.P(t, CustomMethodContext);
            }

            T Result = ReturnOnError;

            if (CustomCodeType.T)
            {
                try
                {
                    Result = FuncToRunSync();
                }
                catch (Exception exc)
                {
                    if (t.S != null && t.S.L == true)
                    {
                        CurrLog.E(null, exc, t, CustomMethodContext);
                    }
                    ef = true;
                }
            }
            else
            {
                Result = FuncToRunSync();
            }

            if (t.S != null && t.S.P == true)
            {
                CurrLog.Pa(PerfLog, null, t);
            }

            if (ef)
            {
                return(ReturnOnError);
            }

            return(Result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Run Common Try FuncToRun With Errors And Performance Logging based on CurrContext and optional CustomCodeType.
        /// </summary>
        public static T r <T, Tx>(this Ax <Tx> CurrContext, f <Cx, T> FuncToRun, Tx CustomCodeType = default, Mx CustomMethodContext = null, T ReturnOnError = default)
        {
            if (CurrContext == null || FuncToRun == null)
            {
                return(ReturnOnError);
            }
            Cx x = null;

            if (CustomCodeType != null && CurrContext.T != null)
            {
                x = CurrContext.T.v(CustomCodeType);
            }
            return(CurrContext.L.r(() => FuncToRun(x), x, CustomMethodContext, ReturnOnError));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Run Common Async Try FuncToRunAsync With Errors And Performance Logging based on CurrContext and optional CustomCodeType.
        /// </summary>
        public static async Task <T> R <T, Tx>(this Ax <Tx> CurrContext, f <Cx, Task <T> > FuncToRunAsync, Tx CustomCodeType = default, Mx CustomMethodContext = null, T ReturnOnError = default)
        {
            if (CurrContext == null || FuncToRunAsync == null)
            {
                return(ReturnOnError);
            }
            Cx x = null;

            if (CustomCodeType != null && CurrContext.T != null)
            {
                x = CurrContext.T.v(CustomCodeType);
            }
            return(await CurrContext.L.R(() => FuncToRunAsync(x), x, CustomMethodContext, ReturnOnError));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Run Common Try Code Schema  with Error Handling
        /// Schema : Init --> Pre --> Validate -->
        /// try[CustomUseTryCatch or CurrCodeType] (Process or Fail) with Error OnErrorCatch
        /// --> Post --> End.
        /// Check methods Init and End methods return bool? results and run CheckTrue/CheckFalse/CheckNull methods if available
        /// or return default on not true
        /// </summary>
        public static ResT T <ParamT, ResT>(Pr <ParamT, ResT> ProcessToRun, Cx CurrCodeType = null)
        {
            bool UseTryCatch = CurrCodeType != null && CurrCodeType.T;

            Pr <ParamT, ResT> Res = ProcessToRun;

            Res.Vt = (param, code) =>
            {
                ResT Result = ProcessToRun.D;
                if (UseTryCatch)
                {
                    try
                    {
                        if (ProcessToRun.Vt != null)
                        {
                            Result = ProcessToRun.Vt(param, CurrCodeType);
                        }
                        else
                        {
                            Result = ProcessToRun.F(param, CurrCodeType);
                        }
                    }
                    catch (Exception exc)
                    {
                        Result = ProcessToRun.D;
                        if (ProcessToRun.Er != null)
                        {
                            Result = ProcessToRun.Er(param, Result, exc, CurrCodeType);
                        }
                    }
                }
                else
                if (ProcessToRun.Vt != null)
                {
                    Result = ProcessToRun.Vt(param, CurrCodeType);
                }
                else
                {
                    Result = ProcessToRun.F(param, CurrCodeType);
                }
                return(Result);
            };

            return(F(Res, CurrCodeType));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Run Common Code Schema without additional  code
        /// Schema : Init --> Pre --> (Process  or Fail) --> Post --> End.
        /// Check methods Init and End methods return bool? results and run CheckTrue/CheckFalse/CheckNull methods if available
        /// or return default on not true
        /// </summary>
        public static ResT F <ParamT, ResT>(Pr <ParamT, ResT> ProcessToRun, Cx CurrCodeType = null)
        {
            if ((ProcessToRun.Vt == null && ProcessToRun.F == null) || ProcessToRun.P == null)
            {
                return(ProcessToRun.D);
            }

            if (!Code_FuncUtils.E(ProcessToRun, ProcessToRun.I, CurrCodeType))
            {
                return(ProcessToRun.D);
            }

            ParamT CurrParam = ProcessToRun.P;

            if (ProcessToRun.Pe != null)
            {
                CurrParam = ProcessToRun.Pe(ProcessToRun.P, CurrCodeType);
            }

            ResT Result = ProcessToRun.D;

            if (ProcessToRun.Vt != null)
            {
                Result = ProcessToRun.Vt(ProcessToRun.P, CurrCodeType);
            }
            else
            {
                Result = ProcessToRun.F(ProcessToRun.P, CurrCodeType);
            }
            if (ProcessToRun.Ps != null)
            {
                Result = ProcessToRun.Ps(ProcessToRun.P, Result, CurrCodeType);
            }

            if (!Code_FuncUtils.E(ProcessToRun, ProcessToRun.E, Result, CurrCodeType))
            {
                return(ProcessToRun.D);
            }

            return(Result);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Create new custom process with specified methods
        /// </summary>
        public static Pr <ParamT, ResT> E <ParamT, ResT>(this ParamT StartParam, f <ParamT, Cx, ResT> ProcessFunc, Cx CurrCodeType = null, f <ParamT, Cx, ResT> ProcessFail = null, f <ParamT, ResT, Exception, Cx, ResT> ProcessError = null)
        {
            Pr <ParamT, ResT> Result = new Pr <ParamT, ResT>();

            Result.P = StartParam;
            if (ProcessFunc == null)
            {
                Result.Vt = ProcessFunc;
            }
            if (ProcessFail == null)
            {
                Result.F = ProcessFail;
            }
            if (ProcessError == null)
            {
                Result.Er = ProcessError;
            }

            return(Result);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Create new Func and add AddOn method with Original with AddAfterOriginal order
        /// </summary>
        public static f <ParamT, ResT, Cx, ResT> A <ParamT, ResT>(this f <ParamT, ResT, Cx, ResT> Original, f <ParamT, ResT, Cx, ResT> AddOn, Cx CurrCodeType = null, bool AddAfterOriginal = false)
        {
            if (Original == null && AddOn == null)
            {
                return(null);
            }

            return((key, arr, code) =>
            {
                var Res = default(ResT);

                if (!AddAfterOriginal && AddOn != null)
                {
                    Res = AddOn(key, arr, code);
                }

                if (Original != null)
                {
                    Res = Original(key, arr, code);
                }

                if (AddAfterOriginal && AddOn != null)
                {
                    Res = AddOn(key, arr, code);
                }
                return Res;
            });
        }
Exemplo n.º 11
0
        /// <summary>
        /// Evaluate common bool? return from CheckToEvaluate with context CheckFalse/CheckNull/CheckTrue methods details
        /// Returns true if any method return true, else false
        /// </summary>
        /// <returns>Returns true if any method return true, else false</returns>
        public static bool E <ParamT, ResT>(this Pr <ParamT, ResT> CurrProcess, f <ParamT, ResT, Cx, bool?> CheckToEvaluate, ResT ResToEvaluate = default(ResT), Cx CurrCodeType = null, bool EvaluateDefault = true)
        {
            if (CheckToEvaluate == null)
            {
                return(EvaluateDefault);
            }
            bool?Res = CheckToEvaluate(CurrProcess.P, ResToEvaluate, CurrCodeType);

            if (Res == null && CurrProcess.Cn != null)
            {
                return(CurrProcess.Cn(CurrProcess.P, ResToEvaluate, CurrCodeType));
            }
            if (Res == false && CurrProcess.Cf != null)
            {
                return(!CurrProcess.Cf(CurrProcess.P, ResToEvaluate, CurrCodeType));
            }
            if (Res == true && CurrProcess.Ct != null)
            {
                return(!CurrProcess.Ct(CurrProcess.P, ResToEvaluate, CurrCodeType));
            }

            if (Res == true)
            {
                return(true);
            }
            return(false);
        }