473,508 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Nov 16 '06 #1
2 2330
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
Nov 16 '06 #2
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 ***
Nov 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1358
by: BlueMonkMN | last post by:
I've been trying to think of the right way to design relationships between objects with different desired lifetimes that raise events. If an event source is a relatively permanent object and the...
4
22855
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
15
6602
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
8
1744
by: Nicky Smith | last post by:
Hello, I'm reading Mike Gunderloy's Mcad Vb.net book, and I've also read the MS press Mcad book for the same topic ".. Windows based applications with VB.net" for exam 70-306. In the...
1
2106
by: lallous | last post by:
Hello I am not very experienced with this issue and would your opinion on whether it is healthy or not: 1) I created an ATL COM object (say MyCOM): - that supports connection points - that...
0
13067
ADezii
by: ADezii | last post by:
Not all ADO Power Users realize that there are Events, specifically related to the Connection and Recordset Objects, for which they can write code for. If one is aware of these Events, it is not...
7
3406
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
9
1880
by: Scott Gifford | last post by:
I have a question about concurrency and delegates. Say I have a class roughly like this: public class Class1 { public delegate void MyEventHandler(); private event MyEventHandler onMyEvent;...
69
5516
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.