Adds the persistence helpers for safe entity updates

This commit is contained in:
BinaryAlley committed 2026-09-21 13:38:21 +03:00
1 parent 5a8c2dc405
commit 89886ebe22
2 files changed
+110

No files matched your search

@@ -0,0 +1,71 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.DataAccess.Common.Persistence;
/// <summary>
/// Reconciles the children of a tracked entity against a desired set, preserving the identity of the children that already exist.
/// </summary>
internal static class CollectionReconciler
{
/// <summary>
/// Applies the desired <paramref name="incomingItems"/> onto the tracked <paramref name="existingItems"/>, by matching both sides with the provided key selectors.
/// </summary>
/// <remarks>
/// 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
/// <paramref name="shouldReplace"/> 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.
/// </remarks>
/// <typeparam name="TExisting">The type of the tracked items.</typeparam>
/// <typeparam name="TIncoming">The type of the desired items.</typeparam>
/// <typeparam name="TKey">The type of the key used to match the two sides.</typeparam>
/// <param name="existingItems">The tracked collection that is reconciled in place.</param>
/// <param name="incomingItems">The desired items.</param>
/// <param name="existingKeySelector">Selects the matching key of a tracked item.</param>
/// <param name="incomingKeySelector">Selects the matching key of a desired item.</param>
/// <param name="shouldReplace">Determines whether a matched tracked item must be replaced, because a non-key value changed.</param>
/// <param name="createNew">Creates the entity used for a desired item that is not matched by any tracked item.</param>
public static void Reconcile<TExisting, TIncoming, TKey>(
ICollection<TExisting> existingItems,
IEnumerable<TIncoming> incomingItems,
Func<TExisting, TKey> existingKeySelector,
Func<TIncoming, TKey> incomingKeySelector,
Func<TExisting, TIncoming, bool> shouldReplace,
Func<TIncoming, TExisting> 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<TKey, TExisting> 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<TIncoming> incomingList = [.. incomingItems];
HashSet<TKey> 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));
}
}
}
@@ -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;
/// <summary>
/// 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.
/// </summary>
internal static class EditableValuesCopier
{
/// <summary>
/// Copies the scalar values of <paramref name="incoming"/> onto the tracked <paramref name="tracked"/> entity, restoring the identity and audit columns afterwards,
/// so that the tracked row is only considered modified when an editable value actually changed.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <typeparam name="TEntity">The type of the tracked and mapped repository entity.</typeparam>
/// <param name="context">The Entity Framework context that tracks <paramref name="tracked"/>.</param>
/// <param name="tracked">The tracked entity whose editable values are updated.</param>
/// <param name="incoming">The mapped entity carrying the desired values.</param>
public static void CopyEditableValues<TEntity>(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;
}
}