With Notification: Collection

time to read 2 min | 252 words

I needed to create a class with a collectiont that would raise events when items where added or removed to it. It was the work of a few minutes to add the events and then write this code:

outputs = new EntitySet<IConnection>(

      delegate(IConnection connection) { Raise(OutputAdded, connection); },

      delegate(IConnection connection) { Raise(OutputRemoved, connection); }

      );

Where Raise() looks like this:

private void Raise(EventHandler<ConnectionEventArgs> eventToRaise, IConnection connection)

{

      ConnectionEventArgs args = new ConnectionEventArgs(connection);

      if (eventToRaise != null)

            eventToRaise(this, args);

}

It starting too look like O/RM and frieds are the first tools that I pool out of the toolbox.