When I’m implementing a paged query, I wanted a simple object that I could return a strongly typed list of values along with a total count from my query. The business object used for search and paging also has the return of a List<T> of objects along with a TotalCount property from my Repository. You’ll see this as the return object in many of my sample paging posts.
This is basically a DTO, it doesn’t have any specific functionality.
/// <summary>
/// Yasgar Technology Group, Inc.
/// http://www.ytgi.com
/// </summary>
/// <typeparam name="TEntity"></typeparam>
[DataContract(Name = "PagedResult", Namespace = "", IsReference = true)]
public class PagedResult<TEntity> : IPagedResult<TEntity>
{
#region Constructors
/// <summary>
/// Primary constructor
/// </summary>
/// <param name="items"></param>
/// <param name="totalCount"></param>
public PagedResult(IEnumerable<TEntity> items, int totalCount)
{
m_items = items;
m_totalCount = totalCount;
}
#endregion // Constructors
#region Fields
private IEnumerable<TEntity> m_items;
private int m_totalCount = 0;
#endregion // Fields
#region Properties
/// <summary>
/// IEnumerable list of items returned
/// </summary>
[DataMember]
public IEnumerable<TEntity> Items
{
get { return m_items; }
protected set { m_items = value; }
}
/// <summary>
/// Total count of records from query
/// </summary>
[DataMember]
public int TotalCount
{
get { return m_totalCount; }
protected set { m_totalCount = value; }
}
#endregion // Properties
}
This data transfer object (DTO) is used to house the results from a paging query that has the minimum amount of information needed to return from the database. It does not need to have search criteria etc, as it is assumed that the calling method already has all that data. This will help you have a standard return object that not only has the collection of objects, but the total count that would have been returned had there not been any filters.
Here is an example of this object in use: