MicroLinq NotifyingCollection Published

I’ve been pondering if/how to implement an Observable Collection type class in the MicroLinq project for NETMF. Tonight I finally set about doing it.

I started with the ValueTypeArrayList class, removed the type restriction and switched storage to just plain old object[]. I took a look at the current ObservableCollection in the full framework and set about deciding what was a must-have and what could be tossed away to save memory and footprint.

It was obvious the CollectionChanged event was necessary but I threw away the PropertyChanged event. It just didn’t seem to provide anything useful in this case. So, concentrating just on the CollectionChanged event, I looked at the EventArgs and saw a lot of code which basically provides a read-only copy of the old and/or new collection (before the modification and after) depending on the change type. I cut this first because I just couldn’t justify the memory usage.

That left only the Action type of which there are 5 in the full framework. Only 3 could ever be reported from my implementation so I cut the other two leaving just Add, Remove and Reset. I kept the class and interface names the same as the full framework in case there’s ever a native implementation. It should be trivial to port code that makes use of NotifyingCollection now this way.

Once all this was settled the implementation was fast. I added a little private function to fire the event if there were any listeners providing the change type that was made. My Add/Insert, Remove/RemoveAt and Clear methods got a call to the new helper.

I compiled up the project, checked to make sure there wasn’t anything obviously wrong with the size of the binaries and tested it out (in a limited fashion) to see events thrown as expected when changes were made!

The reason I wanted to implement this was for data logging applications or other uses where pieces of information would be coming in at unknown times over a longer period. Rather than looping a check on the size, you can just attach an event handler and react only when necessary and free up clock cycles for other operations.

Sample Usage

In body somewhere…
NotifyingCollection watchable = new NotifyingCollection(10);
watchable.CollectionChanged += new
    NotifyCollectionChangedEventHandler
        (watchable_CollectionChanged);

watchable.Add(5);
watchable.Add(8);
watchable.Insert(0, 9);
watchable.Remove(8);
watchable.Add(11);

Debug.Print(string.Empty);
Debug.Print("Current values:");
foreach (object i in watchable)
{
   Debug.Print(i.ToString());
}
Debug.Print(string.Empty);

watchable.Clear();
And the event handler…
static void watchable_CollectionChanged(object sender,
                    NotifyCollectionChangedEventArgs e)
{
   Debug.Print("WATCHABLE UPDATED! Type: " + e.Action);
}

Sample Output

WATCHABLE UPDATED! Type: 0
WATCHABLE UPDATED! Type: 0
WATCHABLE UPDATED! Type: 0
WATCHABLE UPDATED! Type: 1
WATCHABLE UPDATED! Type: 0

Current values:
9
5
11

WATCHABLE UPDATED! Type: 2

Looking forward to seeing how people use it!

Excitedly,
-Erik

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s