public void HandleEvent(PreActionHookEvent eventMessage) { var entries = eventMessage.ModifiedEntries; if (!entries.Any() || !_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; } } } } }
public void HandleEvent(PostActionHookEvent eventMessage) { var entries = eventMessage.ModifiedEntries.ToArray(); if (entries.Length == 0 || !_postHooks.Any()) { return; } foreach (var entry in entries) { var e = entry; // Prevents access to modified closure var postHooks = GetPostHookInstancesFor(e.Entity.GetType()); foreach (var hook in postHooks) { if (hook.HookStates == e.PreSaveState) { 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); } } } } }
/// <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); } }
public void HandleEvent(PreActionHookEvent eventMessage) { var entries = eventMessage.ModifiedEntries; if (!entries.Any() || !_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); hook.HookObject(e.Entity, metadata); if (metadata.HasStateChanged) { e.PreSaveState = metadata.State; } } } } }
public void HandleEvent(PostActionHookEvent eventMessage) { var entries = eventMessage.ModifiedEntries; if (!entries.Any() || !_postHooks.Any()) { return; } foreach (var entry in entries) { var e = entry; // Prevents access to modified closure var postHooks = GetPostHookInstancesFor(e.Entity.GetType()); foreach (var hook in postHooks) { if (hook.HookStates == e.PreSaveState) { var metadata = new HookEntityMetadata(e.PreSaveState); hook.HookObject(e.Entity, metadata); } } } }
/// <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);