Results 1 to 3 of 3

Thread: Creating interfaces for a messaging system

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Creating interfaces for a messaging system

    Ok, i've been going round in circles for a few days now.
    What i want is to allow classes in my program to listen for messages, as well as send messages out. These messages could be sent over the network, saved to disk or delt with instantly etc.

    In any program there may be several message dispatchers (handling multiple message types), several message listeners (listening for multiple message types).

    The overall problem i'm coming across is how to define all the interfaces, classes and extension methods to facilitate this.

    Here is the code i've got so far:

    csharp Code:
    1. public class Message
    2.     {
    3.         public object Sender { get; set; }
    4.         public string Name { get; set; }
    5.         public Message(object sender, string name) { Sender = sender; Name = name; }
    6.     }
    7.     public class GUIMessage : Message
    8.     {
    9.         public string Control { get; set; }
    10.         public GUIMessage(object sender, string name, string control)
    11.             : base(sender, name) { Control = control; }
    12.     }
    13.  
    14.     public class MouseMessage : Message
    15.     {
    16.         public Point Pos { get; set; }
    17.         public MouseMessage(object sender, string name, Point pos)
    18.             : base(sender, name) { Pos = pos; }
    19.     }
    20.  
    21.  
    22.  
    23. public interface IMessageDispatcher
    24.     {
    25.         List<IMessageListener<Message>> Listeners { get; set; }
    26.         List<Message> Messages { get; set; }
    27.     }
    28.  
    29.     public static class MessageDispatcherExtensionMethods
    30.     {
    31.         public static void PostMessage(this IMessageDispatcher MessageDispatcher, Message Msg)
    32.         {
    33.             MessageDispatcher.Messages.Add(Msg);
    34.         }
    35.  
    36.         public static void DispatchMessages<TArgs>(this IMessageDispatcher MessageDispatcher) where TArgs : Message
    37.         {
    38.             foreach (TArgs Msg in MessageDispatcher.Messages)
    39.             {
    40.                 foreach (IMessageListener<TArgs> Listener in MessageDispatcher.Listeners)
    41.                 {
    42.                     Listener.HandleMessage(Msg.Sender, Msg);
    43.                 }
    44.             }
    45.             MessageDispatcher.Messages.Clear();
    46.         }
    47.  
    48.         public static void AddListener<TArgs>(this IMessageDispatcher MessageDispatcher, IMessageListener<TArgs> MessageListener) where TArgs : Message
    49.         {
    50.             MessageDispatcher.Listeners.Add(MessageListener);
    51.         }
    52.     }
    53.  
    54.  
    55.  
    56. public interface IMessageListener<TArgs> where TArgs : Message
    57.     {
    58.         void HandleMessage(object sender, TArgs Args);
    59.     }


    The problem is that it seems i can't have a message dispatcher that contains a list of all messages since c# doesn't support covarence i.e. i can't add an IMessageListener<GUIMessage> to my List<IMessageListener<Message>> Listeners, even though GUIMessage is derived from Message.

    Am i going to have to make the IMessageDispatcher generic too, and have my classes impliment each one seperately or is there a more elegent solution?

    Cheers for any help, i've been banging my head against the wall for a while now!
    Last edited by penagate; May 9th, 2009 at 05:55 AM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Creating interfaces for a messaging system

    sometimes it isn't about what you know... but remembering where you read about it...

    I thought it sounded familiar.... I was just reading (OK, so I glossed over it at the time, but it did register) about covarence in Visual Studio Magazine.... here's a link to the article... I don't know how much, if any, help it will be .... http://visualstudiomagazine.com/colu...torialsid=3097

    Oh... dang... upon further review, I see now that it's going on about C#4.0 .. still, there might be something.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Creating interfaces for a messaging system

    Thanks for the link. I've already read about c# 4.0's support for it, but i'm starting to think i'll have to go with the method of making the IMessageDispatcher generic then having each class implement the generic interfaces explicitly.

    It's a shame as i wanted to put as much code into a library as possible, leaving the classes that implement the interfaces as clean as possible.

    If anyone knows of a workaround, or can think of a better way of doing this i'm all ears!
    Cheers!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width