简体   繁体   中英

List of Generics for DTO

I'm building a WebAPI projet in.Net Core 5. The output is always paginated results for all the endpoints.

Therefore I've created a DTO for the paginated output like the following:

public class PagedCommodityListDto
{
    /// <summary>
    /// The total number of items
    /// </summary>
    public int TotalItems { get; set; }
    /// <summary>
    /// The current page
    /// </summary>
    public int Page { get; set; }
    /// <summary>
    /// The list of paged items
    /// </summary>
    public List<CommodityDto> Items { get; set; }
}

As you can see it contains a list of the real objects returned by the endpoint, in the above example it is CommodityDto

The Dto is used and filled in the following way:

public static BusinessLogic.Dto.PagedCommodityListDto GetCommmodities(int page, ...)
{
    //Query that extracts the values from the data layer
    IQueryable<BusinessLogic.Dto.CommodityDto> commodities = MyQuery;
    
    //Logic for the paging...
    int totalItems = commodities.Count();
    skip = (page - 1) * pageSize;
    commodities = commodities.Skip(skip).Take(pageSize);

    BusinessLogic.Dto.PagedCommodityListDto pcl = new BusinessLogic.Dto.PagedEconomicDataDto();
    pcl.Items = commodities.ToList();
    pcl.TotalItems = totalItems;
    pcl.Page = page;
    
    return pcl;
}

Now, I have more or less 30 endpoints, that I add in the Items property of the paged DTO, each with a different DTO for the entities as the properties are different for each of them, therefore 30 DTOs.

My question is: should I have to create 30 additional DTOs for the "Paged Containers" or there is a way to make the public List Items { get; set; } generic?

Please note that the API must have a valid Swagger with the returned object definition for each method in the controller, like the following:

[HttpGet]
[ApiVersion("1.0")]
[Route("List")]
[ProducesResponseType(typeof(BusinessLogic.Dto.PagedCommodityListDto), 200)]
public async Task<ActionResult> MethodName(....)
{
    return Ok(Models.Repository.Commodity.GetCommmodities(page, ....));
}

You can do like this

public class PaginationViewModel
    {
        public int PageNumber { get; set; }
        public int PageSize { get; set; }
        public int? TotalRecords { get; set; }
        public int? TotalPages => TotalRecords.HasValue ? (int)Math.Ceiling(TotalRecords.Value / (double)PageSize) : (int?)null;
                
    }


    public class PagedResult<T>
    {
        public PagedResult(IEnumerable<T> results, int pageNumber, int pageSize, int? totalRecords)
        {
            Results = new List<T>(results);
            PagingInfo = new PaginationViewModel
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                TotalRecords = totalRecords,
            };
        }
        public List<T> Results { get; private set; }
        public PaginationViewModel PagingInfo { get; set; }
    }

and you use like this

var pagedResult = new PagedResult<your model>(your model, pageNumber, 20, totalRows);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM