Hmm.. Not every thing comes out of the box in .NET

January 10, 2008

Caution: This is an interesting thing happened in office today. I truly wanted this post to be non-techy, but couldn’t maintain it through the end. so read at your own risk! 😉

It was one fine day, when my PM came to me and asked if I could try out something since I was ‘officially’ off-work. One of the project requires to generate and display a report containing variable number of columns. The architecture of the project restricts many of the usual solutions for this. Anyway, it didn’t sound scary!

So, my first task was ‘to generate the variable-numbered-columns as result set to be returned from the stored procedure’. This turned out to be very easy. (I know this was piece of cake, so actually I lazed out and delayed to work on the problem. I wonder if my PM is reading this.. 🙂 ). Ok, the next part, moving the data from database to data layer. Here’s a little background: we use a framework, which copies all the data from database and gives back a package (array) of data – making the developer’s work simple enough. Even now, it didn’t sound scary!

The problem started there. The ‘framework’ doesn’t support this kind of variable-unknown-number of columns to be retrieved. I figured out I have multiple options: create runtime definition of class from the returned data or read the values into arrays (as usual). But the architecture of the project (which mandates a web service to be exposed by business layer and requires the web site to consume the web service) quite easily restricts transfer of ‘runtime generated type’s objects’. I knew there is no easy solution this way, the reason being web services (.net 2.0) needs to know the definition of data type which it handles. (This is made possible by process called ‘Serialization‘). and nope at his moment, it sounded not-scary).

So, I’m left with option of taking the data across web service as array of arrays (yes, a 2-dimensional array) along with other details I need.

Though I was almost near to completion of the solution, I wasn’t convinced of the idea that I couldn’t create runtime class in this solution that I thought was the only way to make it happen. And comes, the only way the data was to be displayed in the screen, it required to be a ‘typed’ 1-dimensional array. 🙂 Voila! – it’s an ‘array of objects of type that is generated at runtime’. That was quite interesting, since it was long time I worked on IL.

End of day – I was looking at the data displayed in the web page. Then I thought, ‘Hmm.. Not every thing comes out of the box in .NET’. Tomorrow I need to look at other aspects of the solution drafted before publishing it to the team.

I sure wanted to write more about this, I saved it for the next day!

PS: If you are wondering why I didn’t think of using datasets, it’s because datasets used across web services is real performance killer!

PS-2: I didn’t find any similar scenario posted in the web. For Type Builder in .NET refer here.