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

Thursday, September 18, 2008

Language Wars !!!

Wikipedia defines language as "A language is a system of visual, auditory, or tactile symbols of communication and the rules used to manipulate them" . If we look at this definition closely, this wont be a completely wrong defintion if applied to a programming language. It is not natural for humans to adapt to change and same applies to programmers, even if some of us live a life of a creature of another form. The first line of code that I even wrote was in C , hence I am most comfortable programming in languages that essentially look like C. Every one knows that C is the closest to assembly language when it comes to speed of execution but would not be a language of choice fro building todays apps for most.
In todays world where more and more cultures are interacting being multilingual is not very uncommon. Similarly, I think the time to stick one language and be faithful to it by taking a oath that "Thou shall never code a single line in any other language!" is gone. Arguement that a certain language is the best language in the world does not make sense to me. In fact the arguement sould be, considering the technical accumen of the team which language will provide us with the tool to accomplish the task at hand in the best way, minimizing conflicts and maximizing productivity. 
     I keep reading lots of rant blogs where someone says "Python kicks ass", "Ruby make programmers love their job"," New version of C# makes programmers 10 time productive than Java"..blah blah blah. Whats the point ?? Is it something that media does where it makes you believe that you need a new kind of shampoo??
   My suggestion to all programmers who love their job no matter what language you use is that keep your mind open for new ideas. Like the world moved from C to C++ be open minded about changing language, paradigms and platforms. This will only make you versatile with changing technical expectations. You will never be stuck with open/closed systems. Every language out there has a jewel somewhere, take it if you see it otherwise just accept that you dont see it.

Cheers!
X