C# – Linq to Objects performance and best implementation


I work on a web application that modifies a XML document that is stored in a database. It is made to mimic exactly like any program does when you click the "preferences" option in the menu. The XML document is read by a local client application and any changes are reflected in the local client.

My web app has 3 tiers I guess you would say with the aspx page being the "view", a service layer where I validate / process all user input, and a data layer where I update the XML document.

For each page of settings, I had been recursively traversing all the controls on the page and if I came to one that I wanted to work on (checkbox, textbox, …) I added it to a IList and then sent that IList to the service layer where I then had to pull the control out of the list so I could work on it.

I noticed this seemed to be a bit slow so I profiled the page and basically the LINQ to Objects queries that I had been using tend to eat up a ton of time.

(CheckBox)lstControls.Where(x => x.ID == "some_id").SingleOrDefault();

I then switched to manually adding the controls on the page to a IList and then pulling them out in the service layer by indexer in the order they were put in. This is fugly and completely dependent on you not screwing up the index of the control you are looking for.

Finally, this breaks the rule of mixing elements from the view with the service layer or data layer. I know I should be working with data only, but I am at a loss of how to efficiently do this.

Each settings page has from one to thirty controls that need to be processed. How do I get all the data from the controls to the service layer without sending the actual controls?

Thanks for the help….

Best Solution

You might get into a better situation if you gather the data, the stuff you are actually interested in, into some object structure with some semantics that will help you structure what is passed on to the next layer. The keyword "Databinding" should also help you to just get to the values that were entered. There is no need to pass any controls back to the next layer..