views:

258

answers:

2

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??

A: 

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.

Otávio Décio
I can bind too, how are you sorting and paging??
Perpetualcoder
I sort and page at the SQL level, mostly because in my case I never know if the data changed or not so I made a decision to requery. Also, when you sort most of the time the data itself will be different in the current page anyways.
Otávio Décio
I also use the Sorting event to determine ASC, DESC and which column to sort by.
Otávio Décio
@ocdecio = I am doing that too but my handler needs to do things like if sortby="FirstName" {sort by Person.Firstname} are you doing that too ?
Perpetualcoder
Yes. In my model, the DAO properties match the table column name as well as the template column id on the GridView which makes it easy to add it to the sort clause. I store the ASC/DESC in a session variable though.
Otávio Décio
@ocdecio = Then you will need to do it for each class in Model am I correct?
Perpetualcoder
Not in my case. My DAL generates SQL from the POCO class, and I have a AddSort method in my search criteria class that receives the column name and direction, that's all I need. My POCO classes are as stable as the db schema for the most part.
Otávio Décio
+3  A: 

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);
         }
     }
 }
Toran Billups
Question - how does that work if you have, say, 100,000 Suppliers?
Otávio Décio
This is the only negative to this approach. With 100,000 Suppliers for example - you would need to hit SQL and return all the records, map that into a List<Supplier> and then call this 1 line of code to do the sort ... that will then run through all 100,000 objects ... not ideal for tons of data
Toran Billups
+1 for using Generics.
David Robbins