|
-
May 8th, 2009, 03:02 PM
#1
Thread Starter
Not NoteMe
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:
public class Message
{
public object Sender { get; set; }
public string Name { get; set; }
public Message(object sender, string name) { Sender = sender; Name = name; }
}
public class GUIMessage : Message
{
public string Control { get; set; }
public GUIMessage(object sender, string name, string control)
: base(sender, name) { Control = control; }
}
public class MouseMessage : Message
{
public Point Pos { get; set; }
public MouseMessage(object sender, string name, Point pos)
: base(sender, name) { Pos = pos; }
}
public interface IMessageDispatcher
{
List<IMessageListener<Message>> Listeners { get; set; }
List<Message> Messages { get; set; }
}
public static class MessageDispatcherExtensionMethods
{
public static void PostMessage(this IMessageDispatcher MessageDispatcher, Message Msg)
{
MessageDispatcher.Messages.Add(Msg);
}
public static void DispatchMessages<TArgs>(this IMessageDispatcher MessageDispatcher) where TArgs : Message
{
foreach (TArgs Msg in MessageDispatcher.Messages)
{
foreach (IMessageListener<TArgs> Listener in MessageDispatcher.Listeners)
{
Listener.HandleMessage(Msg.Sender, Msg);
}
}
MessageDispatcher.Messages.Clear();
}
public static void AddListener<TArgs>(this IMessageDispatcher MessageDispatcher, IMessageListener<TArgs> MessageListener) where TArgs : Message
{
MessageDispatcher.Listeners.Add(MessageListener);
}
}
public interface IMessageListener<TArgs> where TArgs : Message
{
void HandleMessage(object sender, TArgs Args);
}
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. 
-
May 8th, 2009, 04:40 PM
#2
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
-
May 8th, 2009, 04:44 PM
#3
Thread Starter
Not NoteMe
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|