ObservableCollection(Of T)vs BindingList(Of T)? [英] ObservableCollection(Of T) vs BindingList(Of T)?
问题描述
公共MustInherit类BaseEntity
继承SerializableObject
实现IEntity
实现IComparer,_
IEditableObject,_
INotifyPropertyChanging,INotifyPropertyChanged,_
IApplicationSecurity
结束类
在同一个核心库中,我有一个通用的基础集合BaseEntityCollection。这些集合允许我为每个对象定义他的相关的强类型的集合,这在很多情况下都是基于数据的应用程序。这是它的基本定义:
公共MustInherit类BaseEntityCollection(作为BaseEntity)
/ pre>
继承BindingList(Of T)
实现IEntityCollection
实现INotifyPropertyChanged,INotifyPropertyChanging,ICopyable(Of T)
实现IDisposable
实现ISerializable
结束类
如您所见,我使用Winforms中正确数据绑定所需的所有东西:
- INotifyPropertyChanged,INotifyPropertyChanging,对象的IEditableObject。
- 基于BindingList(Of T)的集合,用于收集。
- BindingList实现Tist的IList,而ObservableCollection不支持。
- BindingList实现ICancelAddNew接口,数据绑定机制用于取消新添加的项目(当您向DataGridView添加行后单击转义时,行将消失)。
我还对新技术进行了干预,所以我最近看了一些关于WPF的网络广播。在这些网络广播中,它们用作收集和数据绑定支持的基类ObservableCollection(Of T)。
我正在考虑将我的一些应用程序从Winforms迁移到WPF我的问题是,对于我的业务逻辑,最好是根据BindingList(Of T)保存我的集合,或者我应该将我的基类收集类更改为使它从ObservableCollection(Of T)继承。我想为所有项目保留一个独特的基础集合,可以在Winforms应用程序,WPF应用程序或ASP.NET中使用。
我也在我的项目中使用Linq到对象,所以我没有限制,只保留我的项目仅基于框架2.0。
谢谢,
ClaBer
Claber
我会保留BindingList,因为BindingList支持更多的界面和更多的功能比ObservableCollection。
例如:
我对WPF自己很新,并且不知道ObservableCollection提供的具体优势。
希望这有帮助。
I've developped 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 Businness 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, wich is very interresting, 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 interrested 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.
Thanks,
ClaBer
Claber,
I would keep the BindingList, because BindingList supports more interfaces and more feature rich than ObservableCollection. For example:
- BindingList implements IList of T, whereas ObservableCollection does not.
- 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.
这篇关于ObservableCollection(Of T)vs BindingList(Of T)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!