示例#1
0
        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     = Clock.Now,
                    User      = UserHolder.Current?.ToLite() !,
                };

                using (OperationLogic.AllowSave(entity.GetType()))
                    OperationLogic.OnSuroundOperation(this, log, entity, args).EndUsing(_ =>
                    {
                        try
                        {
                            using (var tr = new Transaction())
                            {
                                OnDelete((T)entity, args);

                                log.SetTarget(entity);
                                log.End = Clock.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 (var tr2 = Transaction.ForceNew())
                            {
                                log.Target    = entity.ToLite();
                                log.Exception = exLog.ToLite();

                                log.SaveLog();

                                tr2.Commit();
                            }

                            throw;
                        }
                    });
            }
        }
示例#2
0
        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     = Clock.Now,
                    User      = UserHolder.Current?.ToLite() !,
                };

                try
                {
                    using (var tr = new Transaction())
                    {
                        T?result = null;
                        using (OperationLogic.AllowSave <T>())
                            OperationLogic.OnSuroundOperation(this, log, null, args).EndUsing(_ =>
                            {
                                result = Construct(args);

                                if (result != null)
                                {
                                    AssertEntity(result);
                                }

                                if ((result != null && !result.IsNew) || LogAlsoIfNotSaved)
                                {
                                    log.SetTarget(result);
                                    log.End = Clock.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 (var tr2 = Transaction.ForceNew())
                        {
                            log !.Exception = exLog.ToLite();

                            log.SaveLog();

                            tr2.Commit();
                        }
                    }

                    throw;
                }
            }
        }
示例#3
0
        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     = Clock.Now,
                    User      = UserHolder.Current?.ToLite() !
                };

                try
                {
                    using (var 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);

                                    if (result != null)
                                    {
                                        AssertEntity(result);
                                    }

                                    if ((result != null && !result.IsNew) || LogAlsoIfNotSaved)
                                    {
                                        log.End = Clock.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 (var tr2 = Transaction.ForceNew())
                        {
                            log !.Exception = exLog.ToLite();

                            log.SaveLog();

                            tr2.Commit();
                        }
                    }

                    throw;
                }
            }
        }
示例#4
0
        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     = Clock.Now,
                    User      = UserHolder.Current?.ToLite() !
                };

                try
                {
                    using (var 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 = Clock.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 (var 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;
                }
            }
        }
示例#5
0
        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     = Clock.Now,
                    User      = UserHolder.Current?.ToLite() !,
                    Origin    = origin.ToLite(origin.IsNew),
                };

                try
                {
                    using (var 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);

                                    if (result != null)
                                    {
                                        AssertEntity(result);
                                    }

                                    if ((result != null && !result.IsNew) || LogAlsoIfNotSaved)
                                    {
                                        log.End = Clock.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 (var tr2 = Transaction.ForceNew())
                        {
                            log !.Exception = exLog.ToLite();

                            log.SaveLog();

                            tr2.Commit();
                        }
                    }

                    throw;
                }
            }
        }