ObservableCollection Extensions

I’ve had to create a couple of Extensions for the ObservableCollection, (well – I didn’t have to), erm, and so here they are! All are ‘AddRange’ just with different args for different usages..

namespace Extensions
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
 
    /// <summary>Extension methods for the <see cref="ObservableCollection{T}"/> class.</summary>
    public static class ObservableCollectionExtensions
    {
        /// <summary>Adds a range of items to an <see cref="ObservableCollection{T}"/>.</summary>
        /// <typeparam name="T">The type held in the <see cref="ObservableCollection{T}"/>.</typeparam>
        /// <param name="observableCollection">The <see cref="ObservableCollection{T}"/> instance to add the items to.</param>
        /// <param name="items">The items to add the <see cref="ObservableCollection{T}"/>.</param>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="observableCollection"/> given is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="items"/> given is null.</exception>
        public static void AddRange<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> items)
        {
            Throw.IfNull("observableCollection", observableCollection);
            Throw.IfNull("items", items);
 
            foreach (T item in items)
                observableCollection.Add(item);
        }
 
        /// <summary>Adds a range of items to an <see cref="ObservableCollection{T}"/>.</summary>
        /// <typeparam name="T">The type held in the <see cref="ObservableCollection{T}"/>.</typeparam>
        /// <param name="observableCollection">The <see cref="ObservableCollection{T}"/> instance to add the items to.</param>
        /// <param name="items">The items to add the <see cref="ObservableCollection{T}"/>.</param>
        /// <param name="filter">A filter to use to select specific values from the <paramref name="items"/>.</param>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="observableCollection"/> given is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="items"/> given is null.</exception>
        public static void AddRange<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> items, Func<T, bool> filter)
        {
            Throw.IfNull("observableCollection", observableCollection);
            Throw.IfNull("items", items);
 
            foreach (T item in items.Where(filter))
                observableCollection.Add(item);
        }
 
        /// <summary>Adds a range of items to an <see cref="ObservableCollection{T}"/>.</summary>
        /// <typeparam name="T">The type held in the <see cref="ObservableCollection{T}"/>.</typeparam>
        /// <param name="observableCollection">The <see cref="ObservableCollection{T}"/> instance to add the items to.</param>
        /// <param name="items">The items to add the <see cref="ObservableCollection{T}"/>.</param>
        /// <param name="filter">A filter to use to select specific values from the <paramref name="items"/>.</param>
        /// <param name="transform">A transform to apply to each of the <paramref name="items"/>.</param>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="observableCollection"/> given is null.</exception>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="items"/> given is null.</exception>
        public static void AddRange<T>(this ObservableCollection<T> observableCollection, IEnumerable<T> items, Func<T, bool> filter, Func<T, T> transform)
        {
            Throw.IfNull("observableCollection", observableCollection);
            Throw.IfNull("items", items);
 
            foreach (T item in items.Where(filter))
                observableCollection.Add(transform(item));
        }
    }
}

Where ‘Throw’ is defined as:

public static class Throw
{
    /// <summary>Throws a <see cref="ArgumentNullException"/> if the given <paramref name="argumentValue"/> is null.</summary>
    /// <param name="argumentName">Name of the argument being checked.</param>
    /// <param name="argumentValue">The <see cref="object"/> argument value being checked.</param>
    /// <param name="extraMessages">Any extra user defined messages to add to the <see cref="ArgumentException.Message"/>.</param>
    /// <exception cref="ArgumentNullException">Thrown if the <paramref name="argumentValue"/> given was null.</exception>
    public static void IfNull(string argumentName, object argumentValue, params string[] extraMessages)
    {
        if(argumentValue != null)
            return;
 
        throw new ArgumentNullException(argumentName, MergeMessages("Argument was null.", extraMessages));
    }
}

But that’s just to add code brevity.

Print | posted @ Thursday, August 5, 2010 5:29 AM

Comments on this entry:

Gravatar # re: ObservableCollection Extensions
by Matthew Abbott at 8/5/2010 8:09 AM

See Throw is getting some life ;)

Would it be better to build the extensions on ICollection<T> instead? I know you work mostly with WPF which sees heavy usage of ObservableCollection<T>, but those methods need only an ICollection<T> instance passed in...?
Gravatar # re: ObservableCollection Extensions
by Chris at 8/5/2010 8:20 AM

Throw has grown and become one with me :)
You are of course correct, ICollection would have been a better class to use.

Sometimes you can't see the wood for the ObservableCollections....
Post A Comment
Title:
Name:
Email:
Comment:
Verification: