Пример #1
0
        public void Create(InfusionGlobalPreference value)
        {
            using (log.Activity(m => m($"Creating {nameof(InfusionGlobalPreference)} by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateCreate(value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var entity = null as GlobalPreference;
                using (log.Activity(m => m("Create Entity")))
                {
                    try
                    {
                        entity = context.GlobalPreferences.Add(value);
                        context.SaveChanges();
                    }
                    catch (Exception e) when(e.HasDuplicateKeyNumber())
                    {
                        log.Warn($"Duplicate {nameof(InfusionGlobalPreference.Name)}:\"{value.Name}\"", e);
                        throw new DuplicateKeyException(value.Name);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnCreated(newValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(m => m($"Created {nameof(InfusionGlobalPreference)}[{entity.Id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }
Пример #2
0
        public void Create(UserPreference value)
        {
            using (log.Activity(m => m($"Creating {nameof(UserPreference)} by {Thread.CurrentPrincipal?.Identity?.Name}")))
            {
                using (log.Activity(m => m("Authorization")))
                {
                    try
                    {
                        security.ValidateCreate(value);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        log.Warn($"Authorization Denied");
                        throw;
                    }
                    catch (Exception e)
                    {
                        log.Error($"Authorization Error", e);
                        throw;
                    }
                }

                var entity = null as UserPreference;
                using (log.Activity(m => m("Create Entity")))
                {
                    try
                    {
                        entity = context.UserPreferences.Add(value);
                        context.SaveChanges();
                    }
                    //TODO: KB: Do this on index validation
                    //throw new DuplicateKeyException(value.Name)
                    catch (Exception e)
                    {
                        log.Error($"Update Error", e);
                        throw;
                    }
                }
                var newValue = entity.Filter();

                using (log.Activity(m => m("Emit Event")))
                {
                    try
                    {
                        emitter.OnCreated(newValue);
                    }
                    catch (Exception e)
                    {
                        log.Error($"Emit Event Error", e);
                        throw;
                    }
                }

                log.Info(m => m($"Created {nameof(UserPreference)}[{entity.Id}] by {Thread.CurrentPrincipal?.Identity?.Name}"));
            }
        }