Reputation: 1739
I have been following a tutorial to add pagination to a web api. I have a class called ReviewSummaryPOCO which has my db results binded to it from Dappper as a IEnumerable and I pass this to a PaginationList which keeps information about pagination. I have a mapper class which I keep all my various mapper helpers to translate my POCOs to my DTOs.
Since my pagination class uses generics since various objects bind into it I am struggling how to make a mapper method for this and also how to even access the list inside the PageinationList class.
My pagination model:
public class PaginationList<T> : List<T>
{
public int CurrentPage { get; private set; }
public int TotalPages { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public bool HasPrevious => CurrentPage > 1;
public bool HasNext => CurrentPage < TotalPages;
public PaginationList(List<T> items, int count, int pageNumber, int pageSize)
{
TotalCount = count;
PageSize = pageSize;
CurrentPage = pageNumber;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
AddRange(items);
}
public static PaginationList<T> ToPagedList(ICollection<T> source, int pageNumber, int pageSize)
{
var count = source.Count();
var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
return new PaginationList<T>(items, count, pageNumber, pageSize);
}
}
My repository looks as follows:
public async Task<PaginationList<ReviewSummaryPOCO>> GetAllAsync(PaginationRequestDTO request)
{
using(var connection = _context.CreateConnection())
{
var offset = (request.PageNumber - 1) * request.PageSize;
string sql = @"
SELECT COUNT(0) [Count] FROM (select * from dbo.A2F_DMCP_0001_Browse()) as TMP;
select * from dbo.A2F_DMCP_0001_Browse() order by MMCID Desc OFFSET @offset ROWS FETCH NEXT @pageSize ROWS ONLY;";
var multiQuery = await connection.QueryMultipleAsync(sql, new {offset, request.PageSize});
var totalRowCount = multiQuery.Read<int>().Single();
var gridDataRows = multiQuery.Read<ReviewSummaryPOCO>().ToList();
return PaginationList<ReviewSummaryPOCO>.ToPagedList(gridDataRows, request.PageNumber, request.PageSize);
}
}
I was doing a manual mapping as shown:
public async Task<PaginationList<ReviewSummaryDTO>> GetAllAsync(PaginationRequestDTO request)
{
var paginationListPOCO = await _reviewSummaryRepository.GetAllAsync(request);
var paginationListDTO = _mapper.Map<ReviewSummaryDTO>(paginationListPOCO);
return paginationListDTO;
}
An example of a mapper method from my mapper class is as follows:
public async Task<List<DocumentDTO>> MapAsync(IEnumerable<DocumentPOCO> documentsPOCO)
{
if(documentsPOCO is null)
{
return new List<DocumentDTO>();
}
var documentsDTO = new List<DocumentDTO>();
var mapTask = documentsPOCO.Select(async document => {
documentsDTO.Add(await MapAsync(document));
});
await Task.WhenAll(mapTask);
return documentsDTO;
}
public DocumentDTO Map(DocumentPOCO documentPOCO)
{
var documentDTO = new DocumentDTO{
Mmcid = documentPOCO.MMCID,
FilenameAtUpload = documentPOCO.FILENAME_AT_UPLOAD,
DocGroupTypeDd = documentPOCO.DOC_GROUP_TYPE_DD,
WhenUploaded = documentPOCO.WHEN_UPLOADED,
DmcpBoxId = documentPOCO.DMCP_BOX_ID,
DmcpTabId = documentPOCO.DMCP_TAB_ID,
DmcpDocumentPath = documentPOCO.DMCP_DOCUMENT_PATH,
OactDocumentPath = documentPOCO.DOCUMENT_PATH,
Filename = documentPOCO.DOCUMENT_FILENAME
};
return documentDTO;
}
Upvotes: 0
Views: 43