Searches and Pagination
An extremely common use case in applications is the need to search for entities and display them in a paginated list. Greyhound provides a SearchResult<T> class and a Pagination class to support this use case.
SearchResult
The SearchResult<T> class is a generic class that contains a collection of entities of type T, as well as pagination information. It is designed to be used as the return type for search requests.
The recommended way to construct a SearchResult<T> is to use the constructor with three arguments: the collection of entities, the requested pagination, and the total count of entities. The pagination information should be a Pagination object, typically passed in from the request.
public async Task<Response<SearchResult<Employee>>> Handle(SearchEmployeesRequest request, CancellationToken cancellationToken){ // Count the total number of employees matching the criteria int totalCount = await CountEmployees(request, cancellationToken);
// Handle Pagination.Zero if (request.Pagination.PageSize == 0) { return new SearchResult<Employee>([], request.Pagination, totalCount); }
// Perform the search List<Employee> employees = await SearchForEmployees(request, cancellationToken);
// Construct the search result return new SearchResult<Employee>(employees, request.Pagination, totalCount);}Pagination
The Pagination type represents the pagination information for a search request. At minimum, it includes the page number and size. The Pagination type is designed to be passed in from the request and used to limit the number of entities returned. A Pagination object can also be constructed with a total count of entities to support pagination controls in the UI.