|
@@ -420,10 +420,26 @@ namespace InABox.Database
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ protected virtual void BeforeSave(IEnumerable<T> entities)
|
|
|
+ {
|
|
|
+ foreach(var entity in entities)
|
|
|
+ {
|
|
|
+ BeforeSave(entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
protected virtual void AfterSave(T entity)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
+ protected virtual void AfterSave(IEnumerable<T> entities)
|
|
|
+ {
|
|
|
+ foreach(var entity in entities)
|
|
|
+ {
|
|
|
+ AfterSave(entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
protected virtual void OnSave(T entity, ref string auditnote)
|
|
|
{
|
|
|
CheckAutoIncrement(entity);
|
|
@@ -498,56 +514,38 @@ namespace InABox.Database
|
|
|
|
|
|
public void Save(IEnumerable<T> entities, string auditnote)
|
|
|
{
|
|
|
- DoSave(entities, auditnote);
|
|
|
+ DoSave(entities.AsArray(), auditnote);
|
|
|
}
|
|
|
|
|
|
public void Save(IEnumerable<Entity> entities, string auditnote)
|
|
|
{
|
|
|
- var updates = new List<T>();
|
|
|
- foreach (var entity in entities)
|
|
|
- updates.Add((T)entity);
|
|
|
- DoSave(updates, auditnote);
|
|
|
+ DoSave(entities.Select(x => (T)x).ToArray(), auditnote);
|
|
|
}
|
|
|
|
|
|
- protected virtual void OnSave(IEnumerable<T> entities, ref string auditnote)
|
|
|
+ protected virtual void OnSave(T[] entities, ref string auditnote)
|
|
|
{
|
|
|
- CheckAutoIncrement(entities.ToArray());
|
|
|
+ CheckAutoIncrement(entities);
|
|
|
Provider.Save(entities);
|
|
|
}
|
|
|
|
|
|
- private void DoSave(IEnumerable<T> entities, string auditnote)
|
|
|
+ private void DoSave(T[] entities, string auditnote)
|
|
|
{
|
|
|
UpdateUserTracking(UserTrackingAction.Write);
|
|
|
|
|
|
- entities = RunScript(ScriptType.BeforeSave, entities.ToList());
|
|
|
-
|
|
|
- //OpenSession("Save", true);
|
|
|
+ entities = RunScript(ScriptType.BeforeSave, entities).AsArray();
|
|
|
|
|
|
- //try
|
|
|
- //{
|
|
|
// Process any AutoIncrement Fields before we apply the Unique Code test
|
|
|
// Thus, if we have a unique autoincrement, it will be populated prior to validation
|
|
|
- CheckAutoIncrement(entities.ToArray());
|
|
|
+ CheckAutoIncrement(entities);
|
|
|
|
|
|
var changes = new Dictionary<T, string>();
|
|
|
foreach (var entity in entities)
|
|
|
{
|
|
|
changes[entity] = entity.ChangedValues();
|
|
|
- //UpdateInternalLinks(entity);
|
|
|
- BeforeSave(entity);
|
|
|
}
|
|
|
+ BeforeSave(entities);
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
- //OpenSession("Save", true);
|
|
|
- OnSave(entities, ref auditnote);
|
|
|
- //CloseSession("Save", true);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- //CloseSession("Save", true);
|
|
|
- throw e;
|
|
|
- }
|
|
|
+ OnSave(entities, ref auditnote);
|
|
|
|
|
|
if (DbFactory.IsSupported<AuditTrail>())
|
|
|
{
|
|
@@ -559,10 +557,10 @@ namespace InABox.Database
|
|
|
if (!string.IsNullOrEmpty(auditnote))
|
|
|
notes.Add(auditnote);
|
|
|
|
|
|
- if (changes.ContainsKey(entity) && !string.IsNullOrEmpty(changes[entity]))
|
|
|
- notes.Add(changes[entity]);
|
|
|
+ if (changes.TryGetValue(entity, out string? value) && !string.IsNullOrEmpty(value))
|
|
|
+ notes.Add(value);
|
|
|
|
|
|
- if (notes.Any())
|
|
|
+ if (notes.Count != 0)
|
|
|
{
|
|
|
var audit = new AuditTrail
|
|
|
{
|
|
@@ -572,30 +570,18 @@ namespace InABox.Database
|
|
|
Note = string.Join(": ", notes)
|
|
|
};
|
|
|
audittrails.Add(audit);
|
|
|
- //Provider.Save<AuditTrail>(audit);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (audittrails.Any())
|
|
|
+ if (audittrails.Count != 0)
|
|
|
Provider.Save(audittrails);
|
|
|
}
|
|
|
|
|
|
- foreach (var entity in entities)
|
|
|
- {
|
|
|
- AfterSave(entity);
|
|
|
- //UpdateExternalLinks(entity, false);
|
|
|
- //entity.CommitChanges();
|
|
|
- }
|
|
|
+ AfterSave(entities);
|
|
|
|
|
|
- entities = RunScript(ScriptType.AfterSave, entities);
|
|
|
+ entities = RunScript(ScriptType.AfterSave, entities).AsArray();
|
|
|
|
|
|
NotifyListeners(entities);
|
|
|
-
|
|
|
- //}
|
|
|
- //catch (Exception e)
|
|
|
- //{
|
|
|
- // throw e;
|
|
|
- //}
|
|
|
}
|
|
|
|
|
|
private static List<Tuple<Type, Action<Guid[]>>> _listeners = new List<Tuple<Type, Action<Guid[]>>>();
|