. Onun paketi IEnumerable uygulayan bir IPagedList döndürür, ancak IsLastPage, PageNumber, PageCount, vb. Gibi özel özellikler de içerir.AutoMapper Özel Tür Dönüştürücüsü çalışmıyor Web0pi'de sayfalama bilgisi vermek için <a href="https://github.com/TroyGoode/PagedList">Troy Goode's PagedList</a> kullanıyorum
Bu sınıfı WebApi denetleyici yönteminden (GET gibi) döndürmeye çalıştığınızda, Numaralandırılır serileştirilir ancak özel özellikler değil. Ben ayrı bir mülkte Enumerable hareket olduğundan, seri hale mükemmel şekilde işleyen
public class PagedViewModel<T>
{
public int FirstItemOnPage { get; set; }
public bool HasNextPage { get; set; }
public bool HasPreviousPage { get; set; }
public bool IsFirstPage { get; set; }
public bool IsLastPage { get; set; }
public int LastItemOnPage { get; set; }
public int PageCount { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalItemCount { get; set; }
public IEnumerable<T> rows { get; set; }
}
: Yani, ben bunun gibi bir sınıfa dönüştürmek için AutoMapper kullanmak ve özel bir tip dönüştürücü yazmayı düşündüm. Böyle diyoruz çalıştığınızda,
Mapper.CreateMap<IPagedList<Department>, PagedViewModel<Department>>().ConvertUsing(new PagedListTypeConverter<Department>());
Ama: Yani, oturdum ve bu gibi özel bir tür dönüştürücü yazdı:
public class PagedListTypeConverter<T> : ITypeConverter<IPagedList<T>, PagedViewModel<T>>
{
public PagedViewModel<T> Convert(ResolutionContext context)
{
var source = (IPagedList<T>)context.SourceValue;
return new PagedViewModel<T>()
{
FirstItemOnPage = source.FirstItemOnPage,
HasNextPage = source.HasNextPage,
HasPreviousPage = source.HasPreviousPage,
IsFirstPage = source.IsFirstPage,
IsLastPage = source.IsLastPage,
LastItemOnPage = source.LastItemOnPage,
PageCount = source.PageCount,
PageNumber = source.PageNumber,
PageSize = source.PageSize,
TotalItemCount = source.TotalItemCount,
rows = source
};
}
}
Sonra böyle benim yapılandırmasında kurmak:
:Missing type map configuration or unsupported mapping.
Mapping types: IPagedList
1 -> PagedViewModel
1 PagedList.IPagedList1[[Provision.DomainObjects.Department, Provision.DomainObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> Provision.DomainObjects.PagedViewModel
1[[Provision.DomainObjects.Department, Provision.DomainObjects, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]Destination path: PagedViewModel`1
Source value: PagedList.StaticPagedList`1[Provision.DomainObjects.Department]
var x = Mapper.Map<IPagedList<Department>, PagedViewModel<Department>>(departments);
bu hatayı alıyorum
Bu işi nasıl yapabilirim?
Güzel iş Brian. Bana sahip olduğunuz iş parçacığı sorunu hakkında biraz daha bilgi verir misiniz? Kodu kullanıyorum ve ortaya çıkabilecek sorunları giderebildiğimden emin olmak istiyorum. – maguy
Keşke hatırlayabilseydim, ama bir yıl sonra hiçbir fikrim yok. Bununla birlikte, hatırladığım, yaptığım gerçekten aptalca bir şey olduğunu hatırlıyorum, bu yüzden onunla karşılaşacağınızdan şüpheliyim. –