Skip to content

WrappingObservableReadOnlyList

Yeah69 edited this page Nov 5, 2016 · 2 revisions

Intention

Wraps an ObservableCollection into the IObservableReadOnlyList interface.

Motivation

The ConcatenatingObservableReadOnlyList has no constructor taking ObservableCollections directly, it only can concatenate wrapped ObservableCollections (the reason for that is written in its wiki page). Another purpose of wrapping an ObservableCollection might be to just make it read-only, so the ObservableCollection may be private and expose the CollectionChanged through a public wrapper. In this case the source collection is save from manipulation from outside.

Features

Actually, the inteface of the WrappingObservableReadOnlyList (IReadOnlyList, INotifyCollectionChanged, INotifyPropertyChanged) is a sub set of the ObsevableCollection's interface (ICollection, INotifyCollectionChanged, INotifyPropertyChanged), because IReadOnlyList is a part of ICollection. So by wrapping the ObservableCollection functionality is taken away or more precisely, the functionality is omitted. Still there are two reasons for wrapping:

  • Co-Variance of the generic type.
  • Encapsulation.

Example

IObservableReadOnlyList<T> wrapA = new WrappingObservableReadOnlyList<TA>(aObservableCollectionOfTypeTA);

Note that TA is inheriting from T in this example. Thus, this is an example of the co-variance capability and also wrapA can only be read and observed. Modification have to be done on aObservableCollectionOfTypeTA, which could be a private field for encapsulation.

Clone this wiki locally