« Back to blog

Trivial Error

Well, this is a post about how i made a real silly mistake when i could have easily avoided it. I want to share this experience so that others can gain something from it. So here is the Scenario: A classical integration of the services with the Silverlight UI. All calls are Asynchronous and hence all, of them have a get completed event handler. So, here is a page where i have to make 2 service calls and thus two event handlers. Service Call 1 (SvC1) was to get the data for the UI. Service Call 2 (SvC2) was to get a certain key value pair which would dictate the number and content of a set of check boxes in the UI. So in essence, i was using SvC2 to dynamically set up controls (i was using an items control). Mind you, SvC2 was never returning the controls it self, it was simply giving me the data, i.e the Label/Content property of the check boxes and their tooltip/description property. And yes, the UI has a standard New and Open scenarios. Here is what i did: I made the call to SvC1 first and set up the data context as required, ie the data context would not be set in case of a New Item creation. So, whether or not i was going to bind the data, i would be making the call. Then, irrespective of that, i make the SvC2 to get the data for the check box controls and then with the data-context (if given), and the data from SvC2, i create the controls. Now, why i say "if given" in the previous statement is because some of the check boxes would be clicked if the data context were given, else all of them would be false or not clicked. There is another issue, once i have created the check box controls, it will be very difficult to access them (they are inside the ItemsControl). I would rather make the controls, and assign the data to them in one go. I would not like to split these two tasks, for the sake of simplicity of code and its understandability. Here is what i should have done: I should have made the call to SvC2 first, which would have returned the list of data to be shown in the controls i.e  the check boxes. Then i should have called SvC1 on the Completed (event or on completion) of SvC2, and set the data context, i.e. bound the data with the controls. While doing this i should have set the items source for the check boxes, or, in simple words, i should have created the check boxes dynamically using the information i got in SvC2. This way, a) I would have gotten the data for the controls before hand, b) I would have made the check on the data-context requirement on the completion of SvC2 and before calling SvC1, and end up saving one SvC if required. Otherwise, i was making the SvC1 no matter what and then probably not end up using it and then call SvC2 anyway because it was being called on the get completed event handler of SvC1. c) And finally, i would still be able to create the controls and bind the data to them in one go. You may not be cutting trees by making an extra service call, but you sure are troubling some million electrons for it. :D So take care, use the Service wisely.