Advanced use of delegates to automatically disconnect delegates from a set of events

K

kristian.freed

Hi,

I currently work in a project written fully in C# where we make
extensive use of delegates and events. We have a model where a "state",
an object holding data but not much code but which fires events when
the data changes, is often the central part. Connected to these states
are various observers that act on changes in data, by altering the
information presented to the user, executing code and so on, each
observer with its own function.

A situation that occurs very frequently because of this design is the
need to connect or disconnect an observer to/from a state. The connect
method will usually just hook a number of events, and the disconnect
method should unhook each of these events so the observer can be used
again on another state or be garbage collected. A repetitive and error
prone task is thus to make sure that these two methods are in sync. To
help with this, I would like to create a class with the responsibility
of keeping track of all events that have been connected, and when
requested disconnect them all. The interface would look something like
this:

interface IDelegateConnector
{
void ConnectDelegate<T>(T evt, T del) where T : Delegate;
void DisconnectAll();
}

Where you would connect delegate del to event evt. You can however not
use Delegate as base class constraint to generic methods, so instead it
would have to be something like this:

interface IDelegateConnector
{
void ConnectDelegate(DelegateType1 evt, DelegateType1 del);
void ConnectDelegate(DelegateType2 evt, DelegateType2 del);
void ConnectDelegate(DelegateType3 evt, DelegateType3 del);

void DisconnectAll();
}

where I manually create one overload for each delegate/event type that
I am interested in connecting this way. There are a number of problems
with these interfaces and their implementation, but this should be
enough to present the idea of what I want.

Does anyone know how this would be possible to implement or know of
another method to simplify my event handling code?

/ Kristian
 
Y

Your_Persona

Have you had a look into multicast delegates?
MulticastDelegate Members :
CombineImpl :Overridden. Combines this Delegate with the specified
Delegate to form a new delegate.
RemoveImpl :Overridden. Removes an element from the invocation list of
this MulticastDelegate that is equal to the specified delegate.
....
....
"
MulticastDelegate is a special class. Compilers and other tools can
derive from this class, but you cannot derive from it explicitly. The
same is true of the Delegate class.

A MulticastDelegate has a linked list of delegates, called an
invocation list, consisting of one or more elements. When a multicast
delegate is invoked, the delegates in the invocation list are called
synchronously in the order in which they appear. If an error occurs
during execution of the list then an exception is thrown.

"
http://msdn2.microsoft.com/en-gb/library/system.multicastdelegate_members.aspx

Maby i'm not understanding your question compleately.

Have a great day!

Austin
 
J

Jeff Louie

Well your question made me curious so I coded this as an experiment:

public abstract class MCV
{
public delegate void ClearHandler(object source, EventArgs
e);
public delegate void RefreshHandler(object source, EventArgs
e);
public delegate void EnableHandler(object source, EventArgs
e);
public delegate void DisableHandler(object source,
DisableEventArgs e);
public class DisableEventArgs : EventArgs
{
public readonly string Message = "";
public DisableEventArgs(string message)
{
this.Message = message;
}
}
public interface IViewable
{
bool Clear();
bool Refresh();
bool Enable();
bool Disable(string message);
}
public interface IViewableEvents
{
MCV.DisableHandler GetDisableHandler();
MCV.EnableHandler GetEnableHandler();
MCV.ClearHandler GetClearHandler();
MCV.RefreshHandler GetRefreshHandler();
}
} // end MCV class

The controller class fires events to all registered Views using
IViewableEvents, but each View can be individually disabled using
IViewable. A disabled View will ignore notify and refresh events.

http://www.geocities.com/jeff_louie/oop32.htm

Regards,
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top