ObservableCollection(Of T) vs BindingList(Of T)? [英] ObservableCollection(Of T) vs BindingList(Of T)?

查看:15
本文介绍了ObservableCollection(Of T) vs BindingList(Of T)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

  • INotifyPropertyChanged、INotifyPropertyChanging、IEditableObject 用于对象.
  • 一个基于 BindingList(Of T) 的集合,用于我的集合.

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

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).

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

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

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

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.

推荐答案

Claber,

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

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

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

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

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

希望这会有所帮助.

这篇关于ObservableCollection(Of T) vs BindingList(Of T)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆