tags:

views:

275

answers:

3

I'd like to map a paged list of business objects to a paged list of view model objects using something like this:

var listViewModel = _mappingEngine.Map<IPagedList<RequestForQuote>, IPagedList<RequestForQuoteViewModel>>(requestForQuotes);

The paged list implementation is similar to Rob Conery's implementation here: http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

How can you setup Automapper to do this?

A: 

AutoMapper automatically handles conversions between several types of lists and arrays: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays

It doesn't appear to automatically convert custom types of lists inherited from IList, but a work around could be:

    var pagedListOfRequestForQuote = new PagedList<RequestForQuoteViewModel>(
        AutoMapper.Mapper.Map<List<RequestForQuote>, List<RequestForQuoteViewModel>>(((List<RequestForQuote>)requestForQuotes),
        page ?? 1,
        pageSize
Bryan
+1  A: 

AutoMapper does not support this out of the box, as it doesn't know about any implementation of IPagedList<>. You do however have a couple of options:

Write a custom IObjectMapper, using the existing Array/EnumerableMappers as a guide. This is the way I would go personally.

Write a custom TypeConverter, using Mapper.CreateMap, IPagedList>().ConvertUsing() and inside use Mapper.Map to map each element of the list.

Jimmy Bogard