You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor分页表格实现疑问:当前方案是否冗余?如何优化?

Blazor分页表格优化方案

问题梳理

实现Blazor带分页的表格后,当前返回类型Task<ServiceResponse<List<MainProductList<List<MainProduct>>>>>层级冗余复杂,同时希望分页逻辑可复用(不与业务数据耦合),并了解如何让方法同时返回多个对象。

优化方案

1. 简化返回类型,移除冗余层级

当前用List<MainProductList<List<MainProduct>>>完全没必要,一次分页请求只需要返回一组分页信息+对应页的数据列表,直接让ServiceResponse的泛型参数为分页结果对象即可。

2. 抽离通用分页逻辑,实现解耦

创建通用的分页结果类,替代原有的MainProductList<T>,让所有需要分页的业务都能复用:

namespace SipposM2.Shared
{
    public class PagedResult<T>
    {
        public string SearchTerm { get; set; } = string.Empty;
        public List<T>? Data { get; set; }
        public int TotalPages { get; set; }
        public int CurrentPage { get; set; }
        public int TotalRecords { get; set; } // 新增总条数,便于前端展示
    }
}

3. 优化服务方法,解决多对象返回问题

方法同时返回业务数据和分页信息的最佳实践是用自定义DTO(即上述的PagedResult),比Tuple更直观、扩展性更强。同时优化查询逻辑,从内存分页改为数据库层面分页,提升性能:

// 保留原有的ServiceResponse类,用于封装API响应状态
namespace SipposM2.Shared
{
    public class ServiceResponse<T>
    {
        public T? Data { get; set; }
        public bool Success { get; set; } = true;
        public string Message { get; set; } = string.Empty;
        public int? StatusCodes { get; set; }
    }
}
// 修改后的服务方法
public async Task<ServiceResponse<PagedResult<MainProduct>>> GetMainProductListAsync(int pageNo = 1, string sTerm = "")
{
    int pageSize = 10;
    // 构建查询,暂不执行
    var query = Model2_context.MainProduct
                    .Where(p => string.IsNullOrEmpty(sTerm) || p.CategoryName.ToLower().StartsWith(sTerm.ToLower()))
                    .Select(p => new MainProduct
                    {
                        CategoryName = p.CategoryName,
                        CategoryDesc = p.CategoryDesc,
                        CategoryId = p.CategoryId,
                        EntryDate = p.EntryDate,
                        ModifyDate = p.ModifyDate,
                        SyncFlag = p.SyncFlag,
                        SyncType = p.SyncType,
                    });

    // 数据库层面获取总记录数
    int totalRecords = await query.CountAsync();
    int totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);
    // 数据库层面分页,仅查询当前页数据
    var pageData = await query.Skip((pageNo - 1) * pageSize).Take(pageSize).ToListAsync();

    var pagedResult = new PagedResult<MainProduct>
    {
        Data = pageData,
        CurrentPage = pageNo,
        TotalPages = totalPages,
        TotalRecords = totalRecords,
        SearchTerm = sTerm
    };

    return new ServiceResponse<PagedResult<MainProduct>>
    {
        Data = pagedResult
    };
}

4. 简化Razor页面逻辑

protected override async Task OnInitializedAsync()
{
    var response = await ProductService.GetMainProductListAsync();
    if (response.Success && response.Data != null)
    {
        ProductService.MainProducts = response.Data.Data;
        ProductService.CurrentPage = response.Data.CurrentPage;
        ProductService.TotalPages = response.Data.TotalPages;
        // 按需绑定其他分页相关字段
    }
}

额外说明

  • 如果非要让方法返回两个独立对象,可以使用ValueTuple,例如:Task<(List<MainProduct> Data, PaginationInfo Info)>,但DTO方式更适合长期维护,尤其是需要在API或组件间传递数据时。
  • 通用的PagedResult<T>可以直接复用在其他业务实体的分页场景中,完全与业务数据解耦。

内容的提问来源于stack exchange,提问作者Edemilorhea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 02:57:58