Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created a little wrapper around AutoMapper to map <code>PagedList&lt;DomainModel&gt;</code> to <code>PagedList&lt;ViewModel&gt;</code>. </p> <pre><code>public class MappingService : IMappingService { public static Func&lt;object, Type, Type, object&gt; AutoMap = (a, b, c) =&gt; { throw new InvalidOperationException( "The Mapping function must be set on the MappingService class"); }; public PagedList&lt;TDestinationElement&gt; MapToViewModelPagedList&lt;TSourceElement, TDestinationElement&gt;(PagedList&lt;TSourceElement&gt; model) { var mappedList = MapPagedListElements&lt;TSourceElement, TDestinationElement&gt;(model); var index = model.PagerInfo.PageIndex; var pageSize = model.PagerInfo.PageSize; var totalCount = model.PagerInfo.TotalCount; return new PagedList&lt;TDestinationElement&gt;(mappedList, index, pageSize, totalCount); } public object Map&lt;TSource, TDestination&gt;(TSource model) { return AutoMap(model, typeof(TSource), typeof(TDestination)); } public object Map(object source, Type sourceType, Type destinationType) { if (source is IPagedList) { throw new NotSupportedException( "Parameter source of type IPagedList is not supported. Please use MapToViewModelPagedList instead"); } if (source is IEnumerable) { IEnumerable&lt;object&gt; input = ((IEnumerable)source).OfType&lt;object&gt;(); Array a = Array.CreateInstance(destinationType.GetElementType(), input.Count()); int index = 0; foreach (object data in input) { a.SetValue(AutoMap(data, data.GetType(), destinationType.GetElementType()), index); index++; } return a; } return AutoMap(source, sourceType, destinationType); } private static IEnumerable&lt;TDestinationElement&gt; MapPagedListElements&lt;TSourceElement, TDestinationElement&gt;(IEnumerable&lt;TSourceElement&gt; model) { return model.Select(element =&gt; AutoMap(element, typeof(TSourceElement), typeof(TDestinationElement))).OfType&lt;TDestinationElement&gt;(); } } </code></pre> <p>Usage:</p> <pre><code>PagedList&lt;Article&gt; pagedlist = repository.GetPagedList(page, pageSize); mappingService.MapToViewModelPagedList&lt;Article, ArticleViewModel&gt;(pagedList); </code></pre> <p>It is important that you would have to use the <strong>element</strong> types!</p> <p>If you have any question or suggestions, please feel free to comment :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload