“Why I hate Data Binding” Slides and Links

I just got home from the Sydney .NET user group. Man, I love presenting. I wish someone would pay me to be a full time presenter. On second thought, I wish someone would pay me just to sleep all day. But presenting would be a pretty good alternative.


#ifdef YOU_WERENT_THERE

To summarise what happened, Adam Cogan gave a great presentation of how nice data binding is. He showed off the cool design time support, binding sources, and how much time data binding saves you.

Then it was my turn. I hate data binding. I hate data binding because it’s too good. See, my problem was simple. I have a combo box that I want to populate with Customer objects using data binding. But then I also want to add an item at the top called “(None)”.

This was all based on a problem I had to solve in a real application recently. I tried a bunch of solutions, but I couldn’t settle on one I liked. In the end, my solution was to scrap data binding (but only for that combo)

A lot of the solutions involved changing things at the database level, or the business layer. None of these sat very well with me, because as far as I’m concerned, it’s a GUI limitation. If I was using ASP.NET, this wouldn’t be an issue - so why would I go creating invalid database records, or changing my business layer, or even creating a view for that matter, just so that I can use data binding?

I made a judgement call, and decided that the advantages of using data binding (in this case, there really wasn’t any) really don’t outweigh the disadvantages and work involved in implementing most of the other solutions that were offered.

I’m certainly not saying don’t ever use data binding. I use databinding in plenty of other places and I love it. After trying 5 or 6 different solutions, and thinking of numerous, hacky workarounds, I concluded that data binding wasn’t worth it in this case.

My problem was, I fell in love with data binding, and even though I knew not using data binding would be the easiest solution, it took me 6 different attempts before I could let go of it. In some cases, like if I was populating a DataGridView, then maybe the database-layer hacks would be worth it. But this was a combo, and it was wasting my time.


#endif

I want to thank the audience member who brought up the hat analogy. He described the problem as being “the hat is too small for your head”. Then he likened the solutions to being “cut off your nose and ears to make the hat fit”. Classic!

I was a little worried about doing this one, as it seemed I had a pretty good problem, but no real solution I could offer. It’s a little scary. But after going through with it, it was a blessing in disguise. Since I couldn’t really offer a solution that seemed perfect, everyone was forced to think and offer other alternatives, or at least to give reasons why they didn’t like the ones I showed. I think at least half the members of the audience had chimed in by the end of the presentation. Who could ask for more?

Corneliu offered another workaround that he says works, so I’ll follow that up in a later post.

I can’t offer the code I showed tonight as it relies on some business objects we’re using in client code, but the slides are pretty self explanatory.

Download the slides to Why I Hate Data Binding.

Also, I said I’d post a link to Microsofts Inductive User Interface guidelines for those who were interested in why we made some of the GUI decisions we did.

Oh, and if you want to learn more about data binding in Windows Forms for .NET 2.0, you should buy this book. I’m only just reading it now, and chances are I’m only one chapter away from the “How to solve any problem related to combo boxes” section. It’s a great read though.

7 Responses to ““Why I hate Data Binding” Slides and Links”

  1. What was stopping you from adding a single ‘None’ option manually to the dropdown, followed by binding the data source?

    Could you inherit from the base dropdown control and add your own default into it? That way, all you’d have to do is just bind as per normal and things would just work.

    Al.

  2. Good presentation last last night - by you and Adam. Thought there was a lot of good discussion around your talk and gave plenty of points to think about in the future.

    Thanks for the slides, and although you can’t put up the code could you paste in Corneliu’s solution? Looked good but I found it hard to follow, being at the early stages of wrapping classes etc. Then at least I can play around with it.

    All the best, Neil.

  3. Al,

    I actually had inherited from the ComboBox control to populate it, that way I only had to do this once. I didn’t try adding the item first and then binding, but I’d bet my computer that it would have had the same outcome as solution 1.

    Neil,

    Thanks! I appreciate that. I’ll list all the solutions (should I say “hacks”?) with sample code in a blog post either tonight or tommorow night. I too am pretty interested to see of Corneliu’s latest crazy solution works.

  4. Neil, Paul,

    I don’t think my solution actually works.

    You can get the the items to be displayed correctly by overriting the Format function, but that only solves half the problem, plus you loose any custom formatting you might want to use. The main problem with this is that your Value will be the internal object itself and not the property from the ValueMember as you would expect.
    I’m now trying to hack this using TypeDescriptors but this is not as simple as you would like it to be.

    However, I have a better idea that might help :) Change your code-generator to also generate a static Customer that has no row inside and returns None for name and -1 for Id for example.
    Make this a static property of your customer so you can access it like Customer.NoneCustomer.
    With this you can use my wrapper list and return that NoneCustomer whenever the item zero is requested.
    This way you would keep your OOP clean as well, as the BO is the one actually creating that static customer.

    Corneliu.

  5. Hi, I was just looking at the issue, and know little about data binding as i have always used our companies standard ObjectToControls and ControlsToObject feature, anyway this might add another train of thought… could you make each business object nullable, ie Dim x as Nullable(of Customer) that way you can have a customer object without really having a customer… just a thought, I could be barking up the wrong tree here… anyways I’ll be keeping an eye on this blog to see how you go.

  6. I don’t really understand why you wouldn’t want to use one of your proposed solutions?

    I always do something like this:

    myDropDown.DataSource = dtSample;
    myDropDown.DataTextField = “dbTextField”;
    myDropDown.DataValueField = “dbValueField”;
    myDropDown.DataBind();

    //Insert the none item at the first position
    myDropDown.Items.Insert(0, new ListItem(”None”, “0″));

  7. Wow, I think you guys have really overcomplicated the problem.

    schlub: That’s exactly how i’ve handled this in the past also

Leave a Reply