shkipper
shkipper

Reputation: 1413

Automapper map custom collections

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 !

Upvotes: 9

Views: 5627

Answers (3)

momvart
momvart

Reputation: 1999

For those who have faced the similar problem recently, and as an update to NoPyGod's answer, you can achieve the general mapping using ITypeConverter. According to the official documentation:

AutoMapper also supports open generic type converters with any number of generic arguments:

var configuration = new MapperConfiguration(cfg =>
  cfg.CreateMap(typeof(Source<>), typeof(Destination<>)).ConvertUsing(typeof(Converter<,>)));

The closed type from Source will be the first generic argument, and the closed type of Destination will be the second argument to close Converter<,>.

So the custom type converter would be:

private class Converter<TSource, TDestination>
    : ITypeConverter<PagedList<TSource>, PagedList<TDestination>>
{
    public PagedList<TDestination> Convert(
        PagedList<TSource> source,
        PagedList<TDestination> destination,
        ResolutionContext context) =>
        new PagedList<TDestination>(
            context.Mapper.Map<List<TSource>, List<TDestination>>(source));
        /* Additional settings comes here. */
}

And then register it:

this.CreateMap(typeof(PagedList<>), typeof(PagedList<>)).ConvertUsing(typeof(Converter<,>));

Upvotes: 2

NoPyGod
NoPyGod

Reputation: 5067

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>>();

Upvotes: 3

boca
boca

Reputation: 2352

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();
    }
}

Upvotes: 7

Related Questions