admin管理员组文章数量:1318973
I'm trying to create a generic repository in .NET that can handle dynamic pagination, filtering, sorting, column selection, and related entity includes. The goal is to make the repository flexible enough for different use cases without having to manually write queries for each scenario.
Is there a package or pattern that can help with this in .NET? Or should I be using a custom approach like dynamic LINQ or Specification Pattern? Any best practices or examples would be greatly appreciated!
This is my current approach. How should I modify it?
public abstract class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly EqpDbContext _dbContext;
private readonly DbSet<T> _dbSet;
public GenericRepository(EqpDbContext dbContext)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<T>();
}
public virtual async Task<List<T>> GetAllAsync(bool includeDeleted = false, CancellationToken cancellationToken = default)
{
var query = _dbSet.AsNoTracking();
if (PropertyExists("IsDeleted") && !includeDeleted)
{
query = query.Where(e => EF.Property<bool>(e, "IsDeleted") == false);
}
return await query.ToListAsync(cancellationToken);
}
public virtual async Task<T?> GetByIdAsync(int id, bool includeDeleted = false, CancellationToken cancellationToken = default)
{
var query = _dbSet.AsQueryable().AsNoTracking();
if (PropertyExists("IsDeleted") && !includeDeleted)
{
query = query.Where(e => EF.Property<bool>(e, "IsDeleted") == false);
}
return await query.FirstOrDefaultAsync(e => EF.Property<int>(e, "Id") == id, cancellationToken: cancellationToken);
}
本文标签:
版权声明:本文标题:entity framework core - Is there a .NET package or approach to handle dynamic pagination, filtering, sorting, column selection, 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739182131a2143541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论