Reusing my PagedList object on WCF
- by AlexCode
The problem:
I have a custom collection PagedList<T> that is being returned from my WCF service as PagedListOfEntitySearchResultW_SH0Zpu5 when T is EntitySearchResult object.
I want to reuse this PagedList<T> type between the application and the service.
My scenario:
I've created a PagedList<T> type that inherits from List<T>.
This type is on a separated assembly that is referenced on both application and WCF service.
I'm using the /reference option on the scvutil to enable the type reusing. I also don't want any arrays returned so I also use the /collection to map to the generic List type.
I'm using the following svcutil command to generate the service proxy:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\svcutil.exe"
/collectionType:System.Collections.Generic.List`1
/reference:..\..\bin\Debug\App.Utilities.dll
http://localhost/App.MyService/MyService.svc?wsdl
/namespace:*,"App.ServiceReferences.MyService"
/out:..\ServiceProxy\MyService.cs
The PagedList object is something like:
[CollectionDataContract]
public partial class PagedList<T> : List<T>
{
public PagedList() { }
/// <summary>
/// Creates a new instance of the PagedList object and doesn't apply any pagination algorithm.
/// The only calculated property is the TotalPages, everything else needed must be passed to the object.
/// </summary>
/// <param name="source"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
public PagedList(IEnumerable<T> source, int pageNumber, int pageSize, int totalRecords)
{
if (source == null)
source = new List<T>();
this.AddRange(source);
PagingInfo.PageNumber = pageNumber;
PageSize = pageSize;
TotalRecords = totalRecords;
}
public PagedList(IEnumerable<T> source, PagingInfo paging)
{
this.AddRange(source);
this._pagingInfo = paging;
}
[DataMember]
public int TotalRecords { get; set; }
[DataMember]
public int PageSize { get; set; }
public int TotalPages()
{
if (this.TotalRecords > 0 && PageSize > 0)
return (int)Math.Ceiling((double)TotalRecords / (double)PageSize);
else
return 0;
}
public bool? HasPreviousPage()
{
return (PagingInfo.PageNumber > 1);
}
public bool? HasNextPage() { return (PagingInfo.PageNumber < TotalPages()); }
public bool? IsFirstPage() { return PagingInfo.PageNumber == 1; }
public bool? IsLastPage() { return PagingInfo.PageNumber == TotalPages(); }
PagingInfo _pagingInfo = null;
[DataMember]
public PagingInfo PagingInfo
{
get {
if (_pagingInfo == null)
_pagingInfo = new PagingInfo();
return _pagingInfo;
}
set
{
_pagingInfo = value;
}
}
}