Skip to content

Commit

Permalink
Remove FilterSet usage from repository implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Jul 2, 2024
1 parent b73c641 commit df98b3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
26 changes: 13 additions & 13 deletions src/BitzArt.CA.Core/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ public interface IRepository<TEntity> : IRepository
public void Remove(TEntity entity);

public void RemoveRange(IEnumerable<TEntity> entities);
[Obsolete("Use an overload with Func<IQueryable<TEntity>, IQueryable<TResult>> instead")]
public Task<IEnumerable<TEntity>> GetAllAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default);

public Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default);

public Task<IEnumerable<TResult>> GetAllAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TResult>> filter, CancellationToken cancellationToken = default);

[Obsolete("Use an overload with Func<IQueryable<TEntity>, IQueryable<TResult>> instead")]
public Task<PageResult<TEntity>> GetPageAsync(PageRequest pageRequest, IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default);
public Task<PageResult<TEntity>> GetPageAsync(PageRequest pageRequest, CancellationToken cancellationToken = default);

public Task<PageResult<TResult>> GetPageAsync<TResult>(PageRequest pageRequest, Func<IQueryable<TEntity>, IQueryable<TResult>> filter, CancellationToken cancellationToken = default);

[Obsolete("Use an overload with Func<IQueryable<TEntity>, IQueryable<TResult>> instead")]
public Task<TEntity?> GetAsync(IFilterSet<TEntity> filter, CancellationToken cancellationToken = default);
public Task<TEntity?> GetAsync(CancellationToken cancellationToken = default);

public Task<TResult?> GetAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TResult>> filter, CancellationToken cancellationToken = default);

[Obsolete("Use an overload with Func<IQueryable<TEntity>, IQueryable<TResult>> instead")]
public Task<int> CountAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default);
public Task<int> CountAsync(CancellationToken cancellationToken = default);

public Task<int> CountAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TResult>> filter, CancellationToken cancellationToken = default);

[Obsolete("Use an overload with Func<IQueryable<TEntity>, IQueryable<TResult>> instead")]
public Task<long> LongCountAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default);
public Task<long> LongCountAsync(CancellationToken cancellationToken = default);

public Task<long> LongCountAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TResult>> filter, CancellationToken cancellationToken = default);

[Obsolete("Use an overload with Func<IQueryable<TEntity>, IQueryable<TResult>> instead")]
public Task<bool> AnyAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default);
public Task<bool> AnyAsync(CancellationToken cancellationToken = default);

public Task<bool> AnyAsync<TResult>(Func<IQueryable<TEntity>, IQueryable<TResult>> filter, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,20 @@ public override async Task<int> SaveChangesAsync(CancellationToken cancellationT

protected virtual IQueryable<TResult> Set<TResult>(Func<IQueryable<TEntity>, IQueryable<TResult>> filter)
{
return filter(Db.Set<TEntity>());
return filter(Set());
}

protected virtual IQueryable<TEntity> Set(IFilterSet<TEntity>? filter = null)
protected virtual IQueryable<TEntity> Set()
{
var result = Db.Set<TEntity>() as IQueryable<TEntity>;
if (filter is not null) result = result.Apply(filter);

return result;
return Db.Set<TEntity>();
}

public virtual async Task<IEnumerable<TEntity>> GetAllAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default)
public virtual async Task<IEnumerable<TEntity>> GetAllAsync(CancellationToken cancellationToken = default)
{
using var saveActivity = Activity.Current?.Source
.StartActivity($"{RepositoryName}: GetAll");

return await Set(filter)
return await Set()
.ToListAsync(cancellationToken);
}

Expand All @@ -68,12 +65,12 @@ public virtual async Task<IEnumerable<TResult>> GetAllAsync<TResult>(Func<IQuery
.ToListAsync(cancellationToken);
}

public virtual async Task<PageResult<TEntity>> GetPageAsync(PageRequest pageRequest, IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default)
public virtual async Task<PageResult<TEntity>> GetPageAsync(PageRequest pageRequest, CancellationToken cancellationToken = default)
{
using var saveActivity = Activity.Current?.Source
.StartActivity($"{RepositoryName}: GetPage");

return await Set(filter)
return await Set()
.ToPageAsync(pageRequest, cancellationToken);
}

Expand All @@ -86,12 +83,12 @@ public virtual async Task<PageResult<TResult>> GetPageAsync<TResult>(PageRequest
.ToPageAsync(pageRequest, cancellationToken);
}

public virtual async Task<TEntity?> GetAsync(IFilterSet<TEntity> filter, CancellationToken cancellationToken = default)
public virtual async Task<TEntity?> GetAsync(CancellationToken cancellationToken = default)
{
using var saveActivity = Activity.Current?.Source
.StartActivity($"{RepositoryName}: Get");

return await Set(filter)
return await Set()
.FirstOrDefaultAsync(cancellationToken);
}

Expand All @@ -104,12 +101,12 @@ public virtual async Task<PageResult<TResult>> GetPageAsync<TResult>(PageRequest
.FirstOrDefaultAsync(cancellationToken);
}

public virtual async Task<int> CountAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default)
public virtual async Task<int> CountAsync(CancellationToken cancellationToken = default)
{
using var saveActivity = Activity.Current?.Source
.StartActivity($"{RepositoryName}: Count");

return await Set(filter)
return await Set()
.CountAsync(cancellationToken);
}

Expand All @@ -122,12 +119,12 @@ public virtual async Task<int> CountAsync<TResult>(Func<IQueryable<TEntity>, IQu
.CountAsync(cancellationToken);
}

public virtual async Task<long> LongCountAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default)
public virtual async Task<long> LongCountAsync(CancellationToken cancellationToken = default)
{
using var saveActivity = Activity.Current?.Source
.StartActivity($"{RepositoryName}: LongCount");

return await Set(filter)
return await Set()
.LongCountAsync(cancellationToken);
}

Expand All @@ -140,12 +137,12 @@ public virtual async Task<long> LongCountAsync<TResult>(Func<IQueryable<TEntity>
.LongCountAsync(cancellationToken);
}

public virtual async Task<bool> AnyAsync(IFilterSet<TEntity>? filter = null, CancellationToken cancellationToken = default)
public virtual async Task<bool> AnyAsync(CancellationToken cancellationToken = default)
{
using var saveActivity = Activity.Current?.Source
.StartActivity($"{RepositoryName}: Any");

return await Set(filter)
return await Set()
.AnyAsync(cancellationToken);
}

Expand All @@ -163,16 +160,9 @@ public class AppDbRepository<TEntity, TKey>(AppDbContext db) : AppDbRepository<T
where TEntity : class, IEntity<TKey>
where TKey : struct
{
protected override IQueryable<TEntity> Set(IFilterSet<TEntity>? filter = null)
protected override IQueryable<TEntity> Set()
{
var result = Db.Set<TEntity>() as IQueryable<TEntity>;

// Default behavior: order by Id,
result = result.OrderBy(x => x.Id);

// Default ordering may be overridden when applying the filter
if (filter is not null) result = result.Apply(filter);

return result;
// Default order: Id, ascending
return base.Set().OrderBy(x => x.Id);
}
}

0 comments on commit df98b3c

Please sign in to comment.