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:
parent
859c03177e
commit
70e3fe5dd7
@ -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, 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(Expression<Func<TEntity, bool>> expression, CancellationToken cancellation = default);
|
||||||
|
|
||||||
|
public Task DeleteAsync(IQueryable<TEntity> query, CancellationToken cancellation = default);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,9 +37,11 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
|
|
||||||
public IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
|
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--)
|
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);
|
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--)
|
for (int i = entities.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user