c# - 将实体分页类映射到 dto 分页类

标签 c# automapper

我有以下PaginatedList类。

public class PaginatedList<T> : List<T>
{
    public int PageIndex { get; }
    public int TotalRecords { get; }
    public int TotalPages { get; }

    public PaginatedList(IEnumerable<T> items, int totalRecords, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        TotalRecords = totalRecords;
        TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);

        AddRange(items);
    }

    public bool HasPreviousPage => PageIndex > 1;

    public bool HasNextPage => PageIndex < TotalPages;

    public static async Task<PaginatedList<T>> CreateAsync(
        IQueryable<T> source, int pageIndex, int pageSize)
    {
        var count = await source.CountAsync();
        var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
        return new PaginatedList<T>(items, count, pageIndex, pageSize);
    }
}

我使用此类来获取使用 EF 检索的实体列表的分页信息。

这就是我使用此类返回具有分页信息的用户列表的方式。

var users = await PaginatedList<User>.CreateAsync(userQuery, pageIndex, pageSize);

以上调用将返回PaginatedList<User>对象。

如果我有该实体的 DTO 类,我们将其称为 UserDto 。如何使用automapper转换PaginatedList<User>进入PaginatedList<UserDto>所以结果将包含所有 userDto 对象以及分页信息?

否则,是否有另一种/更好的方法来实现类似的目标?

最佳答案

我最终在 PaginatedList 中创建了另一个工厂方法来处理转换。

public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
    IQueryable<TSource> source, int pageIndex, int pageSize)
{
    Ensure.It.IsGreaterThan(0, pageIndex);
    Ensure.It.IsGreaterThan(0, pageSize);

    var count = await source.CountAsync();
    var items = await source.Skip((pageIndex - 1) * pageSize)
        .Take(pageSize)
        .Select(ufc => Mapper.Map<TResult>(ufc))
        .ToListAsync();
    return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
}

然后将现有的CreateAsync调用这个新方法 CreateAsync<TSource>方法。

public static async Task<PaginatedList<TResult>> CreateAsync(
    IQueryable<TResult> source, int pageIndex, int pageSize)
{
    return await CreateAsync<TResult>(source, pageIndex, pageSize);
}

有了这个,如果我们想继续返回相同的实体类,我们可以这样使用它

await PaginatedList<User>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)

如果我们想将实体类转换为返回Dto类或者其他类,可以这样使用

await PaginatedList<UserDto>.CreateAsync(_dbContext.User.AsQueryable(), pageIndex, pageSize)

如果我们不将实体转换为其他类,则不需要在自动映射器配置中指定任何内容。但如果你想将实体映射到其他类,那么需要在自动映射器中进行配置。

或多或少,PaginatedList 类如下所示

public class PaginatedList<TResult> : List<TResult>
{
    public int PageIndex { get; }
    public int TotalRecords { get; }
    public int TotalPages { get; }

    public PaginatedList(IEnumerable<TResult> items, int count, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        TotalRecords = count;
        TotalPages = (int)Math.Ceiling(TotalRecords / (double)pageSize);

        AddRange(items);
    }

    public bool HasPreviousPage => PageIndex > 1;

    public bool HasNextPage => PageIndex < TotalPages;

    public static async Task<PaginatedList<TResult>> CreateAsync<TSource>(
        IQueryable<TSource> source, int pageIndex, int pageSize)
    {
        var count = await source.CountAsync();
        var items = await source.Skip((pageIndex - 1) * pageSize)
            .Take(pageSize)
            .Select(ufc => Mapper.Map<TResult>(ufc))
            .ToListAsync();
        return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
    }

    public static async Task<PaginatedList<TResult>> CreateAsync(
        IQueryable<TResult> source, int pageIndex, int pageSize)
    {
        return await CreateAsync<TResult>(source, pageIndex, pageSize);
    }
}

关于c# - 将实体分页类映射到 dto 分页类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54015297/

相关文章:

c# - 带有 SqlParameters 的 SqlDataReader

c# - WCF 在没有 "set "的属性上阻塞。任何解决方法?

c# - LINQ 中按特定时间范围分组

c# - MVC Controller 的操作中的异步/等待

c# - 需要加速automapper ...做113个对象需要32秒

c# - AutoMapper 和 EF 实体 - 忽略所有关系

c# - Automapper 条件语言映射

asp.net-mvc-3 - 如何调试AutoMapper "Missing type map configuration or unsupported mapping"错误

c# - Vista 从安装程序安排任务

c# - 如何在类库项目中配置自动映射器?