如何让Auto Mapper与继承List类内所有内容的自定义列表一起使用?

huangapple go评论94阅读模式
英文:

How can I get Auto Mapper to work with a custom list that inherits everything inside the List Class

问题

这个映射不起作用,因为 PagesList<StuffDto> 需要具有0个参数或仅可选参数的构造函数。请确保你的配置是正确的。要让这个映射像 List 一样工作,你需要为 PagesList<StuffDto> 添加一个符合这些要求的构造函数。

英文:

I have this set as a Mapping Profiles

CreateMap&lt;Stuff, StuffDto&gt;();

This mapping works

StuffDto stuffDto = _mapper.Map&lt;Stuff, StuffDto&gt;(Stuff); 

And this mapping also works

List&lt;StuffDto&gt; stuffDtoList = _mapper.Map&lt;List&lt;Stuff&gt;, List&lt;StuffDto&gt;&gt;(Stuff);

However this mapping does not

PagesList&lt;StuffDto&gt; stuffDtoList = _mapper.Map&lt;PagedList&lt;Stuff&gt;, PagesList&lt;StuffDto&gt;&gt;(Stuff);

The error is : needs to have a constructor with 0 args or only optional args. Validate your configuration for details.

The PageList looks like

public class PagedList&lt;T&gt; : List&lt;T&gt;
{
    public PagedList(IEnumerable&lt;T&gt; items, int count, int pageNumber, int pageSize)
    {
        CurrentPage = pageNumber;
        TotalPages = (int)Math.Ceiling(count / (double)pageSize);
        PageSize = pageSize;
        TotalCount = count;
        AddRange(items);
    }

    public int CurrentPage { get; set; }
    public int TotalPages { get; set; }
    public int PageSize { get; set; }
    public int TotalCount { get; set; }

    public static async Task&lt;PagedList&lt;T&gt;&gt; CreateAsync(IQueryable&lt;T&gt; source, int pageNumber,
        int pageSize)
    {
        // get the count of items EX 200 total events
        var count = await source.CountAsync();
        var items = await source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync();
        return new PagedList&lt;T&gt;(items, count, pageNumber, pageSize);
    }
}

What do I need to do to get this to work/resolve like List does?

_mapper.Map&lt;PagedList&lt;Stuff&gt;, PagesList&lt;StuffDto&gt;&gt;(Stuff);

答案1

得分: 2

  1. 如果PagedList是可变的(可以添加/删除项目),您可以映射到现有的PagedList实例。可能会像这样:
PagesList<StuffDto> stuffDtoList = new PagedList<StuffDto>(Enumerable.Empty<StuffDto>(), ...);
_mapper.Map<PagedList<Stuff>, PagesList<StuffDto>>(Stuff, stuffDtoList);
  1. 另一种方法是映射通用的List,然后创建PagedList
List<StuffDto> stuffDtoList = _mapper.Map<List<Stuff>, List<StuffDto>>(Stuff);
PagesList<StuffDto> pagedList = new PagedList<StuffDto>(stuffDtoList, ...);
  1. 按照这里的描述创建自定义类型转换器,并根据需要自定义它:
CreateMap<Stuff, StuffDto>();
CreateMap<PagedList<Stuff>, PagedList<StuffDto>>().ConvertUsing<PagedListTypeConverter>();
英文:

On the first glance you have three workarounds:

  1. In case PagedList is mutable (can add/remove items) you can map to existing pagedList instance. It might look like this
PagesList&lt;StuffDto&gt; stuffDtoList = new PagedList&lt;StuffDto&gt;(Enumerable.Empty&lt;StuffDto&gt;(), ...);
_mapper.Map&lt;PagedList&lt;Stuff&gt;, PagesList&lt;StuffDto&gt;&gt;(Stuff, stuffDtoList);
  1. Another approach is referenced to mapping generic List, and then craete PagedList
List&lt;StuffDto&gt; stuffDtoList = _mapper.Map&lt;List&lt;Stuff&gt;, List&lt;StuffDto&gt;&gt;(Stuff);
PagesList&lt;StuffDto&gt; pagedList = new PagedList&lt;StuffDto&gt;(stuffDtoList, ...);
  1. Create custom type converter as described here. And customize it as you wish
CreateMap&lt;Stuff, StuffDto&gt;();
CreateMap&lt;PagedList&lt;Stuff&gt;, PagedList&lt;StuffDto&gt;&gt;().ConvertUsing&lt;PagedListTypeConverter&gt;();

huangapple
  • 本文由 发表于 2023年5月17日 08:16:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定