Exemplo n.º 1
0
        public void UpdateBy(IEnumerable <KeyValuePair <TKey, TItem> > source)
        {
            foreach (var kvp in source)
            {
                TItem currentValue;
                if (currentValues.TryGetValue(kvp.Key, out currentValue))
                {
                    if (currentValue.Equals(kvp.Value) == false)
                    {
                        currentValues[kvp.Key] = kvp.Value;
                        OnUpdated.Raise(kvp.Key, kvp.Value);
                    }
                }
                else
                {
                    currentValues[kvp.Key] = kvp.Value;
                    OnAdded.Raise(kvp.Key, kvp.Value);
                }
            }

            var keysRemoved = currentValues.Keys.Except(source.Select(kvp => kvp.Key)).ToArray();

            foreach (var keyRemoved in keysRemoved)
            {
                OnRemoved.Raise(keyRemoved, currentValues[keyRemoved]);
                currentValues.Remove(keyRemoved);
            }
        }