ObservableCollection(Of T) vs BindingList(Of T)?
Asked Answered
L

3

24

I've developed some data based Winforms Application this last two years and all works fine. This application are built on layers (DataAccess, Business Logic and UI). For the Business Logic, all my objects inherit from a base class called BaseEntity with the following definition (there are some custom objects and interfaces, combined with framework elements):

Public MustInherit Class BaseEntity
    Inherits SerializableObject
    Implements IEntity
    Implements IComparer,  _
               IEditableObject,  _
               INotifyPropertyChanging, INotifyPropertyChanged,  _
               IApplicationSecurity
End Class

In the same core library, I have a generic base collection BaseEntityCollection. These collection allows me to define, for each object, his related strongly typed collection, which is very interesting, in data based applications. Here is it's base definition:

 Public MustInherit Class BaseEntityCollection(Of T As BaseEntity)
    Inherits BindingList(Of T)
    Implements IEntityCollection
    Implements INotifyPropertyChanged, INotifyPropertyChanging, ICopyable(Of T)
    Implements IDisposable
    Implements ISerializable
  End Class

As you can see, I use all the stuff that's needed for correct databinding in Winforms:

  • INotifyPropertyChanged,INotifyPropertyChanging, IEditableObject for the object.
  • A collection based on BindingList(Of T) for my collection.

I'm also interested on new technologies, so I recently watched some webcast about WPF. In these webcast, they use as base class for collection and databinding support ObservableCollection(Of T).

I'm thinking on migrate some of my applications from Winforms to WPF for the UI Layer.

My question is, for my business logic, is it better keep my collections based on BindingList(Of T) or should I change my base collection class to make it inherit from ObservableCollection(Of T). I would like to keep a unique base collection for all my projects, that can be used as well in Winforms Applications, WPF Application or ASP.NET. I'm also using Linq to Objects in my projects, so I don't have restriction by keeping my projects based on only framework 2.0.

Latoyalatoye answered 18/1, 2009 at 15:10 Comment(0)
O
11

Claber,

I would keep the BindingList, because BindingList supports more interfaces and more feature rich than ObservableCollection. For example:

  1. BindingList implements IList of T, whereas ObservableCollection does not.
  2. BindingList implements ICancelAddNew interface that data binding mechanisms uses for cancelling the newly added item (when you clicked escape after adding a row to DataGridView, the row will dissappear).

I'm very new to WPF myself, and don't know the specific advantages ObservableCollection offers.

Hope this helps.

Orison answered 18/1, 2009 at 19:46 Comment(5)
ObservableCollection is thread safe, I don't know about BindingList.Curren
That is not correct. Microsoft's documentation explicitly states that the ObservableCollection is not thread safe. msdn.microsoft.com/en-us/library/ms668604.aspxWidgeon
1st statement is wrong since ObservableCollection<T> inherits Collection<T> and Collection<T> DOES implement ILIst<T> interface.Circumscribe
Incorrect information in this answer. ObservableCollection<T> does implement IList<T>.Mouthpart
NB, ObservableCollection<T> also gives actual deleted items. Sometimes this can be very helpful. BindingList<T> doesn't tell you what was deleted, only where.Mouthpart
D
16

I think your answer lies in there : http://xceed.com/CS/blogs/dontpanic/archive/2009/04/01/i-notify-we-notify-we-all-wait-no-we-don-t.aspx

To be short, ObservableCollection does not listen to changes in its children but only to Insert and Remove events.

On the other side BindingList does listen to changes and updates raised by its children. But because Binding list must listen to all its children to propagate the change notifications, it results in more memory load.

Hope this will help :)

--Bruno

Denationalize answered 5/11, 2010 at 13:50 Comment(1)
BindingList listens to each item (it's children, I guess?) within the collection because if an item within the collection changes, the BindingList passes that message to the grid or control to update that single cell or value. This means that the entire row does not need its values refreshed. Specific to the DataGridView, if you refresh a row, and not an individual cell, on the row you are editing, your changes go away. It just depends on your needs.Sannyasi
O
11

Claber,

I would keep the BindingList, because BindingList supports more interfaces and more feature rich than ObservableCollection. For example:

  1. BindingList implements IList of T, whereas ObservableCollection does not.
  2. BindingList implements ICancelAddNew interface that data binding mechanisms uses for cancelling the newly added item (when you clicked escape after adding a row to DataGridView, the row will dissappear).

I'm very new to WPF myself, and don't know the specific advantages ObservableCollection offers.

Hope this helps.

Orison answered 18/1, 2009 at 19:46 Comment(5)
ObservableCollection is thread safe, I don't know about BindingList.Curren
That is not correct. Microsoft's documentation explicitly states that the ObservableCollection is not thread safe. msdn.microsoft.com/en-us/library/ms668604.aspxWidgeon
1st statement is wrong since ObservableCollection<T> inherits Collection<T> and Collection<T> DOES implement ILIst<T> interface.Circumscribe
Incorrect information in this answer. ObservableCollection<T> does implement IList<T>.Mouthpart
NB, ObservableCollection<T> also gives actual deleted items. Sometimes this can be very helpful. BindingList<T> doesn't tell you what was deleted, only where.Mouthpart
V
3

Adding my two cents to an older topic:

When data binding either of these generic collections to a WinForms DataGridView and then updating properties in the source data for multiple selected rows, you'll see:

  1. The ObservableCollection<T> will only update the cell values of the most recently selected row.
  2. The BindingList<T> will update the cell values of all the selected rows.

I think they each have their advantages and disadvantages, but the above example can be a gotcha for those who are unaware of it.

Vice answered 13/2, 2014 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.