Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions src/KSFramework/GenericRepository/GenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ public GenericRepository(DbContext context) : base(context)
/// Gets an entity by its primary key.
/// </summary>
/// <param name="id">The entity ID.</param>
/// <param name="cancellationToken"></param>
/// <returns>The entity if found; otherwise, null.</returns>
public async ValueTask<TEntity?> GetByIdAsync(object id)
public async ValueTask<TEntity?> GetByIdAsync(object id,
CancellationToken cancellationToken = default)
{
return await DbSet.FindAsync(id);
return await DbSet.FindAsync(id, cancellationToken);
}

/// <summary>
/// Gets all entities.
/// </summary>
/// <param name="asNoTracking">Whether to disable tracking for better performance.</param>
/// <param name="cancellationToken"></param>
/// <returns>A list of all entities.</returns>
public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true)
public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true,
CancellationToken cancellationToken = default)
{
return await AsQueryable(asNoTracking).ToListAsync();
return await AsQueryable(asNoTracking).ToListAsync(cancellationToken);
}

/// <summary>
Expand All @@ -47,11 +51,17 @@ public async Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true)
/// <param name="where">Optional filter expression.</param>
/// <param name="orderBy">Property name to order by.</param>
/// <param name="desc">Order descending if true.</param>
/// <param name="cancellationToken"></param>
/// <returns>A paginated list of entities.</returns>
public async Task<PaginatedList<TEntity>> GetPagedAsync(int pageIndex, int pageSize, Expression<Func<TEntity, bool>>? where = null, string? orderBy = "", bool desc = false)
public async Task<PaginatedList<TEntity>> GetPagedAsync(int pageIndex,
int pageSize,
Expression<Func<TEntity, bool>>? where = null,
string? orderBy = "",
bool desc = false,
CancellationToken cancellationToken = default)
{
var query = ApplyWhere(AsQueryable(), where);
return await PaginatedList<TEntity>.CreateAsync(query, pageIndex, pageSize, where, orderBy, desc);
return await PaginatedList<TEntity>.CreateAsync(query, pageIndex, pageSize, where, orderBy, desc, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -84,28 +94,34 @@ public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, bool
/// Gets a single entity that matches the given predicate or null.
/// </summary>
/// <param name="predicate">The filter expression.</param>
/// <param name="cancellationToken"></param>
/// <returns>The matching entity or null.</returns>
public async Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate)
public async Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate,
CancellationToken cancellationToken = default)
{
return await DbSet.SingleOrDefaultAsync(predicate);
return await DbSet.SingleOrDefaultAsync(predicate, cancellationToken);
}

/// <summary>
/// Adds a new entity asynchronously.
/// </summary>
/// <param name="entity">The entity to add.</param>
public async Task AddAsync(TEntity entity)
/// <param name="cancellationToken"></param>
public async Task AddAsync(TEntity entity,
CancellationToken cancellationToken = default)
{
await DbSet.AddAsync(entity);
await DbSet.AddAsync(entity, cancellationToken);
}

/// <summary>
/// Adds a range of entities asynchronously.
/// </summary>
/// <param name="entities">The entities to add.</param>
public async Task AddRangeAsync(IEnumerable<TEntity> entities)
/// <param name="cancellationToken"></param>
public async Task AddRangeAsync(IEnumerable<TEntity> entities,
CancellationToken cancellationToken = default)
{
await DbSet.AddRangeAsync(entities);
await DbSet.AddRangeAsync(entities, cancellationToken);
}

/// <summary>
Expand Down Expand Up @@ -139,9 +155,11 @@ public void RemoveRange(IEnumerable<TEntity> entities)
/// Determines whether any entity exists that matches the specified predicate.
/// </summary>
/// <param name="predicate">The condition to check.</param>
/// <param name="cancellationToken"></param>
/// <returns>True if at least one entity exists; otherwise, false.</returns>
public async Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate)
public async Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate,
CancellationToken cancellationToken = default)
{
return await DbSet.AnyAsync(predicate);
return await DbSet.AnyAsync(predicate, cancellationToken);
}
}
22 changes: 15 additions & 7 deletions src/KSFramework/GenericRepository/IGenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ public interface IGenericRepository<TEntity> where TEntity : class
/// Asynchronously retrieves an entity by its unique identifier.
/// </summary>
/// <param name="id">The unique identifier of the entity.</param>
/// <param name="cancellationToken"></param>
/// <returns>A task representing the asynchronous operation, containing the entity if found; otherwise, null.</returns>
ValueTask<TEntity?> GetByIdAsync(object id);
ValueTask<TEntity?> GetByIdAsync(object id, CancellationToken cancellationToken = default);

/// <summary>
/// Asynchronously retrieves all entities.
/// </summary>
/// <param name="asNoTracking">Whether to track entities in change tracker.</param>
/// <param name="cancellationToken"></param>
/// <returns>A task containing all entities.</returns>
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true);
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true, CancellationToken cancellationToken = default);

/// <summary>
/// Asynchronously retrieves a paginated list of entities with optional filtering and ordering.
Expand All @@ -31,13 +33,15 @@ public interface IGenericRepository<TEntity> where TEntity : class
/// <param name="where">Optional filter expression.</param>
/// <param name="orderBy">Optional property name to order by.</param>
/// <param name="desc">Indicates if the order should be descending.</param>
/// <param name="cancellationToken"></param>
/// <returns>A task containing a paginated list of entities.</returns>
Task<PaginatedList<TEntity>> GetPagedAsync(
int pageIndex,
int pageSize,
Expression<Func<TEntity, bool>>? where = null,
string? orderBy = "",
bool desc = false);
bool desc = false,
CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves a paginated list of entities with optional filtering and ordering.
Expand Down Expand Up @@ -68,19 +72,21 @@ PaginatedList<TEntity> GetPaged(
/// </summary>
/// <param name="predicate">The condition to match.</param>
/// <returns>A task containing a single entity or null.</returns>
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);

/// <summary>
/// Asynchronously adds an entity to the repository.
/// </summary>
/// <param name="entity">The entity to add.</param>
Task AddAsync(TEntity entity);
/// <param name="cancellationToken"></param>
Task AddAsync(TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
/// Asynchronously adds multiple entities to the repository.
/// </summary>
/// <param name="entities">The entities to add.</param>
Task AddRangeAsync(IEnumerable<TEntity> entities);
/// <param name="cancellationToken"></param>
Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);

/// <summary>
/// Updates an existing entity in the repository.
Expand All @@ -104,6 +110,8 @@ PaginatedList<TEntity> GetPaged(
/// Asynchronously checks whether any entity matches the given predicate.
/// </summary>
/// <param name="predicate">The condition to match.</param>
/// <param name="cancellationToken"></param>
/// <returns>True if any entity matches; otherwise, false.</returns>
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate);
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate,
CancellationToken cancellationToken = default);
}
18 changes: 12 additions & 6 deletions src/KSFramework/GenericRepository/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ namespace KSFramework.GenericRepository;
/// <typeparam name="TEntity">The entity type.</typeparam>
public interface IRepository<TEntity> where TEntity : class
{
Task AddAsync(TEntity entity);
Task AddRangeAsync(IEnumerable<TEntity> entities);
Task AddAsync(TEntity entity,
CancellationToken cancellationToken = default);
Task AddRangeAsync(IEnumerable<TEntity> entities,
CancellationToken cancellationToken = default);
IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, bool asNoTracking = true);
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true);
Task<IEnumerable<TEntity>> GetAllAsync(bool asNoTracking = true,
CancellationToken cancellationToken = default);
Task<PaginatedList<TEntity>> GetPagedAsync(
int pageIndex,
int pageSize,
Expand All @@ -25,10 +28,13 @@ PaginatedList<TEntity> GetPaged(
Expression<Func<TEntity, bool>>? where = null,
string? orderBy = "",
bool desc = false);
ValueTask<TEntity?> GetByIdAsync(object id);
ValueTask<TEntity?> GetByIdAsync(object id,
CancellationToken cancellationToken = default);
void Remove(TEntity entity);
void RemoveRange(IEnumerable<TEntity> entities);
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate);
Task<TEntity?> SingleOrDefaultAsync(Expression<Func<TEntity, bool>> predicate,
CancellationToken cancellationToken = default);
Task<bool> IsExistValueForPropertyAsync(Expression<Func<TEntity, bool>> predicate,
CancellationToken cancellationToken = default);
void Update(TEntity entity);
}
14 changes: 10 additions & 4 deletions src/KSFramework/Pagination/PaginatedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ public bool HasNextPage
}
}

public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize, Expression<Func<T, bool>>? where = null,
string? orderBy = "", bool desc = false)
public static async Task<PaginatedList<T>> CreateAsync(
IQueryable<T> source,
int pageIndex,
int pageSize,
Expression<Func<T, bool>>? where = null,
string? orderBy = "",
bool desc = false,
CancellationToken cancellationToken = default)
{
if(where is null) where = x => true;

Expand All @@ -54,8 +60,8 @@ public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int
}
}

var count = await source.CountAsync(where);
var items = await source.Where(where).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
var count = await source.CountAsync(where, cancellationToken);
var items = await source.Where(where).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync(cancellationToken);
return new PaginatedList<T>(items, count, pageIndex, pageSize);
}

Expand Down
Loading