070681888e
Defer code in 'before commit' to run 'after commit'
74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity.Infrastructure;
|
|
using System.Data.Objects;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Disco.Data.Repository.Monitor
|
|
{
|
|
public class RepositoryMonitorEvent
|
|
{
|
|
[JsonIgnore]
|
|
internal ObjectStateEntry objectEntryState;
|
|
[JsonIgnore]
|
|
internal DbEntityEntry dbEntityState;
|
|
[JsonIgnore]
|
|
internal bool afterCommit;
|
|
[JsonIgnore]
|
|
internal List<Action<RepositoryMonitorEvent>> executeAfterCommit;
|
|
|
|
[JsonIgnore]
|
|
public DiscoDataContext dbContext { get; set; }
|
|
|
|
public RepositoryMonitorEventType EventType { get; set; }
|
|
|
|
[JsonIgnore]
|
|
public Type EntityType { get; set; }
|
|
|
|
public string EntityTypeName
|
|
{
|
|
get
|
|
{
|
|
return EntityType.Name;
|
|
}
|
|
}
|
|
|
|
[JsonIgnore]
|
|
public object Entity { get; set; }
|
|
|
|
public Dictionary<string, object> EntityKey { get; set; }
|
|
|
|
public string[] ModifiedProperties { get; set; }
|
|
|
|
public T GetPreviousPropertyValue<T>(string PropertyName)
|
|
{
|
|
if (afterCommit)
|
|
throw new InvalidOperationException("Unable to determine property values after repository commit, use a deferred action instead");
|
|
else
|
|
return (T)dbEntityState.OriginalValues[PropertyName];
|
|
}
|
|
public T GetCurrentPropertyValue<T>(string 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);
|
|
}
|
|
}
|
|
}
|
|
}
|