.NET ObservableDictionary

63,973

Solution 1

I'd suggest that you implement the IDictionary<TKey, TValue> instead of inheriting from Dictionary<TKey, TValue>. Since you have to use new rather than override it's possible that the methods are simply being called on the base class rather than your class. I'd be tempted to use a Dictionary<TKey, TValue> internally to do the actual storing of data.

In fact I found this: http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/12/26/observabledictionary-lt-tkey-tvalue-gt-c.aspx

Solution 2

The Microsoft ParallelExtensionsExtras provides this class which is not only observable but is also concurrent:

Now available via Nuget: https://www.nuget.org/packages/MSFT.ParallelExtensionsExtras/

The source code was recently updated for .NET Standard 2.1 on 05/11/2020 and the source code is available at GitHub: https://github.com/dotnet/samples/tree/master/csharp/parallel/ParallelExtensionsExtras

Note that some of the features are now a part of newer .NET frameworks. Are the ParallelExtensions "Extras" still of value?

Microsoft Tour Blog: https://blogs.msdn.microsoft.com/pfxteam/2010/04/04/a-tour-of-parallelextensionsextras/

//--------------------------------------------------------------------------
// 
//  Copyright (c) Microsoft Corporation.  All rights reserved. 
// 
//  File: ObservableConcurrentDictionary.cs
//
//--------------------------------------------------------------------------

using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
using System.Diagnostics;

namespace System.Collections.Concurrent
{
    /// <summary>
    /// Provides a thread-safe dictionary for use with data binding.
    /// </summary>
    /// <typeparam name="TKey">Specifies the type of the keys in this collection.</typeparam>
    /// <typeparam name="TValue">Specifies the type of the values in this collection.</typeparam>
    [DebuggerDisplay("Count={Count}")]
    public class ObservableConcurrentDictionary<TKey, TValue> :
        ICollection<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>,
        INotifyCollectionChanged, INotifyPropertyChanged
    {
        private readonly SynchronizationContext _context;
        private readonly ConcurrentDictionary<TKey, TValue> _dictionary;

        /// <summary>
        /// Initializes an instance of the ObservableConcurrentDictionary class.
        /// </summary>
        public ObservableConcurrentDictionary()
        {
            _context = AsyncOperationManager.SynchronizationContext;
            _dictionary = new ConcurrentDictionary<TKey, TValue>();
        }

        /// <summary>Event raised when the collection changes.</summary>
        public event NotifyCollectionChangedEventHandler CollectionChanged;
        /// <summary>Event raised when a property on the collection changes.</summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Notifies observers of CollectionChanged or PropertyChanged of an update to the dictionary.
        /// </summary>
        private void NotifyObserversOfChange()
        {
            var collectionHandler = CollectionChanged;
            var propertyHandler = PropertyChanged;
            if (collectionHandler != null || propertyHandler != null)
            {
                _context.Post(s =>
                {
                    if (collectionHandler != null)
                    {
                        collectionHandler(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                    }
                    if (propertyHandler != null)
                    {
                        propertyHandler(this, new PropertyChangedEventArgs("Count"));
                        propertyHandler(this, new PropertyChangedEventArgs("Keys"));
                        propertyHandler(this, new PropertyChangedEventArgs("Values"));
                    }
                }, null);
            }
        }

        /// <summary>Attempts to add an item to the dictionary, notifying observers of any changes.</summary>
        /// <param name="item">The item to be added.</param>
        /// <returns>Whether the add was successful.</returns>
        private bool TryAddWithNotification(KeyValuePair<TKey, TValue> item)
        {
            return TryAddWithNotification(item.Key, item.Value);
        }

        /// <summary>Attempts to add an item to the dictionary, notifying observers of any changes.</summary>
        /// <param name="key">The key of the item to be added.</param>
        /// <param name="value">The value of the item to be added.</param>
        /// <returns>Whether the add was successful.</returns>
        private bool TryAddWithNotification(TKey key, TValue value)
        {
            bool result = _dictionary.TryAdd(key, value);
            if (result) NotifyObserversOfChange();
            return result;
        }

        /// <summary>Attempts to remove an item from the dictionary, notifying observers of any changes.</summary>
        /// <param name="key">The key of the item to be removed.</param>
        /// <param name="value">The value of the item removed.</param>
        /// <returns>Whether the removal was successful.</returns>
        private bool TryRemoveWithNotification(TKey key, out TValue value)
        {
            bool result = _dictionary.TryRemove(key, out value);
            if (result) NotifyObserversOfChange();
            return result;
        }

        /// <summary>Attempts to add or update an item in the dictionary, notifying observers of any changes.</summary>
        /// <param name="key">The key of the item to be updated.</param>
        /// <param name="value">The new value to set for the item.</param>
        /// <returns>Whether the update was successful.</returns>
        private void UpdateWithNotification(TKey key, TValue value)
        {
            _dictionary[key] = value;
            NotifyObserversOfChange();
        }

        #region ICollection<KeyValuePair<TKey,TValue>> Members
        void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
        {
            TryAddWithNotification(item);
        }

        void ICollection<KeyValuePair<TKey, TValue>>.Clear()
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Clear();
            NotifyObserversOfChange();
        }

        bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Contains(item);
        }

        void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).CopyTo(array, arrayIndex);
        }

        int ICollection<KeyValuePair<TKey, TValue>>.Count
        {
            get { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).Count; }
        }

        bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
        {
            get { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).IsReadOnly; }
        }

        bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
        {
            TValue temp;
            return TryRemoveWithNotification(item.Key, out temp);
        }
        #endregion

        #region IEnumerable<KeyValuePair<TKey,TValue>> Members
        IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator();
        }
        #endregion

        #region IDictionary<TKey,TValue> Members
        public void Add(TKey key, TValue value)
        {
            TryAddWithNotification(key, value);
        }

        public bool ContainsKey(TKey key)
        {
            return _dictionary.ContainsKey(key);
        }

        public ICollection<TKey> Keys
        {
            get { return _dictionary.Keys; }
        }

        public bool Remove(TKey key)
        {
            TValue temp;
            return TryRemoveWithNotification(key, out temp);
        }

        public bool TryGetValue(TKey key, out TValue value)
        {
            return _dictionary.TryGetValue(key, out value);
        }

        public ICollection<TValue> Values
        {
            get { return _dictionary.Values; }
        }

        public TValue this[TKey key]
        {
            get { return _dictionary[key]; }
            set { UpdateWithNotification(key, value); }
        }
        #endregion
    }
}

Solution 3

Your solution - Fixed ;)

public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged {
    public ObservableDictionary( ) : base( ) { }
    public ObservableDictionary(int capacity) : base(capacity) { }
    public ObservableDictionary(IEqualityComparer<TKey> comparer) : base(comparer) { }
    public ObservableDictionary(IDictionary<TKey, TValue> dictionary) : base(dictionary) { }
    public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }
    public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { }

    public event NotifyCollectionChangedEventHandler CollectionChanged;
    public event PropertyChangedEventHandler PropertyChanged;

    public new TValue this[TKey key] {
        get {
            return base[key];
        }
        set {
            TValue oldValue;
            bool exist = base.TryGetValue(key, out oldValue);
            var oldItem = new KeyValuePair<TKey, TValue>(key, oldValue);
            base[key] = value;
            var newItem = new KeyValuePair<TKey, TValue>(key, value);
            if (exist) {
                this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newItem, oldItem, base.Keys.ToList( ).IndexOf(key)));
            } else {
                this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newItem, base.Keys.ToList( ).IndexOf(key)));
                this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
            }
        }
    }

    public new void Add(TKey key, TValue value) {
        if (!base.ContainsKey(key)) {
            var item = new KeyValuePair<TKey, TValue>(key, value);
            base.Add(key, value);
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, base.Keys.ToList( ).IndexOf(key)));
            this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
        }
    }

    public new bool Remove(TKey key) {
        TValue value;
        if (base.TryGetValue(key, out value)) {
            var item = new KeyValuePair<TKey, TValue>(key, base[key]);
            bool result = base.Remove(key);
            this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, base.Keys.ToList( ).IndexOf(key)));
            this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
            return result;
        }
        return false;
    }

    public new void Clear( ) {
        base.Clear( );
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        this.OnPropertyChanged(new PropertyChangedEventArgs(nameof(Count)));
    }

    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
        if (this.CollectionChanged != null) {
            this.CollectionChanged(this, e);
        }
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
        if (this.PropertyChanged != null) {
            this.PropertyChanged(this, e);
        }
    }
}

Solution 4

I rolled out my own: https://www.nuget.org/packages/hellosam.net.collections/

It uses AVL tree so operations are O(log N) instead, where most implementations I have seen using List.indexOf() are O(N).

It can even observe your item INotifyPropertyChanged and convert them to a observable collection event, so to keep the DataGrid sort/group response to the change.

Share:
63,973
Cos
Author by

Cos

Updated on May 15, 2020

Comments

  • Cos
    Cos about 4 years

    I have written the following class which implements(or tries to!) a dictionary with notifications:

    public partial class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged
    {
        public ObservableDictionary() : base() { }
        public ObservableDictionary(int capacity) : base(capacity) { }
        public ObservableDictionary(IEqualityComparer<TKey> comparer) : base(comparer) { }
        public ObservableDictionary(IDictionary<TKey, TValue> dictionary) : base(dictionary) { }
        public ObservableDictionary(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }
        public ObservableDictionary(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : base(dictionary, comparer) { }
    
        public event NotifyCollectionChangedEventHandler CollectionChanged;
    
        public new TValue this[TKey key]
        {
            get
            {
                return base[key];
            }
            set
            {
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, key, 0));
                base[key] = value;
            }
        }
    
        public new void Add(TKey key, TValue value)
        {
            base.Add(key, value);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, key, 0));
        }
    
        public new bool Remove(TKey key)
        {
            bool x = base.Remove(key);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, key, 0));
            return x;
        }
    
        public new void Clear()
        {
            base.Clear();
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    
    
        protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (CollectionChanged != null)
            {
                CollectionChanged(this, e);
            }
        }
    }
    

    In another class, I have a listener for the MyObservableDictionary.CollectionChanged event:

    The problem I'm having is that the event doesn't fire. How can I fix this?

  • MasterMastic
    MasterMastic over 10 years
    Any reason why this class is partial?
  • IAbstract
    IAbstract about 10 years
    @MasterMastic: you would have to ask the OP that question. Marciej provided the class declaration the same as the OP.
  • BillHaggerty
    BillHaggerty almost 10 years
    using xaml how do you bind the SelectedValuePath and SelectedValue?
  • O. R. Mapper
    O. R. Mapper over 9 years
    Unfortunately, the blogpost doesn't seem to specify under which license terms the code may be used.
  • AJHope
    AJHope almost 9 years
    This line occurs error: IEnumerator IEnumerable.GetEnumerator() { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator(); }
  • VoteCoffee
    VoteCoffee almost 9 years
    I've used the code and it works. What error message are you getting?
  • AJHope
    AJHope almost 9 years
    Error message: Using the generic type 'System.Collections.Generic.IEnumerator<T>' requires 1 type arguments
  • Rajiv
    Rajiv almost 9 years
    replace that with: IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() { return ((ICollection<KeyValuePair<TKey, TValue>>)_dictionary).GetEnumerator(); }
  • IDIR Samir
    IDIR Samir about 8 years
    I can't get to call .Clear() because it doesnt exist. Any ideas?
  • VoteCoffee
    VoteCoffee about 8 years
    .clear has been part if icollection since DotNET 2.0. It likely is a problem on the icollection type you are using and not this code. Are you using this on a custom icollection class or on one of the built-in types?
  • Carlos Teixeira
    Carlos Teixeira almost 8 years
    A nice addition would be to return the "Keys" and "Values" as an ObservableCollection as well, or some other collection that implements INotifyPropertyChanged/INotifyCollectionChanged.
  • VoteCoffee
    VoteCoffee almost 8 years
    You can! It's generic, so just specify this during implementation/instantiation. If you want to force this behavior, create a derived/inherited class that narrows the definition.
  • Maciej Klemarczyk
    Maciej Klemarczyk almost 8 years
    @MasterMastic there is no reason to use partial.
  • Eric
    Eric over 7 years
    nuget package missing BinaryTree.cs Also, enumerator enumerates values instead of key value pairs which is atypical for dictionaries.
  • Eric
    Eric over 7 years
    in your remove function you have to query the key before you remove the item from the dictionary or else the key will always be -1 :)
  • Nathan R
    Nathan R over 7 years
    Depending on the usage, you may want to make _context.Post(...) use _context.Send(...) instead for synchronous event handling. I ran into race conditions trying to use it asynchronously.
  • Ignacio Soler Garcia
    Ignacio Soler Garcia over 6 years
    To notify only Resets does not correctly fulfill the INotifyCollectionChanged interface.
  • Szybki
    Szybki over 6 years
    @Korayem To get access to the Clear method you must cast the dictionary to the ICollection<KeyValuePair<TKey, TValue>>. I'd suggest using a following extension method: public static ICollection<KeyValuePair<TKey, TValue>> AsCollection<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) { return dictionary; }
  • mattyb
    mattyb about 6 years
    @IgnacioSolerGarcia yeah I second that
  • McGuireV10
    McGuireV10 over 5 years
    @IgnacioSolerGarcia and Matthew ... correct collection notifications here
  • McGuireV10
    McGuireV10 over 5 years
    These collections are probably a better option: codeproject.com/Articles/64936/…
  • VoteCoffee
    VoteCoffee about 5 years
    I appreciate the commentary. I was pasting the source code that is part of the NuGet package.
  • Cfun
    Cfun about 4 years
    Is it working in the case of an initialization? Dic = new ObservableDictionary<string, string>(){ {"1", "First"} }; ?
  • Maciej Klemarczyk
    Maciej Klemarczyk over 3 years
    @Cfun It supports all standard dictionary initialization, I tested it is also valid solution for new .NET 5.0. However with new C# we could improve the syntax of the implementation.