Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It sounds like you want to have some shared functionality between all of the filters. The way you would want to implement this is not necessarily through an interface, but rather through an abstract base class. Additionally, because you will be filtering on different objects, it would make sense to use generics. You could have something like the following:</p> <pre><code>public class FilterBase&lt;T&gt; { protected int startRow; ... public FilterBase(Func&lt;T, IComparable&gt; sortBy, bool sortAscending, int startRow, int numRow) { //assigns to member variables } public IEnumerable&lt;T&gt; Filter(IEnumerable&lt;T&gt; toFilter) { filtered = DoFiltering(toFilter); filtered = DoPaging(filtered); filtered = DoSorting(); return filtered; } protected abstract IEnumerable&lt;T&gt; DoFiltering(IEnumerable&lt;T&gt; toFilter); protected virtual IEnumerable&lt;T&gt; DoPaging(IEnumerable&lt;T&gt; toFilter) { return toFilter.Skip(startRow).Take(numRow); } protected virtual IEnumerable&lt;T&gt; DoSorting(IEnumerable&lt;T&gt; toFilter) { return sortAscending ? toFilter.OrderBy(sortBy) : toFilter.OrderByDescending(sortBy); } } public class BookFilter : FilterBase&lt;Book&gt; { public BookFilter(string id, string year, string sortBy, string sorDir, int startRow, int numRow) : base(sortBy, sorDir, startRow, numRow) { //assign id and year to member variables } protected override IEnumerable&lt;Book&gt; DoFiltering(IEnumerable&lt;Book&gt; toFilter) { return toFilter.Where(b =&gt; b.Id == id &amp;&amp; b.Year == year); } } </code></pre> <p>This will allow you to define the paging and sorting logic once for all of the types of filter, and have each of the types define their own custom filtering based on their private members and the type of object they are referring to. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload