diff --git a/src/Lumina.DataAccess/Common/Persistence/CollectionReconciler.cs b/src/Lumina.DataAccess/Common/Persistence/CollectionReconciler.cs new file mode 100644 index 00000000..40c99b54 --- /dev/null +++ b/src/Lumina.DataAccess/Common/Persistence/CollectionReconciler.cs @@ -0,0 +1,71 @@ +#region ========================================================================= USING ===================================================================================== +using System; +using System.Collections.Generic; +using System.Linq; +#endregion + +namespace Lumina.DataAccess.Common.Persistence; + +/// +/// Reconciles the children of a tracked entity against a desired set, preserving the identity of the children that already exist. +/// +internal static class CollectionReconciler +{ + /// + /// Applies the desired onto the tracked , by matching both sides with the provided key selectors. + /// + /// + /// Existing items that are absent from the desired set are removed, desired items that are absent are added, and matched items are left untouched, unless + /// reports that their non-key values changed, in which case only that item is replaced. Because the tracked instances are + /// never rebuilt as a whole, the rows that did not change keep their identity and their audit columns. + /// + /// The type of the tracked items. + /// The type of the desired items. + /// The type of the key used to match the two sides. + /// The tracked collection that is reconciled in place. + /// The desired items. + /// Selects the matching key of a tracked item. + /// Selects the matching key of a desired item. + /// Determines whether a matched tracked item must be replaced, because a non-key value changed. + /// Creates the entity used for a desired item that is not matched by any tracked item. + public static void Reconcile( + ICollection existingItems, + IEnumerable incomingItems, + Func existingKeySelector, + Func incomingKeySelector, + Func shouldReplace, + Func createNew) + where TExisting : class + where TKey : notnull + { + // Index the tracked children by their key, so that each desired child can be matched in constant time; the first item wins when duplicate keys exist. + Dictionary existingByKey = []; + foreach (TExisting existingItem in existingItems) + existingByKey.TryAdd(existingKeySelector(existingItem), existingItem); + + // Materialize the desired children once, so the source is not enumerated repeatedly, and collect their keys for the removal pass. + List incomingList = [.. incomingItems]; + HashSet incomingKeys = [.. incomingList.Select(incomingKeySelector)]; + + // Remove the tracked children that are absent from the desired set, so that deletions are applied. + foreach (TExisting existingItem in existingItems.ToList()) + if (!incomingKeys.Contains(existingKeySelector(existingItem))) + existingItems.Remove(existingItem); + + // Reconcile each desired child, replacing a matched one only when its non-key values changed, so unchanged rows keep their identity and audit columns. + foreach (TIncoming incomingItem in incomingList) + { + TKey incomingKey = incomingKeySelector(incomingItem); + if (existingByKey.TryGetValue(incomingKey, out TExisting? matchedItem) && matchedItem is not null) + { + if (shouldReplace(matchedItem, incomingItem)) + { + existingItems.Remove(matchedItem); + existingItems.Add(createNew(incomingItem)); + } + } + else // The desired child has no tracked counterpart, so it is added as a new child. + existingItems.Add(createNew(incomingItem)); + } + } +} diff --git a/src/Lumina.DataAccess/Common/Persistence/EditableValuesCopier.cs b/src/Lumina.DataAccess/Common/Persistence/EditableValuesCopier.cs new file mode 100644 index 00000000..003da754 --- /dev/null +++ b/src/Lumina.DataAccess/Common/Persistence/EditableValuesCopier.cs @@ -0,0 +1,39 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Application.Common.DataAccess.Entities.Common; +using Lumina.DataAccess.Core.UoW; +using Microsoft.EntityFrameworkCore; +using System; +#endregion + +namespace Lumina.DataAccess.Common.Persistence; + +/// +/// Copies the editable scalar values of a mapped repository entity onto its tracked counterpart, while leaving the audit columns under the sole ownership of the auditing interceptor. +/// +internal static class EditableValuesCopier +{ + /// + /// Copies the scalar values of onto the tracked entity, restoring the identity and audit columns afterwards, + /// so that the tracked row is only considered modified when an editable value actually changed. + /// + /// + /// The audit interceptor stamps the modification columns of the rows that are actually modified. Copying the audit columns from the mapped entity would mark untouched + /// rows as modified, which would then be stamped with a change that the user never made. + /// + /// The type of the tracked and mapped repository entity. + /// The Entity Framework context that tracks . + /// The tracked entity whose editable values are updated. + /// The mapped entity carrying the desired values. + public static void CopyEditableValues(LuminaDbContext context, TEntity tracked, TEntity incoming) where TEntity : class, IAuditableEntity + { + DateTime createdOnUtc = tracked.CreatedOnUtc; + Guid createdBy = tracked.CreatedBy; + DateTime? updatedOnUtc = tracked.UpdatedOnUtc; + Guid? updatedBy = tracked.UpdatedBy; + context.Entry(tracked).CurrentValues.SetValues(incoming); + tracked.CreatedOnUtc = createdOnUtc; + tracked.CreatedBy = createdBy; + tracked.UpdatedOnUtc = updatedOnUtc; + tracked.UpdatedBy = updatedBy; + } +}