Why is ReadOnlyObservableCollection.CollectionChanged not public?

后端 未结 8 2106
心在旅途
心在旅途 2021-01-31 06:50

Why is ReadOnlyObservableCollection.CollectionChanged protected and not public (as the corresponding ObservableCollection.CollectionChanged is)?

What is the use of a col

8条回答
  •  有刺的猬
    2021-01-31 07:32

    Solution

    ReadOnlyObservableCollection.CollectionChanged is not exposed (for valid reasons outlined in other answers), so let's make our own wrapper class that exposes it:

    /// A wrapped  that exposes the internal "/>.
    public class ObservableReadOnlyCollection : ReadOnlyObservableCollection
    {
        public new NotifyCollectionChangedEventHandler CollectionChanged;
    
        public ObservableReadOnlyCollection(ObservableCollection list) : base(list) { /* nada */ }
    
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args) => 
            CollectionChanged?.Invoke(this, args);
    }
    

    Explanation

    People have asked why you would want to observe changes to a read-only collection, so I'll explain one of many valid situations; when the read-only collection wraps a private internal collection that can change.

    Here's one such scenario:

    Suppose you have a service that allows adding and removing items to an internal collection from outside the service. Now suppose you want to expose the values of the collection but you don't want consumers to manipulate the collection directly; so you wrap the internal collection in a ReadOnlyObservableCollection.

    Note that in order to wrap the internal collection with ReadOnlyObservableCollection the internal collection is forced to derive from ObservableCollection by the constructor of ReadOnlyObservableCollection.

    Now suppose you want to notify consumers of the service when the internal collection changes (and hence when the exposed ReadOnlyObservableCollection changes). Rather than rolling your own implementation you just want to expose the CollectionChanged of the ReadOnlyObservableCollection. Rather than forcing the consumer to make an assumption about the implementation of the ReadOnlyObservableCollection, you simply swap the ReadOnlyObservableCollection with this custom ObservableReadOnlyCollection, and you're done.

    The ObservableReadOnlyCollection hides ReadOnlyObservableCollection.CollectionChanged with it's own, and simply passes on all the collection changed events to any attached event handler.

提交回复
热议问题