"Doubt is uncomfortable, certainty is ridiculous." - Voiltaire

Sudeced by Elegant Databinding

This question on Stackoverflow.com came up the other day and immediately several answers started churning through my brain. The questioner was basically trying to use Linq to transform a set of nested objects into a flat collection so he could Databind the data to a GridView control. Easy right?

Well it seems easy until you read further into the question and realize that he wants to transform string buckets into a concrete type’s properties. Now this is possible by really bending C# as a language using a Dynamic Linq, using some type of reflection, or emitting your own IL but is it the practical solution?

Other C# bending functionality like Proxies used in NHibernate for lazy loading or mocking frameworks for Unit Testing solves much more complex problems for extremely valuable functionality. In this case we have an extremely complex solution to essentially throw some data into tr tags which to me does not seem worth the time or effort to make this solution viable.

Unless your an IL weaving maniac and wrote the Dynamic Linq library yourself your really marching into unfamiliar territory to make this databinding magic happen. Creating GridView columns dynamically or hand rolling your own HTML is a much more troubleshootable and simple solution.

To me the correct answer to the question wasn’t a “here’s how you do this answer” but more of a “why are you doing this” answer. I feel as though once the questioner actually thought about all the trouble he was going through just to avoid a for loop he would have realized that all the elegant code he was creating was just a huge waste of code and time.

So far is looks like the questioner abandoned his question and oddly no votes have been case for any of the other answers. I’m kinda itching to see what happens here to see which answer wins out; My pragmatic answer or answers by the language twisting enabling answers.


 
 
 

Leave a Reply