Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kristian.freed@gmail.com - 16 Nov 2006 09:18 GMT
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
Your_Persona - 16 Nov 2006 09:38 GMT
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

On Nov 16, 2:18 am, kristian.fr...@gmail.com wrote:
> Hi,
>
[quoted text clipped - 45 lines]
>
> / Kristian
Jeff Louie - 18 Nov 2006 21:56 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.