feat(repository): refactor UpdateAsync and DeleteAsync to accept query functions
Changed UpdateAsync and DeleteAsync to accept Func<IQueryable<TEntity>, IQueryable<TEntity>> instead of IQueryable<TEntity> Improved flexibility by allowing callers to compose queries dynamically Updated overloads to wrap expressions with q => q.Where(expression) for compatibility
This commit is contained in:
parent
003636e243
commit
75d0a6f1df
@ -16,9 +16,9 @@ public interface IRepository<TEntity>
|
|||||||
|
|
||||||
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task UpdateAsync<TDto>(TDto dto, IQueryable<TEntity> query, CancellationToken cancel = default);
|
public Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task DeleteAsync(IQueryable<TEntity> query, CancellationToken cancel = default);
|
public Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,11 +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 Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => UpdateAsync(dto, Entities.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, IQueryable<TEntity> query, CancellationToken cancel = default)
|
public virtual async Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
||||||
{
|
{
|
||||||
var entities = await query.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--)
|
||||||
{
|
{
|
||||||
@ -52,11 +52,11 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
await Context.SaveChangesAsync(cancel);
|
await Context.SaveChangesAsync(cancel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => DeleteAsync(Entities.Where(expression), cancel);
|
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => DeleteAsync(q => q.Where(expression), cancel);
|
||||||
|
|
||||||
public virtual async Task DeleteAsync(IQueryable<TEntity> query, CancellationToken cancel = default)
|
public virtual async Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
||||||
{
|
{
|
||||||
var entities = await query.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--)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user