refactor(repository): make UpdateAsync more flexible with Action<TEntity>

- Introduced Action<TEntity>-based UpdateAsync method for more flexible updates
- Updated TDto-based UpdateAsync to delegate to the new method
- Reduced code duplication and Mapper dependency
- Preserved existing Create, Read, and Delete functionality
This commit is contained in:
tekh 2025-11-05 14:20:12 +01:00
parent 5567f6731b
commit d16a4b6bb4
2 changed files with 123 additions and 111 deletions

View File

@ -1,5 +1,4 @@
using DigitalData.Core.Abstractions.Interfaces; using System.Linq.Expressions;
using System.Linq.Expressions;
#if NETFRAMEWORK #if NETFRAMEWORK
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -15,8 +14,8 @@ namespace DigitalData.Core.Abstraction.Application.Repository
{ {
#endif #endif
public interface IRepository<TEntity> public interface IRepository<TEntity>
{ {
#region Create #region Create
#if NET #if NET
public public
@ -71,6 +70,16 @@ public interface IRepository<TEntity>
public public
#endif #endif
Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default); Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
#if NET
public
#endif
Task UpdateAsync(Action<TEntity> modification, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
#if NET
public
#endif
Task UpdateAsync(Action<TEntity> modification, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
#endregion Update #endregion Update
#region Delete #region Delete
@ -98,7 +107,7 @@ public interface IRepository<TEntity>
#endif #endif
IQueryable<TEntity> ReadOnly(); IQueryable<TEntity> ReadOnly();
#endregion #endregion
} }
#if NETFRAMEWORK #if NETFRAMEWORK
} }
#endif #endif

View File

@ -21,8 +21,8 @@ namespace DigitalData.Core.Infrastructure
{ {
#endif #endif
public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class
{ {
protected internal readonly TDbContext Context; protected internal readonly TDbContext Context;
protected internal readonly DbSet<TEntity> Entities; protected internal readonly DbSet<TEntity> Entities;
@ -87,22 +87,25 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
#region Update #region Update
public virtual Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => UpdateAsync(dto, q => q.Where(expression), cancel); public virtual Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => UpdateAsync(dto, q => q.Where(expression), cancel);
public virtual async Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default) public virtual Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
=> UpdateAsync(entity => Mapper
#if NET
!
#endif
.Map(dto, entity), query, cancel);
public virtual async Task UpdateAsync(Action<TEntity> modification, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
{ {
var entities = await query(Entities).ToListAsync(cancel); var entities = await query(Entities).ToListAsync(cancel);
for (int i = entities.Count - 1; i >= 0; i--) for (int i = entities.Count - 1; i >= 0; i--)
{ modification.Invoke(entities[i]);
Mapper
#if NET
!
#endif
.Map(dto, entities[i]);
Entities.Update(entities[i]);
}
await Context.SaveChangesAsync(cancel); await Context.SaveChangesAsync(cancel);
} }
public virtual Task UpdateAsync(Action<TEntity> modification, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default)
=> UpdateAsync(modification, q => q.Where(expression), cancel);
#endregion Update #endregion Update
#region Delete #region Delete
@ -128,7 +131,7 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
[Obsolete("Use IRepository<TEntity>.Get")] [Obsolete("Use IRepository<TEntity>.Get")]
public virtual IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking(); public virtual IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
#endregion #endregion
} }
#if NETFRAMEWORK #if NETFRAMEWORK
} }
#endif #endif