views:

4998

answers:

4

Hi,

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

+3  A: 

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.

Valentin Vasiliev
A: 

Valve,

Thank you for your answer, I will let my bussiness layer logic the same (based on BindingList) on my released projects for the moment.

Looking further the ObservableCollection class definition, I noticed the ICollectionChanged interface, wich maybe could be the way of WPF native binding process. I also noticed that both inherit from Collection(of T), but as you said, there are more specific interfaces defined in the BindingList.

I think that using ObservableCollection in Winforms projects could be difficult, without implementing all BindingList specific interfaces in my base class. I also think that mixing these two systems of notification, may probaly cause bugs in my apps.

So, I will first try to test my actual bussiness layer based on BindingList with a small WPF projet, to check out how WPF binding works and find out what could be better with ObservableCollection. I will also try to build a small Winforms test application, to see the ObservableCollection behaviour in these context, with a modified BaseEntityCollection based on ObservableCollection.

Thanks,

Claude

ClaBer
+1  A: 

Im sorry but saying that ObservableCollection does not implement IList of T is wrong. System.Collections.ObjectModel.ObservableCollection inherets from System.Collections.ObjectModel.Collection witch implements Collection of T AND Ilist of T. WTF. So now we have System.Collections.ObjectModel.Collection that implements bots ICollection of T and IList og T. It kinda feels like there ate some ppl at MS that dont speak to each other.

Jens Schumacher
This should be a comment.
Josh Kodroff