Automapper map custom collections

烂漫一生 提交于 2019-12-09 18:29:21

问题


Hello. I have a list that looks like this one:

public class PagedList<T> : List<T>
{
    public PagedList(IEnumerable<T> collection) : base(collection)
    { }
    public int TotalItems { get; set; }
    public int CurrentPage { get; set; }
    public int PageSize { get; set; }
    //some other properties
}

and used in repository for paging

 public PagedList<TEntity> GetPaged(int page)
 {
   var pagedEntities = some_query;
   return pagedEntities.AsPagedList(totalResults, page, pageSize);
 }

The same PagedList is also used in asp mvc view models for paging. Is it possible to map this collections using Automapper with all the properties TotalItems/CurrentPage/... ?

   PagedList<DbItem> dbItems = _repository.GetPages(page);
   var viewItems = new PagedList<SomeItemView>();
   Mapper.Map(dbItems , viewItems);

Tahnk You !


回答1:


This worked for me. Are you looking for something more generic?

public class DbItem
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class ViewItem
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class PagedList<T>: List<T>
{
    public int TotalItems { get; set; }
    public int CurrentPage { get; set; }
    public int PageSize { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        MapItems();
    }

    public static void MapItems()
    {
        Mapper.CreateMap<DbItem, ViewItem>();
        Mapper.CreateMap<PagedList<DbItem>, PagedList<ViewItem>>()
            .AfterMap((s, d) => Mapper.Map<List<DbItem>, List<ViewItem>>(s, d));

        var dbList = new PagedList<DbItem>
                         {
                             new DbItem {Id = 1, Name = "a"}, 
                             new DbItem {Id = 2, Name = "b"}
                         };
        dbList.TotalItems = 2;
        dbList.CurrentPage = 1;
        dbList.PageSize = 10;
        var viewList = Mapper.Map<PagedList<DbItem>, PagedList<ViewItem>>(dbList);

        Console.WriteLine(viewList.TotalItems);
        Console.WriteLine(viewList.CurrentPage);
        Console.WriteLine(viewList.PageSize);
        Console.WriteLine(viewList[0].Id + " " + viewList[0].Name);
        Console.WriteLine(viewList[1].Id + " " + viewList[1].Name);
        Console.ReadLine();
    }
}



回答2:


What you need is a custom type converter

    public class PagedListConverter<TIn, TOut> : ITypeConverter<IPagedList<TIn>, IPagedList<TOut>>
    {
        public IPagedList<TOut> Convert(AutoMapper.ResolutionContext context)
        {

            var source = (IPagedList<TIn>)context.SourceValue;

            var mapped = Mapper.Map<IList<TOut>>(source);

            return new StaticPagedList<TOut>(mapped, source.GetMetaData());
        }
    }

Usage:

    Mapper.CreateMap<IPagedList<Company>, IPagedList<CompanyViewModel>>().ConvertUsing<PagedListConverter<Company, CompanyViewModel>>();


来源:https://stackoverflow.com/questions/8228967/automapper-map-custom-collections

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!