Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comprehensive sorting/paging/filtering service layer interface method

I'm trying to write a service layer method that will take all the necessary parameters that will allow me to get data from the repository in the way how a webforms grid would work.

The repository returns IQueryable<T>.

I came up with a model like this:

public class PagedModel<T>
{
    public GridSortOptions GridSortOptions { get; set; } //Enum for ASC and DESC
    public IList<T> Items { get; set; }
    public int Page { get; set; }
    public int PageSize { get; set; }
}

The method:

PagedModel<User> GetUsers(Expression<Func<T,bool>> predicate, int page, int pageSize, GridSortOptions sortOption);

Questions:

  1. What parameters am I missing from this method?
  2. What properties am I missing from the PagedModel?
  3. How do I implement the method?

What I want:

  1. Paging
  2. Sorting
  3. Filtering
like image 719
Shawn Mclean Avatar asked Aug 02 '26 22:08

Shawn Mclean


1 Answers

I find it nice sometimes to create a "Criteria" class that contains all the properties separate from the list of items you are searching for. This way you can use it as a model property that will automatically be bound on your search actions, pass it to service and repository methods (instead of a bunch of separate params), persist it in session if need be, have strongly typed filter properties specific to the current type you are searching for (ie User), have a supertype for holding generic paging or sorting.

Something like

public class UserCriteria
{
    public GridSortOptions GridSortOptions { get; set; } //Enum for ASC and DESC
    public int Page { get; set; }
    public int PageSize { get; set; }
    public bool? IsActive { get; set; }
    public string UserName { get; set; }
}
like image 140
JeremyWeir Avatar answered Aug 04 '26 11:08

JeremyWeir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!