Results 1 to 3 of 3

Thread: Creating interfaces for a messaging system

Threaded View

  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.


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