Wednesday, August 29, 2012

Observable Dictionary

I recently came across a situation where I needed to flush some preset values if an IDictionary's collection of key-value pairs had changed.  I was surprised to discover that the Dictionary object was not event-aware.  It made me sad.

A quick look on Google showed others had created ObservableDictionary objects, but I really didn't want to pull in someone else's project or binary to my solution.  It was a really simple piece of code.  I present it here if someone else wants to create their own version of the ObservableDictionary.


public class odKVPEventArgs<TKey, TValue> : EventArgs
{
public TKey Key { get; set; }
public TValue Value { get; set; }
}
public class odKVPChangeEventArgs<TKey, TValue> : odKVPEventArgs<TKey, TValue>
{
public TValue OldValue { get; set; }
}
public delegate void kvpEvent<TKey, TValue>(object sender, odKVPEventArgs<TKey, TValue> e);
public delegate void kvpChangeEvent<TKey, TValue>(object sender, odKVPChangeEventArgs<TKey, TValue> e);
public delegate void dictClearEvent(object sender, EventArgs e);
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> Base;
public ObservableDictionary()
{
Base = new Dictionary<TKey, TValue>();
}

public event kvpEvent<TKey, TValue> OnAdd;
public event kvpEvent<TKey, TValue> OnRemove;
public event kvpChangeEvent<TKey, TValue> OnChange;
public event dictClearEvent OnClear;

public void Add(TKey key, TValue value)
{
Base.Add(key, value);
if (OnAdd != null)
{
OnAdd(this, new odKVPEventArgs<TKey, TValue>()
{
Key = key,
Value = value
});
}
}

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

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

public bool Remove(TKey key)
{
var theKey = key;
var theValue = Base[key];
var r = Base.Remove(key);
if (OnRemove != null)
{
OnRemove(this, new odKVPEventArgs<TKey, TValue>()
{
Key = theKey,
Value = theValue
});
}
return r;
}

public bool TryGetValue(TKey key, out TValue value)
{
var r = Base.TryGetValue(key, out value);
return r;
}

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

public TValue this[TKey key]
{
get
{
return Base[key];
}
set
{
if (Base.ContainsKey(key))
{
var oldValue = Base[key];
Base[key] = value;
if (OnChange != null)
{
OnChange(this, new odKVPChangeEventArgs<TKey, TValue>()
{
Key = key,
OldValue = oldValue,
Value = value
});
}
}
else
{
Base[key] = value;
if (OnAdd != null)
{
OnAdd(this, new odKVPEventArgs<TKey, TValue>()
{
Key = key,
Value = value
});
}
}
}
}

public void Add(KeyValuePair<TKey, TValue> item)
{
Base.Add(item);
if (OnAdd != null)
{
OnAdd(this, new odKVPEventArgs<TKey, TValue>()
{
Key = item.Key,
Value = item.Value
});
}
}

public void Clear()
{
Base.Clear();
if (OnClear != null)
{
OnClear(this, new EventArgs());
}
}

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

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

public int Count
{
get { return Base.Count; }
}

public bool IsReadOnly
{
get { return Base.IsReadOnly; }
}

public bool Remove(KeyValuePair<TKey, TValue> item)
{
var r = Base.Remove(item);
if (OnRemove != null)
{
OnRemove(this, new odKVPEventArgs<TKey, TValue>()
{
Key = item.Key,
Value = item.Value
});
}
return r;
}

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return Base.GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return Base.GetEnumerator();
}
}

No comments:

Post a Comment