05 May, 2005

.NET, Data Binding and Despair

You have a well-architectured application, i.e. one which uses an object persistence framework. You also want to modularise the GUI code; to this end, the controls to edit each object are wrapped in a UserControl. A control to edit a composite object is made by embedding the constituent user controls in a new user control, along with editors for any other persistent properties. Each UserControl exposes just one property, the bound object. How do you get data binding to work in such a scenario?

Have a look around the MSDN documentation. I'm not going to provide any links to it; I want you to get the "Microsoft experience" yourself. When you've finished, you'll probably need a valium. Unless you're binding a Dataset to regular controls on the same form, it isn't explained properly anywhere, although it will take a lot of wading through spotty and incoherent documentation to discover this. Hence you turn to Google. As you might never find a decent explanation (it took me long enough), I'll point you to one. Don't complain about the English; how good is your German? The most relevant page to the task at hand is this one.

OK, so you've written a dozen or so fooControl.DataBindings.Add("Value", persistentObject, "property"); lines to each control (so much for RAD and compile-time checks). Then you add a fooChanged event to each UserControl, and wire up the event handlers. Finally, you run the program. After tracing lots of mysterious errors to misspelled string literals in the Add(string, Object, string) methods, it might work. It probably won't though.

If you were foolish enough to go through with this frustrating exercise, you'll notice that I've skipped a lot of the steps in the linked page. This is to save you a lot of unnecessary trouble, because you can deduce from the failings in its current form that the missing code won't help. There is also a critical step missing; .NET data binding is so over-engineered, over-complicated and under-documented that it is simply not possible to make it work reliably with anything other than Datasets. I ended up writing the code to flag dirty objects and save or reload them as necessary myself. Don't despair though, we're in good company. If you look around the net you'll discover that (a)hardly anyone seems to be able to get this basic task right, and (b)whenever the question is raised at a Microsoft WebCast, the hosts avoid the question. I.e. Microsoft can't do it either.

2 Comments:

Anonymous Anonymous said...

Thanks for the link :)

One think I could not undestand:
After all was my little site useful to you?

NoiseEHC

1:25 am  
Blogger G. Eek! said...

It was useful as an educational experience :-)

8:59 am  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home