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
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