4

I've noticed these two ways of implementing databinding in winforms.However, I would like to know which one of them is more preferred (In terms of overall performance e.g design time, efficiency? As far as I know, these two are:

BindingSource as BindingSource:

this.textBox1.DataBindings.Add(new Binding("Text", this.myBindingSource, "Augend", true));

  • Can be easily implemented during design time with the use of properties window of the form and let it auto-generate the code.

  • Updates the control by using INotifyPropertyChanged and simply calling OnPropertyChanged without strict PropertyName values (which seems to be a let down for me)

BindingSource as ViewModel:

this.textBox1.DataBindings.Add(new Binding("Text", this.myViewModel, "Augend", true));

  • Seems to be more work for the setup without the auto-generation and ProeprtyName Matching of the ViewModel

  • Updates the control by using INotifyPropertyChanged BUT the PropertyName should be the same as the Property of the object (which somehow gives the feeling of assurance instead of the previous one)

I'm starting to lean more towards BindingSource as ViewModel but I think it's much easier for the control designer of the application if the BindingSource as BindingSource was used. I believe that the control and the binding will be losely coupled. He can change the controls to whatever he wants and just binds the data using its property windows instead of diving into the code and manually change the setup there.

sac1
  • 1,344
  • 10
  • 15
Lawrence
  • 334
  • 2
  • 12
  • `BindingSource` is a source for data-binding and probably your question is **"Which one is preferred: data-binding to a view model with or without BindingSource?"** – Reza Aghaei Dec 14 '15 at 09:53
  • Now that you mention it, my `this.myBindingSource` is technically binded to a ViewModel through the property window. I guess my initial idea came from the snippet that I showed where the source is either a ViewModel or a `BindingSource`. – Lawrence Dec 14 '15 at 10:21

1 Answers1

1

However, I would like to know which one of them is more preferred (In terms of overall performance e.g design time, efficiency?

Shortly, there is no preferred one.

  • The difference in performance (if any) is negligible. There is a lot reflection involved in data binding in order to count a few additional delegating calls (which nobody does these days anyway in general).
  • What about the design time support, it depends of the requirements and cannot be used as "overall performance" factor. For instance, many business applications prefer runtime automatic UI generation by code, using rules and attributes (like data annotations), thus do not need design time support at all. From the other side, if you do need a design time support, there is no other option than using BindingSource or similar intermediary. The BindingSource itself is no more than a data source adapter, which binds to a type (or a small data mockup) at the design time, and to a real instance at runtime.
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343