Пример #1
0
        /// <summary>
        /// Decorates each method to perform common tasks
        /// </summary>
        /// <typeparam name="T">Result type of the decorated function</typeparam>
        /// <param name="action">A lambda expression representing the business logic</param>
        /// <param name="save">Specifies if SaveChanges must be performed each time</param>
        /// <returns>The object returned by the decorated function</returns>

        private T Decorator <T>(Func <T> action, bool save = true)
        {
            try
            {
                ResetRules();
                T    result = action.Invoke();
                bool status = GetRulesStatus();
                if (save && AutoSave && status)
                {
                    SaveChanges();
                }
                if (!status)
                {
                    StackTrace stackTrace = new StackTrace();
                    string     methodName = stackTrace.GetFrame(1).GetMethod().Name;
                    foreach (var rule in ApplicationRules.Where(r => !r.Result))
                    {
                        _logger.LogInfo(rule.Reason.ToString(), methodName);
                    }
                }
                return(result);
            }
            catch (Exception ex)
            {
                StackTrace stackTrace = new StackTrace();
                string     methodName = stackTrace.GetFrame(1).GetMethod().Name;
                _logger.LogError(ex, methodName);
                return(default(T));
            }
        }