Wednesday, October 8, 2008

The Annoying ObjectDatasource

Displaying database records like tables in web pages is an extremely common scenario. If you have chosen ASP.net to do this task life can be heaven or hell depending on the data source you use. .Net provides the following types of DataSource objects-
  1. SqlDataSource - Can be used with SQL Server database as the back end, takes care of DIRT/CRUD operations using T-SQL embedded in its properties.
  2. XmlDataSource - Can be used with XML as the backend.
  3. LinqDataSource - A newcomer, can be used when displaying objects implementing the IQueryable interface
  4. AccessDataSource - As the name suggests, use it when you are using Access as a back end.
  5. ObjectDataSource - Can be used when the data is stored as custom objects or POCO (Plain old CLR object)
I am a big fan of using POCO as the containers to hold data between layers. It is not a good idea sending SQL scripts as arguements or commands with the danger of sql injection etc. If you are like me, ObjectDataSource is your friend. Like other datasources it binds to a Data Control like GridView or FormView seamlessly without any code. But if you are displaying data on a web page, we would obviously want to have the usual features; sorting,paging, filtering, inplace CRUD etc. If you thought doing this is simply setting some properties..you are in for a surprise.

Since Microsoft .net framework 1.0, anyone who binds an object datasource with a datacontrol you would get an error when you try to sort from the data control
The data source 'GridSource' does not support sorting with IEnumerable data. Automatic sorting is only supported with DataView, DataTable, and DataSet
That meant this error has not been sorted out since .net 1.0. My reaction was, REEEAAAALLLY??WTF...now what will I do with my 100's of entity/DTO classes. Do I have to use Typed Datasets?? I am dealing with millions of rows in tables, is DataSets/ DataTables good, considering all the diffgram stuff etc??

I stumbled upon a few blogs/articles -
http://devtoolshed.blogspot.com/2008/03/gridview-objectdatasource-linq-paging.html
http://borrell.parivedasolutions.com/2008/01/objectdatasource-linq-paging-sorting.html

And I am sure if you look hard enough you will find more. If you follow these articles you will be able to perform paging, sorting and selections. If you get more greedy and try to add deleteion, updation and insertion in the datacontrol...9/10 times you will see

ObjectDataSource 'RolesDataSource' could not find a non-generic method 'Update' that has parameters: RoleId, RoleName, Description, original_ApplicationId, original_LoweredRoleName.

Again I went WTF?? What do I need do now?? I went to Mr. Scott Guthrie's blog and he suggested to use DataKeyNames but guess what..it doesnt work. Other blogs have suggested different metods to solve this problem, most of them are mere hacks. Some examples are
http://weblogs.asp.net/bradygaster/archive/2006/09/26/How-to-Bloody-Your-Forehead.aspx

most solutions are SQL centric and are useless if your data methods are like Add(Customer entity) or Remove(Customer entity). I am extremely tired of playing with DataControls and ObjectDatasource. I want to wait for new Gridview and new ObjectDatasource unless there is someone smart enough out there to give me a complete example that has a master/detail, CRUD, searching, filtering, sorting, paging without embedding SQL in the control and not using DataSets/DataTables. Comeon Microsoft; it shouldnt be so hard it is such a mundane task for programmers to implement these features. Is your answer Dynamic Data???

Cheers
X

No comments: