ObservableCollection(Of T) 与 BindingList(Of T)?

发布于 2024-07-12 05:24:46 字数 1385 浏览 12 评论 0原文

过去两年我开发了一些基于数据的 Winforms 应用程序,一切正常。 该应用程序构建在多个层(数据访问、业务逻辑和 UI)之上。 对于业务逻辑,我的所有对象都继承自名为 BaseEntity 的基类,其定义如下(有一些自定义对象和接口,与框架元素相结合):

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

在同一个核心库中,我有一个通用基集合 BaseEntityCollection。 这些集合允许我为每个对象定义其相关的强类型集合,这在基于数据的应用程序中非常有趣。 这是它的基本定义:

 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

如您所见,我使用了 Winforms 中正确数据绑定所需的所有内容:

  • 对象的 INotifyPropertyChanged、INotifyPropertyChanging、IEditableObject。
  • 我的集合基于 BindingList(Of T) 的集合。

我对新技术也很感兴趣,所以最近看了一些关于WPF的网络广播。 在这些网络广播中,他们使用 ObservableCollection(Of T) 作为集合和数据绑定支持的基类。

我正在考虑将我的一些应用程序从 Winforms 迁移到 WPF 的 UI 层。

我的问题是,对于我的业务逻辑,是否最好让我的集合基于 BindingList(Of T),或者我应该更改我的基集合类以使其继承自 ObservableCollection(Of T)。 我想为我的所有项目保留一个独特的基础集合,它也可以在 Winforms 应用程序、WPF 应用程序或 ASP.NET 中使用。 我还在我的项目中使用 Linq to Objects,因此我的项目仅基于框架 2.0,因此不会受到限制。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-07-19 05:24:46

我认为你的答案就在那里: http://xceed.com/CS/blogs/dontpanic/archive/2009/04/01/i-notify-we-notify-we-all-wait -no-we-don-t.aspx

简而言之,ObservableCollection 不会侦听其子项的更改,而只会侦听 Insert 和 Remove 事件。

另一方面,BindingList 确实监听其子级引发的更改和更新。 但由于绑定列表必须侦听其所有子级才能传播更改通知,因此会导致更多的内存负载。

希望这会有所帮助:)

--布鲁诺

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

审判长 2024-07-19 05:24:46

Claber,

我会保留 BindingList,因为 BindingList 比 ObservableCollection 支持更多的接口和更丰富的功能。
例如:

  1. BindingList 实现了 T 的 IList,而 ObservableCollection 则没有。
  2. BindingList 实现 ICancelAddNew 接口,数据绑定机制使用该接口取消新添加的项目(当您在向 DataGridView 添加行后单击 escape 时,该行将消失)。

我自己对 WPF 很陌生,不知道 ObservableCollection 提供的具体优势。

希望这可以帮助。

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.

笛声青案梦长安 2024-07-19 05:24:46

将我的两分钱添加到较旧的主题中:

当将这些通用集合中的任何一个数据绑定到 WinForms DataGridView,然后更新多个选定行的源数据中的属性时,您将看到:

  1. ObservableCollection 将仅更新最近选择的行的单元格值。
  2. BindingList 将更新所有选定行的单元格值。

我认为它们各有优点和缺点,但上面的例子对于那些不了解它的人来说可能是一个陷阱。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文