Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

winforms - ObservableCollection(Of T) vs BindingList(Of T)?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...