简体   繁体   English

DTO的Generics列表

[英]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.我正在.Net Core 5 中构建一个 WebAPI 项目。 output 始终是所有端点的分页结果。

Therefore I've created a DTO for the paginated output like the following:因此,我为分页的 output 创建了一个 DTO,如下所示:

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如您所见,它包含端点返回的真实对象的列表,在上面的示例中,它是 CommodityDto

The Dto is used and filled in the following way: Dto的使用和填写方式如下:

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.现在,我在分页 DTO 的 Items 属性中添加了或多或少的 30 个端点,每个端点都有不同的实体 DTO,因为每个实体的属性都不同,因此有 30 个 DTO。

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;我的问题是:我是否必须为“分页容器”创建30 个额外的 DTO ,或者有一种方法可以使公共列表项 { 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:请注意,API 必须具有有效的 Swagger 并返回 object 定义为 Z594C103F2C6E04C0C241AZ0 中的每个方法,如下所示:5031

[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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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