我有一个PagedList实现,想要使用AutoMapper将实体PagedList映射到DTO PagedList。以下是我的接口:
public interface IPagedList<T> : IList<T>
{
PagingInformation Paging { get; set; }
}
这是我的类实现:
public class PagedList<T> : List<T>, IPagedList<T> //, IList<T>
{
public PagingInformation Paging { get; set; }
public PagedList()
{
}
public PagedList(IEnumerable<T> collection) : base(collection)
{
}
public PagedList(IEnumerable<T> collection, PagingInformation paging) : base(collection)
{
Paging = paging;
}
public PagedList(int capacity) : base(capacity)
{
}
PagingInformation IPagedList<T>.Paging
{
get => Paging;
set => Paging = value;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
我正在使用Automapper,类似于:
public async Task<DomainResult<IPagedList<PositionDto>>> GetPagedListAsync(int pageIndex = 0, int pageSize = 20)
{
return DomainResult<IPagedList<PositionDto>>.Success(_mapper.Map<IPagedList<PositionDto>>(await _positionRepository.GetPagedListAsync(pageIndex, pageSize)));
}
没有Mapper配置: 我得到以下错误:
映射类型错误。 映射类型:PagedList <1-> IPagedList 1 WestCore.Shared.Collections.Pagination.PagedList <1 [[WestCore.Domain.Entities.Position, WestCore.Domain,Version = 1.0.0.0,Culture = neutral, PublicKeyToken = null]]-> WestCore.Shared.Collections.Pagination.IPagedList 1 [[WestCore.AppCore.Models.PositionDto, WestCore.AppCore,Version = 1.0.0.0,Culture = neutral, PublicKeyToken = null]]
当我将
CreateMap(typeof(PagedList << >>),typeof(IPagedList << >>))添加到Mapper Pofile中时,我得到以下错误:
程序集'AutoMapper.Proxies,Version = 0.0.0.0,Culture = neutral,
PublicKeyToken = be96cd2c38ef1005'中类型“Proxy WestCore.Shared.Collections.Pagination.IPagedList`1[[WestCore.AppCore.Models.PositionDto_WestCore.AppCore_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null]]_WestCore.Shared_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null”的方法“get_Item”没有实现。
当我将 CreateMap(typeof(PagedList << >>),typeof(IPagedList << >>)).As(typeof(PagedList << >>))添加到Mapper Profile中时,我不会出错,但是PagedList返回空结果集
我不确定是否在 PagedList 方法中缺少实现,或者它是一个配置问题。
编辑:
下面添加了分页信息:
public class PagingInformation
{
/// <summary>
/// Gets the index start value.
/// </summary>
/// <value>The index start value.</value>
public int IndexFrom { get; }
/// <summary>
/// Gets the page index (current).
/// </summary>
public int PageIndex { get; }
/// <summary>
/// Gets the page size.
/// </summary>
public int PageSize { get; }
/// <summary>
/// Gets the total count of the list of type <typeparamref name="TEntity"/>
/// </summary>
public int TotalCount { get; }
/// <summary>
/// Gets the total pages.
/// </summary>
public int TotalPages { get; }
/// <summary>
/// Gets the has previous page.
/// </summary>
/// <value>The has previous page.</value>
public bool HasPreviousPage => PageIndex - IndexFrom > 0;
/// <summary>
/// Gets the has next page.
/// </summary>
/// <value>The has next page.</value>
public bool HasNextPage => PageIndex - IndexFrom + 1 < TotalPages;
public PagingInformation(int pageIndex, int pageSize, int indexFrom, int count)
{
if (indexFrom > pageIndex)
{
throw new ArgumentException($"indexFrom: {indexFrom} > pageIndex: {pageIndex}, must indexFrom <= pageIndex");
}
PageIndex = pageIndex;
PageSize = pageSize;
IndexFrom = indexFrom;
TotalCount = count;
TotalPages = (int) Math.Ceiling(TotalCount / (double) PageSize);
}
}
谢谢你。
感谢您。