PC Review


Reply
Thread Tools Rate Thread

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

 
 
kristian.freed@gmail.com
Guest
Posts: n/a
 
      16th Nov 2006
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

 
Reply With Quote
 
 
 
 
Your_Persona
Guest
Posts: n/a
 
      16th Nov 2006
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/lib...e_members.aspx

Maby i'm not understanding your question compleately.

Have a great day!

Austin

On Nov 16, 2:18 am, kristian.fr...@gmail.com wrote:
> 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


 
Reply With Quote
 
Jeff Louie
Guest
Posts: n/a
 
      18th Nov 2006
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

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Events v. Delegates Siegfried Heintze Microsoft VB .NET 10 18th Jan 2008 11:24 PM
Custom Events versus Multicast Delegates for events(VB.NET) =?Utf-8?B?aGVyYmVydA==?= Microsoft Dot NET 2 14th Feb 2007 02:51 PM
when to use delegates and events DKode Microsoft C# .NET 4 22nd Sep 2005 01:03 AM
PinvokeDLL or delegates or ??? ... Calling C# delegates from C++ as CALLBACK Roland Rehmnert Microsoft Dot NET Compact Framework 1 20th Feb 2005 12:21 PM
Re: Events and Delegates A Microsoft C# .NET 0 16th Jul 2003 01:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:48 AM.