I don't understand your question TBH since AFAIK you need to register your mappings for AutoMapper
to work based on its documentation, but see if this will help you.
Use a static Mapper
public class MapperConfig
{
public static MapperConfiguration MapperCfg { get; private set; }
public static IMapper Mapper { get; private set; }
public static void RegisterMappings()
{
MapperCfg = new MapperConfiguration(cfg =>
{
cfg.AllowNullCollections = true;
cfg.AllowNullDestinationValues = true;
#region Entity VMs
cfg.CreateMap<Address, AddressVM>().MaxDepth(3).ReverseMap();
cfg.CreateMap<ApplicationUserConfig, ApplicationUserConfigVM>().MaxDepth(3).ReverseMap();
// ... You need to define the objects in the mapper config
cfg.CreateMap<WarehouseConfig, WarehouseConfigVM>().MaxDepth(3).ReverseMap();
#endregion
});
Mapper = MapperCfg.CreateMapper();
}
}
On your application startup call MapperConfig.RegisterMappings();
to register the model mappings.
Then you can create an extension
public static class Extension
{
public static IPagedList<TDestination> ToMappedPagedList<TSource, TDestination>(this IPagedList<TSource> list)
{
IMapper mapper = MapperConfig.Mapper;
IEnumerable<TDestination> sourceList = mapper.Map<IEnumerable<TDestination>>(list);
IPagedList<TDestination> pagedResult = new StaticPagedList<TDestination>(sourceList, list.GetMetaData());
return pagedResult;
}
public static IEnumerable<TDestination> Map<TSource, TDestination>(this IEnumerable<TSource> list)
where TSource : class
where TDestination : class, new()
{
IMapper mapper = MapperConfig.Mapper;
IEnumerable<TDestination> sourceList = mapper.Map<IEnumerable<TDestination>>(list);
return sourceList;
}
public static TDestination Map<TSource, TDestination>(this TSource entity)
where TSource : class
where TDestination : class, new()
{
IMapper mapper = MapperConfig.Mapper;
return mapper.Map<TDestination>(entity);
}
}
Ideally you would register your IMapper
in an IoC Container and not call MapperConfig.Mapper
directly in your Extention.cs
but this will do for the example.
Usage:
Address address = new Address();
AddressVM = address.Map<Address, AddressVM>();