public BusinessBase(ILogger logger = null, bool logOnException = true) { Result = new BusinessResult(); this.Logger = logger; ThrowException = false; this.logOnException = logOnException; OnBeforeExecute = () => true; OnBeforeExecuteAsync = () => Task.Run(() => true); }
public virtual void Execute(Func <bool> onExecute) { this.OnExecute = onExecute; if (Result == null) { Result = new BusinessResult(); } try { if (this.OnExecute == null) { throw new ArgumentNullException("OnExecute"); } if (OnBeforeExecute != null) { Result.IsOnBeforExecute = OnBeforeExecute(); if (Result.IsOnBeforExecute == false) { return; } } if (this.OnExecute != null) { Result.IsOnExecute = this.OnExecute(); if (Logger != null && logOnException == false && LogInfo != null) { Logger.LogList.Add(LogInfo); LogInfo = null; } if (Result.IsOnExecute == false) { return; } } if (OnAfterExecute != null) { Result.IsOnAfterExecute = OnAfterExecute(); } } catch (Exception ex) { if (Logger != null) { Logger.Log(ex, this.GetType().FullName, (Thread.CurrentPrincipal.Identity as Identity)?.Id ?? 0); LogInfo = null; } Result.Exception = ex; Result.Message = new BusinessMessage("خطا", "در سامانه خطایی رخ داده است."); if (ThrowException) { throw ex; } } }