英文:
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<Stuff, StuffDto>();
This mapping works
StuffDto stuffDto = _mapper.Map<Stuff, StuffDto>(Stuff);
And this mapping also works
List<StuffDto> stuffDtoList = _mapper.Map<List<Stuff>, List<StuffDto>>(Stuff);
However this mapping does not
PagesList<StuffDto> stuffDtoList = _mapper.Map<PagedList<Stuff>, PagesList<StuffDto>>(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<T> : List<T>
{
public PagedList(IEnumerable<T> 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<PagedList<T>> CreateAsync(IQueryable<T> 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<T>(items, count, pageNumber, pageSize);
}
}
What do I need to do to get this to work/resolve like List does?
_mapper.Map<PagedList<Stuff>, PagesList<StuffDto>>(Stuff);
答案1
得分: 2
- 如果
PagedList
是可变的(可以添加/删除项目),您可以映射到现有的PagedList
实例。可能会像这样:
PagesList<StuffDto> stuffDtoList = new PagedList<StuffDto>(Enumerable.Empty<StuffDto>(), ...);
_mapper.Map<PagedList<Stuff>, PagesList<StuffDto>>(Stuff, stuffDtoList);
- 另一种方法是映射通用的
List
,然后创建PagedList
:
List<StuffDto> stuffDtoList = _mapper.Map<List<Stuff>, List<StuffDto>>(Stuff);
PagesList<StuffDto> pagedList = new PagedList<StuffDto>(stuffDtoList, ...);
- 按照这里的描述创建自定义类型转换器,并根据需要自定义它:
CreateMap<Stuff, StuffDto>();
CreateMap<PagedList<Stuff>, PagedList<StuffDto>>().ConvertUsing<PagedListTypeConverter>();
英文:
On the first glance you have three workarounds:
- In case
PagedList
is mutable (can add/remove items) you can map to existing pagedList instance. It might look like this
PagesList<StuffDto> stuffDtoList = new PagedList<StuffDto>(Enumerable.Empty<StuffDto>(), ...);
_mapper.Map<PagedList<Stuff>, PagesList<StuffDto>>(Stuff, stuffDtoList);
- Another approach is referenced to mapping generic
List
, and then craetePagedList
List<StuffDto> stuffDtoList = _mapper.Map<List<Stuff>, List<StuffDto>>(Stuff);
PagesList<StuffDto> pagedList = new PagedList<StuffDto>(stuffDtoList, ...);
- Create custom type converter as described here. And customize it as you wish
CreateMap<Stuff, StuffDto>();
CreateMap<PagedList<Stuff>, PagedList<StuffDto>>().ConvertUsing<PagedListTypeConverter>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论