void IDeleteOperation.Delete(IEntity entity, params object[]?args) { using (HeavyProfiler.Log("Delete", () => Symbol.Symbol.Key)) { OperationLogic.AssertOperationAllowed(Symbol.Symbol, entity.GetType(), inUserInterface: false); string?error = OnCanDelete((T)entity); if (error != null) { throw new ApplicationException(error); } OperationLogEntity log = new OperationLogEntity { Operation = Symbol.Symbol, Start = TimeZoneManager.Now, User = UserHolder.Current?.ToLite() !, }; using (OperationLogic.AllowSave(entity.GetType())) OperationLogic.OnSuroundOperation(this, log, entity, args).EndUsing(_ => { try { using (Transaction tr = new Transaction()) { OnDelete((T)entity, args); log.SetTarget(entity); log.End = TimeZoneManager.Now; log.SaveLog(); tr.Commit(); } } catch (Exception ex) { OperationLogic.SetExceptionData(ex, Symbol.Symbol, (Entity)entity, args); if (Transaction.InTestTransaction) { throw; } var exLog = ex.LogException(); using (Transaction tr2 = Transaction.ForceNew()) { log.Target = entity.ToLite(); log.Exception = exLog.ToLite(); log.SaveLog(); tr2.Commit(); } throw; } }); } }
IEntity IConstructOperation.Construct(params object[]?args) { using (HeavyProfiler.Log("Construct", () => operationSymbol.Key)) { OperationLogic.AssertOperationAllowed(operationSymbol, typeof(T), inUserInterface: false); OperationLogEntity?log = new OperationLogEntity { Operation = operationSymbol, Start = TimeZoneManager.Now, User = UserHolder.Current?.ToLite() !, }; try { using (Transaction tr = new Transaction()) { T?result = null; using (OperationLogic.AllowSave <T>()) OperationLogic.OnSuroundOperation(this, log, null, args).EndUsing(_ => { result = Construct(args); AssertEntity(result); if ((result != null && !result.IsNew) || LogAlsoIfNotSaved) { log.SetTarget(result); log.End = TimeZoneManager.Now; } else { log = null; } }); if (log != null) { log.SaveLog(); } return(tr.Commit(result !)); } } catch (Exception ex) { OperationLogic.SetExceptionData(ex, operationSymbol, null, args); if (LogAlsoIfNotSaved) { if (Transaction.InTestTransaction) { throw; } var exLog = ex.LogException(); using (Transaction tr2 = Transaction.ForceNew()) { log !.Exception = exLog.ToLite(); log.SaveLog(); tr2.Commit(); } } throw; } } }
void IExecuteOperation.Execute(IEntity entity, params object[]?args) { using (HeavyProfiler.Log("Execute", () => Symbol.Symbol.Key)) { OperationLogic.AssertOperationAllowed(Symbol.Symbol, entity.GetType(), inUserInterface: false); string?error = OnCanExecute((T)entity); if (error != null) { throw new ApplicationException(error); } OperationLogEntity log = new OperationLogEntity { Operation = Symbol.Symbol, Start = TimeZoneManager.Now, User = UserHolder.Current?.ToLite() ! }; try { using (Transaction tr = new Transaction()) { using (OperationLogic.AllowSave(entity.GetType())) OperationLogic.OnSuroundOperation(this, log, entity, args).EndUsing(_ => { Execute((T)entity, args); AssertEntity((T)entity); if (!AvoidImplicitSave) { entity.Save(); //Nothing happens if already saved } log.SetTarget(entity); log.End = TimeZoneManager.Now; }); log.SaveLog(); tr.Commit(); } } catch (Exception ex) { OperationLogic.SetExceptionData(ex, Symbol.Symbol, (Entity)entity, args); if (Transaction.InTestTransaction) { throw; } var exLog = ex.LogException(); using (Transaction tr2 = Transaction.ForceNew()) { OperationLogEntity newLog = new OperationLogEntity //Transaction chould have been rollbacked just before commiting { Operation = log.Operation, Start = log.Start, User = log.User, Target = entity.IsNew ? null : entity.ToLite(), Exception = exLog.ToLite(), }; newLog.SaveLog(); tr2.Commit(); } throw; } } }
IEntity IConstructorFromManyOperation.Construct(IEnumerable <Lite <IEntity> > lites, params object[]?args) { using (HeavyProfiler.Log("ConstructFromMany", () => operationSymbol.Key)) { foreach (var type in lites.Select(a => a.EntityType).Distinct()) { OperationLogic.AssertOperationAllowed(operationSymbol, type, inUserInterface: false); } OperationLogEntity?log = new OperationLogEntity { Operation = operationSymbol, Start = TimeZoneManager.Now, User = UserHolder.Current?.ToLite() ! }; try { using (Transaction tr = new Transaction()) { T?result = null; using (OperationLogic.AllowSave <F>()) using (OperationLogic.AllowSave <T>()) OperationLogic.OnSuroundOperation(this, log, null, args).EndUsing(_ => { result = OnConstruct(lites.Cast <Lite <F> >().ToList(), args); AssertEntity(result); if ((result != null && !result.IsNew) || LogAlsoIfNotSaved) { log.End = TimeZoneManager.Now; log.SetTarget(result); } else { log = null; } }); if (log != null) { log.SaveLog(); } return(tr.Commit(result !)); } } catch (Exception ex) { OperationLogic.SetExceptionData(ex, operationSymbol, null, args); if (LogAlsoIfNotSaved) { if (Transaction.InTestTransaction) { throw; } var exLog = ex.LogException(); using (Transaction tr2 = Transaction.ForceNew()) { log !.Exception = exLog.ToLite(); log.SaveLog(); tr2.Commit(); } } throw; } } }
IEntity IConstructorFromOperation.Construct(IEntity origin, params object[]?args) { using (HeavyProfiler.Log("ConstructFrom", () => operationSymbol.Key)) { OperationLogic.AssertOperationAllowed(operationSymbol, origin.GetType(), inUserInterface: false); string?error = OnCanConstruct(origin); if (error != null) { throw new ApplicationException(error); } OperationLogEntity?log = new OperationLogEntity { Operation = operationSymbol, Start = TimeZoneManager.Now, User = UserHolder.Current?.ToLite() !, Origin = origin.ToLite(origin.IsNew), }; try { using (Transaction tr = new Transaction()) { T?result = null; using (OperationLogic.AllowSave(origin.GetType())) using (OperationLogic.AllowSave <T>()) OperationLogic.OnSuroundOperation(this, log, origin, args).EndUsing(_ => { result = Construct((F)origin, args); AssertEntity(result); if ((result != null && !result.IsNew) || LogAlsoIfNotSaved) { log.End = TimeZoneManager.Now; log.SetTarget(result); } else { log = null; } }); if (log != null) { log.SaveLog(); } return(tr.Commit(result !)); } } catch (Exception ex) { OperationLogic.SetExceptionData(ex, operationSymbol, (Entity)origin, args); if (LogAlsoIfNotSaved) { if (Transaction.InTestTransaction) { throw; } var exLog = ex.LogException(); using (Transaction tr2 = Transaction.ForceNew()) { log !.Exception = exLog.ToLite(); log.SaveLog(); tr2.Commit(); } } throw; } } }
IEntity IConstructorFromManyOperation.Construct(IEnumerable <Lite <IEntity> > lites, params object[] args) { using (HeavyProfiler.Log("ConstructFromMany", () => Symbol.Symbol.Key)) { OperationLogic.AssertOperationAllowed(Symbol.Symbol, inUserInterface: false); OperationLogEntity log = new OperationLogEntity { Operation = Symbol.Symbol, Start = TimeZoneManager.Now, User = UserHolder.Current?.ToLite() }; try { using (Transaction tr = new Transaction()) { T result; using (OperationLogic.AllowSave <F>()) using (OperationLogic.AllowSave <T>()) using (OperationLogic.OnSuroundOperation(this, log, null, args)) { result = OnConstruct(lites.Cast <Lite <F> >().ToList(), args); AssertEntity(result); if ((result != null && !result.IsNew) || LogAlsoIfNotSaved) { log.End = TimeZoneManager.Now; log.SetTarget(result); } else { log = null; } } if (log != null) { using (ExecutionMode.Global()) log.Save(); } return(tr.Commit(result)); } } catch (Exception ex) { OperationLogic.SetExceptionData(ex, Symbol.Symbol, null, args); if (LogAlsoIfNotSaved) { if (Transaction.InTestTransaction) { throw; } var exLog = ex.LogException(); using (Transaction tr2 = Transaction.ForceNew()) { log.Exception = exLog.ToLite(); using (ExecutionMode.Global()) log.Save(); tr2.Commit(); } } throw; } } }