我想在我的 Web API 中实现分页,但据我所知,大多数分页结果都包含指向自身、下一个、上一个、最后一个、第一个的 URL 或链接。
我不确定将分页逻辑放在哪里,因为服务层无法生成 URL。而且我不想将我的服务层与 ASP NET Core 耦合。
我怎样才能做到这一点?我想要带有分页的简单 CRUD。
Controller 是否应该使用 URL 等生成我的“分页”模型?服务只会返回 IQueryable
?
例如,Github API 在“链接”标题中返回页面:https://api.github.com/search/code?q=addClass+user:mozilla&per_page=2
最佳答案
url 生成越接近 Mvc/WebApi 的东西(在 Controller 、过滤器或任何你想要的机制中)就应该包括在内,你不应该把你的 url 生成放在服务层中,除非你有定义的业务规则网址生成。
尝试从服务层生成 url 将强制包含 HttpContext 和 Mvc 引用,最好避免。
服务应该知道业务数据而不是 ui 层组件。
将其视为尝试为表或 View 重用相同的服务,那么您就不需要 url 生成的东西了。您应该返回数据、偏移量、限制、排序依据和总计数(如果需要),因为它需要查询该数据,而不是 url 信息。
我通常在服务或应用层内使用 Entity Framework 和 crud 操作这样的东西,它封装分页简化计数和跳过,采取行动
/// <summary>
/// Paged queryable
/// </summary>
/// <typeparam name="T">T</typeparam>
public sealed class PagedQueryable<T> : IPagedEnumerable<T> ,IEnumerable<T>
{
IQueryable<T> _source = null;
int? _totalCount = null;
/// <summary>
/// Ctor
/// </summary>
/// <param name="source">source</param>
/// <param name="offset">start element</param>
/// <param name="limit">max number of items to retrieve</param>
public PagedQueryable(IQueryable<T> source, int offset, int? limit)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
_source = source;
Limit = limit;
Offset = Math.Max(offset, 0);
}
public int TotalCount
{
get
{
if (!_totalCount.HasValue && _source != null)
_totalCount = _source.Count();
return _totalCount.GetValueOrDefault();
}
}
public int? Limit { get; }
public int Offset { get; }
public IEnumerator<T> GetEnumerator()
{
if (_source is IOrderedQueryable<T>)
{
var query = _source.Skip(Offset);
if (Limit.GetValueOrDefault() > 0)
query = query.Take(Limit.GetValueOrDefault());
return query.ToList().GetEnumerator();
}
else
return Enumerable.Empty<T>().GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
关于c# - 服务层分页,分页结果(逻辑放哪?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49896072/