Question

I am not a big fan of datasets so I use POCO to return data. I have achieved paging and sorting using custom methods that I create for a POCO type that work on page size and give me a set of the complete POCO collection at a time, I use methods that check for name of the DataItem clicked and sort order to do that sort. Creating such methods over and over for every POCO that you plan to use with an ASP.net data control like Gridview is pretty painful.

Is there a technique to automate this so that I do not need to make such methods every time for a new POCO so that it works as if you were using a DataTable? I can provide some more explanation if required.

NOTE: Some people may call POCO as DTOs .

EDIT : I found this article on this topic. Is this the only possible way to get to what i am trying to do??

Was it helpful?

Solution

I agree with the base class idea as this will save all the duplicate code. One thing I did that takes a step in this direction is to create a class to handle the sorting of any generic list (for DTO/POCO). This allowed me to sort a list in my presenter or code-behind with only 1 line of code.

Typically for SortExpression I return the property name of the DTO you want to sort on. In addition, the SortDirection would be a simple "Ascending" Or "Decending"

List<Supplier> SupplierList = mSupplierService.GetSuppliers();
SupplierList.Sort(new GenericComparer<Supplier>(mView.SortExpression, mView.SortDirection));
mView.Suppliers = SupplierList;

Here is the class I used

public class GenericComparer<T> : IComparer<T>
 {

     private string mDirection;
     private string mExpression;

     public GenericComparer(string Expression, string Direction)
     {
         mExpression = Expression;
         mDirection = Direction;
     }

     public int Compare(T x, T y)
     {
         PropertyInfo propertyInfo = typeof(T).GetProperty(mExpression);
         IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
         IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);
         if (mDirection == "Ascending") {
             return obj1.CompareTo(obj2);
         }
         else {
             return obj2.CompareTo(obj1);
         }
     }
 }

OTHER TIPS

I created an Entity base class. My DAOs derive from it and have properties corresponding to the table columns (for the most part). My DAL returns List for any query and that is bindable to a GridView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top