Skip to content

Instantly share code, notes, and snippets.

@lbargaoanu
Last active September 25, 2016 12:57
Show Gist options
  • Save lbargaoanu/dd0d23ffe2268639d65e to your computer and use it in GitHub Desktop.
Save lbargaoanu/dd0d23ffe2268639d65e to your computer and use it in GitHub Desktop.
void Main()
{
var source = new PaginatedList<Source> { PageNumber = 13, PageSize = 5 };
source.AddRange(Enumerable.Range(1, 10).Reverse().Select(i=>new Source(i)));
var result = AutoMapperConfig.Mapper.Map<PaginatedList<Destination>>(source).Dump();
result.PageNumber.Dump();
result.PageSize.Dump();
}
public class PaginatedList<T> : List<T>
{
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPages { get; set; }
public string SortBy { get; set; }
public string SortDirection { get; set; }
}
public static class AutoMapperConfig
{
public static IMapper Mapper { get; private set; }
static AutoMapperConfig()
{
Mapper = new MapperConfiguration(cfg => Configure(cfg)).CreateMapper();
}
private static void Configure(IMapperConfiguration cfg)
{
cfg.AllowNullCollections = true;
cfg.CreateMap<Source, Destination>().IncludePagedResultMapping(cfg);
}
public static IMappingExpression<TSrc, TDest> IncludePagedResultMapping<TSrc, TDest>(this IMappingExpression<TSrc, TDest> expression, IMapperConfiguration cfg)
{
cfg.CreateMap<PaginatedList<TSrc>, PaginatedList<TDest>>()
.AfterMap((source, destination, context) => {
var listContext = new ResolutionContext(source, destination, typeof(List<TSrc>), typeof(List<TDest>), null, context);
((IRuntimeMapper)Mapper).Map(listContext);
});
return expression;
}
}
class Source
{
public Source(int value)
{
Value = value;
}
public int Value { get; set; }
}
class Destination
{
public int Value { get; set; }
}
@long2know
Copy link

I noticed that the latest version of Automapper appears to remove the IMapperConfiguration interface.

Would it be possible to update the sample to work with 5.1.1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment