Skip to content

Instantly share code, notes, and snippets.

@bzbetty
Created December 20, 2012 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzbetty/4343545 to your computer and use it in GitHub Desktop.
Save bzbetty/4343545 to your computer and use it in GitHub Desktop.
public class PaginationMapper : IObjectMapper
{
private IMappingEngineRunner _mapper;
public T Map<T>(object source)
{
TypeMap typeMap = _mapper.ConfigurationProvider.FindTypeMapFor(source, source.GetType(), typeof(T));
MappingOperationOptions mappingOperationOptions = new MappingOperationOptions();
ResolutionContext resolutionContext = new ResolutionContext(typeMap, source, source.GetType(), typeof(T), mappingOperationOptions);
return (T)_mapper.Map(resolutionContext);
}
public PagedList<T> CreatePagedList<T>(IPagination source)
{
var result = Activator.CreateInstance<PagedList<T>>();
result.TotalItems = source.TotalItems;
result.TotalPages = source.TotalPages;
result.HasNextPage = source.HasNextPage;
result.HasPreviousPage = source.HasPreviousPage;
result.PageNumber = source.PageNumber;
result.PageSize = source.PageSize;
result.FirstItem = source.FirstItem;
result.LastItem = source.LastItem;
foreach (var item in source)
{
result.Add(Map<T>(item));
}
return result;
}
public object Map(ResolutionContext context, IMappingEngineRunner mapper)
{
_mapper = mapper;
Type destinationType = context.DestinationType.GetGenericArguments()[0];
var method = typeof(PaginationMapper).GetMethod("CreatePagedList").MakeGenericMethod(destinationType);
return method.Invoke(this, new[] { context.SourceValue });
}
public bool IsMatch(ResolutionContext context)
{
return typeof(IPagination).IsAssignableFrom(context.SourceType) && typeof(IPagination).IsAssignableFrom(context.DestinationType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment