Update: Repository Monitor Deferred Actions
Defer code in 'before commit' to run 'after commit'
This commit is contained in:
@@ -74,11 +74,18 @@ namespace Disco.Data.Repository.Monitor
|
|||||||
|
|
||||||
internal static void UpdateAfterEventFromEntryState(RepositoryMonitorEvent monitorEvent)
|
internal static void UpdateAfterEventFromEntryState(RepositoryMonitorEvent monitorEvent)
|
||||||
{
|
{
|
||||||
|
monitorEvent.afterCommit = true;
|
||||||
|
|
||||||
if (monitorEvent.EventType == RepositoryMonitorEventType.Added)
|
if (monitorEvent.EventType == RepositoryMonitorEventType.Added)
|
||||||
{
|
{
|
||||||
// Update Entity Key for Added Events
|
// Update Entity Key for Added Events
|
||||||
monitorEvent.EntityKey = DetermineEntityKey(monitorEvent.objectEntryState);
|
monitorEvent.EntityKey = DetermineEntityKey(monitorEvent.objectEntryState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute Deferred Actions
|
||||||
|
if (monitorEvent.executeAfterCommit != null)
|
||||||
|
foreach (var deferredAction in monitorEvent.executeAfterCommit)
|
||||||
|
deferredAction.Invoke(monitorEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static RepositoryMonitorEvent EventFromEntryState(DiscoDataContext dbContext, DbEntityEntry entityEntry, ObjectStateEntry entryState)
|
internal static RepositoryMonitorEvent EventFromEntryState(DiscoDataContext dbContext, DbEntityEntry entityEntry, ObjectStateEntry entryState)
|
||||||
|
|||||||
@@ -12,11 +12,13 @@ namespace Disco.Data.Repository.Monitor
|
|||||||
public class RepositoryMonitorEvent
|
public class RepositoryMonitorEvent
|
||||||
{
|
{
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
internal ObjectStateEntry objectEntryState { get; set; }
|
internal ObjectStateEntry objectEntryState;
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
internal DbEntityEntry dbEntityState { get; set; }
|
internal DbEntityEntry dbEntityState;
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
internal bool afterCommit { get; set; }
|
internal bool afterCommit;
|
||||||
|
[JsonIgnore]
|
||||||
|
internal List<Action<RepositoryMonitorEvent>> executeAfterCommit;
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public DiscoDataContext dbContext { get; set; }
|
public DiscoDataContext dbContext { get; set; }
|
||||||
@@ -44,7 +46,7 @@ namespace Disco.Data.Repository.Monitor
|
|||||||
public T GetPreviousPropertyValue<T>(string PropertyName)
|
public T GetPreviousPropertyValue<T>(string PropertyName)
|
||||||
{
|
{
|
||||||
if (afterCommit)
|
if (afterCommit)
|
||||||
throw new InvalidOperationException("Unable to determine property values after repository commit");
|
throw new InvalidOperationException("Unable to determine property values after repository commit, use a deferred action instead");
|
||||||
else
|
else
|
||||||
return (T)dbEntityState.OriginalValues[PropertyName];
|
return (T)dbEntityState.OriginalValues[PropertyName];
|
||||||
}
|
}
|
||||||
@@ -52,5 +54,20 @@ namespace Disco.Data.Repository.Monitor
|
|||||||
{
|
{
|
||||||
return (T)dbEntityState.CurrentValues[PropertyName];
|
return (T)dbEntityState.CurrentValues[PropertyName];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ExecuteAfterCommit(Action<RepositoryMonitorEvent> action){
|
||||||
|
if (afterCommit)
|
||||||
|
{
|
||||||
|
// Execute Immediately
|
||||||
|
action.Invoke(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Defer Execution
|
||||||
|
if (executeAfterCommit == null)
|
||||||
|
executeAfterCommit = new List<Action<RepositoryMonitorEvent>>();
|
||||||
|
executeAfterCommit.Add(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user