Introduces the closed generic persistence toolkit

This commit is contained in:
BinaryAlley committed 2026-09-27 21:20:35 +03:00
1 parent 9ba9b089b5
commit a632f4ced7
8 files changed
+270 -19

No files matched your search

@@ -2,22 +2,26 @@
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.DataAccess.Core.UoW;
using Microsoft.EntityFrameworkCore;
using System;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using System.Collections.Generic;
using System.Linq;
#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.
/// Copies the editable scalar values of a mapped repository entity onto its tracked counterpart, while leaving the primary key and the audit columns under the sole ownership of the persistence medium.
/// </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.
/// Copies the scalar values of <paramref name="incoming"/> onto the tracked <paramref name="tracked"/> entity, except for its primary key and its audit columns,
/// so that the tracked row is only considered modified when an editable value actually changed, and its stored identity is never reassigned.
/// </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
/// The primary key is not copied, so that a mapped entity carrying a different, or unset, key can never retarget the tracked row. The audit interceptor stamps the
/// modification columns of the rows that are actually modified, so the audit columns are not copied either: copying them 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>
@@ -26,14 +30,24 @@ internal static class EditableValuesCopier
/// <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;
EntityEntry<TEntity> trackedEntry = context.Entry(tracked);
EntityEntry<TEntity> incomingEntry = context.Entry(incoming);
// The primary key and the audit columns are owned by the persistence medium, so they are never copied from the mapped entity.
HashSet<string> excludedProperties =
[
.. (trackedEntry.Metadata.FindPrimaryKey()?.Properties.Select(property => property.Name) ?? []),
nameof(IAuditableEntity.CreatedOnUtc),
nameof(IAuditableEntity.CreatedBy),
nameof(IAuditableEntity.UpdatedOnUtc),
nameof(IAuditableEntity.UpdatedBy)
];
foreach (IProperty property in trackedEntry.Metadata.GetProperties())
{
if (excludedProperties.Contains(property.Name))
continue;
trackedEntry.Property(property.Name).CurrentValue = incomingEntry.Property(property.Name).CurrentValue;
}
}
}