public void HandleEvent(PreActionHookEvent eventMessage)
        {
            var entries = eventMessage.ModifiedEntries.ToArray();

            if (entries.Length == 0 || !_preHooks.Any())
            {
                return;
            }

            foreach (var entry in entries)
            {
                var e        = entry;          // Prevents access to modified closure
                var preHooks = GetPreHookInstancesFor(e.Entity.GetType());
                foreach (var hook in preHooks)
                {
                    if (hook.HookStates == e.PreSaveState && hook.RequiresValidation == eventMessage.RequiresValidation)
                    {
                        var metadata = new HookEntityMetadata(e.PreSaveState);
                        using (var scope = new DbContextScope(hooksEnabled: false))
                        {
                            // dead end: don't let hooks call hooks again
                            hook.HookObject(e.Entity, metadata);
                        }

                        if (metadata.HasStateChanged)
                        {
                            e.PreSaveState = metadata.State;
                        }
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Implements the interface.  This causes the hook to only run for objects that are assignable to TEntity.
 /// </summary>
 public void HookObject(object entity, HookEntityMetadata metadata)
 {
     if (typeof(TEntity).IsAssignableFrom(entity.GetType()))
     {
         Hook((TEntity)entity, metadata);
     }
 }
Пример #3
0
 /// <summary>
 /// The logic to perform per entity after the registered action gets performed.
 /// This gets run once per entity that has been changed.
 /// </summary>
 public abstract void Hook(TEntity entity, HookEntityMetadata metadata);