Refactor(DbRepository): Add IQueryable overloads for UpdateAsync and DeleteAsync

- Added UpdateAsync<TDto>(TDto dto, IQueryable<TEntity> query, ...) overload
- Added DeleteAsync(IQueryable<TEntity> query, ...) overload
- Expression-based methods now delegate to the IQueryable overloads
- Reduces code duplication and allows more flexible queries
This commit is contained in:
tekh 2025-08-25 14:27:55 +02:00
parent 859c03177e
commit 70e3fe5dd7
2 changed files with 12 additions and 4 deletions

View File

@ -16,5 +16,9 @@ public interface IRepository<TEntity>
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default);
public Task UpdateAsync<TDto>(TDto dto, IQueryable<TEntity> query, CancellationToken cancellation = default);
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default);
public Task DeleteAsync(IQueryable<TEntity> query, CancellationToken cancellation = default);
}

View File

@ -37,9 +37,11 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
public IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
public virtual async Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
public virtual Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default) => UpdateAsync(dto, Entities.Where(expression), ct);
public virtual async Task UpdateAsync<TDto>(TDto dto, IQueryable<TEntity> query, CancellationToken ct = default)
{
var entities = await Entities.Where(expression).ToListAsync(ct);
var entities = await query.ToListAsync(ct);
for (int i = entities.Count - 1; i >= 0; i--)
{
@ -50,9 +52,11 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
await Context.SaveChangesAsync(ct);
}
public virtual async Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken ct = default) => DeleteAsync(Entities.Where(expression), ct);
public virtual async Task DeleteAsync(IQueryable<TEntity> query, CancellationToken ct = default)
{
var entities = await Entities.Where(expression).ToListAsync(ct);
var entities = await query.ToListAsync(ct);
for (int i = entities.Count - 1; i >= 0; i--)
{