Skip to content

Commit

Permalink
Merge pull request arch#126 from AIKICo/master
Browse files Browse the repository at this point in the history
GetAllAsync
  • Loading branch information
rigofunc authored Jan 3, 2020
2 parents 21af5fc + b3d7e35 commit 5bf04f9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
4 changes: 2 additions & 2 deletions samples/UnitOfWork.Host/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public ValuesController(IUnitOfWork unitOfWork, ILogger<ValuesController> logger

// GET api/values
[HttpGet]
public async Task<IPagedList<Blog>> Get()
public async Task<IList<Blog>> Get()
{
return await _unitOfWork.GetRepository<Blog>().GetPagedListAsync(include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));
return await _unitOfWork.GetRepository<Blog>().GetAllAsync(include: source => source.Include(blog => blog.Posts).ThenInclude(post => post.Comments));
}

// GET api/values/Page/5/10
Expand Down
30 changes: 29 additions & 1 deletion src/UnitOfWork/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,41 @@ Task<TEntity> GetFirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate =
/// <summary>
/// Gets all entities. This method is not recommended
/// </summary>
/// <returns>The <see cref="IQueryable{TEntity}"/>.</returns>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="orderBy">A function to order elements.</param>
/// <param name="include">A function to include navigation properties</param>
/// <param name="disableTracking"><c>true</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
/// <param name="ignoreQueryFilters">Ignore query filters</param>
/// <returns>An <see cref="IPagedList{TEntity}"/> that contains elements that satisfy the condition specified by <paramref name="predicate"/>.</returns>
/// <remarks>Ex: This method defaults to a read-only, no-tracking query.</remarks>
IQueryable<TEntity> GetAll(Expression<Func<TEntity, bool>> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
bool disableTracking = true,
bool ignoreQueryFilters = false);

/// <summary>
/// Gets all entities. This method is not recommended
/// </summary>
/// <returns>The <see cref="IQueryable{TEntity}"/>.</returns>
Task<IList<TEntity>> GetAllAsync();

/// <summary>
/// Gets all entities. This method is not recommended
/// </summary>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="orderBy">A function to order elements.</param>
/// <param name="include">A function to include navigation properties</param>
/// <param name="disableTracking"><c>true</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
/// <param name="ignoreQueryFilters">Ignore query filters</param>
/// <returns>An <see cref="IPagedList{TEntity}"/> that contains elements that satisfy the condition specified by <paramref name="predicate"/>.</returns>
/// <remarks>Ex: This method defaults to a read-only, no-tracking query.</remarks>
Task<IList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
bool disableTracking = true,
bool ignoreQueryFilters = false);

/// <summary>
/// Gets the count based on a predicate.
/// </summary>
Expand Down
70 changes: 70 additions & 0 deletions src/UnitOfWork/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,25 @@ public virtual void ChangeTable(string table)
}
}

/// <summary>
/// Gets all entities. This method is not recommended
/// </summary>
/// <returns>The <see cref="IQueryable{TEntity}"/>.</returns>
public IQueryable<TEntity> GetAll()
{
return _dbSet;
}

/// <summary>
/// Gets all entities. This method is not recommended
/// </summary>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="orderBy">A function to order elements.</param>
/// <param name="include">A function to include navigation properties</param>
/// <param name="disableTracking"><c>true</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
/// <param name="ignoreQueryFilters">Ignore query filters</param>
/// <returns>An <see cref="IPagedList{TEntity}"/> that contains elements that satisfy the condition specified by <paramref name="predicate"/>.</returns>
/// <remarks>Ex: This method defaults to a read-only, no-tracking query.</remarks>
public IQueryable<TEntity> GetAll(
Expression<Func<TEntity, bool>> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Expand Down Expand Up @@ -679,5 +693,61 @@ public virtual void Delete(object id)
/// </summary>
/// <param name="entities">The entities.</param>
public virtual void Delete(IEnumerable<TEntity> entities) => _dbSet.RemoveRange(entities);

/// <summary>
/// Gets all entities. This method is not recommended
/// </summary>
/// <returns>The <see cref="IQueryable{TEntity}"/>.</returns>
public async Task<IList<TEntity>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}

/// <summary>
/// Gets all entities. This method is not recommended
/// </summary>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="orderBy">A function to order elements.</param>
/// <param name="include">A function to include navigation properties</param>
/// <param name="disableTracking"><c>true</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
/// <param name="ignoreQueryFilters">Ignore query filters</param>
/// <returns>An <see cref="IPagedList{TEntity}"/> that contains elements that satisfy the condition specified by <paramref name="predicate"/>.</returns>
/// <remarks>Ex: This method defaults to a read-only, no-tracking query.</remarks>
public async Task<IList<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
bool disableTracking = true, bool ignoreQueryFilters = false)
{
IQueryable<TEntity> query = _dbSet;

if (disableTracking)
{
query = query.AsNoTracking();
}

if (include != null)
{
query = include(query);
}

if (predicate != null)
{
query = query.Where(predicate);
}

if (ignoreQueryFilters)
{
query = query.IgnoreQueryFilters();
}

if (orderBy != null)
{
return await orderBy(query).ToListAsync();
}
else
{
return await query.ToListAsync();
}
}
}
}

0 comments on commit 5bf04f9

Please sign in to comment.