Interface ISubscriber<TMessage> +
+Interface representing subscribers to messages sent over a MessageBus. Classes wishing to respond to one or more message types as they are sent across +the bus should implement this interface.
+Namespace: GoRogue.Messaging
+Assembly: GoRogue.dll
+Syntax
+public interface ISubscriber<TMessage>
+ Type Parameters
+Name | +Description | +
---|---|
TMessage | +The type of message that the subscriber wants to handle. Any and all messages sent over the event bus you subscribe to +that can cast to this type will be passed the Handle(TMessage) function when they are sent. + |
+
Remarks
+It is possible to have one class handle two different event types, by having it implement multiple ISubscriber types, passing different types as TMessage.
+When this is performed, however, the compiler will be unable to automatically resolve the template parameter of RegisterSubscriber<TMessage>(ISubscriber<TMessage>),
+so it will need to be specified manually. Further, the RegisterSubscriber function will need to be called once for each ISubscriber type the class implements.
+
+
+class MultipleSubscriber : ISubscriber<string>, ISubscriber<string[]>
+{
+ /* Explicit interface definitions are not required but are recommened for code clarity
+ void ISubscriber<string>.Handle(string message) => Console.WriteLine(message);
+ void ISubscriber<string[]>.Handle(string[] message) => Console.WriteLine(message.ExtendToString());
+}
+
+/* Later, when we add the subscriber to our message bus, we add each subscriber interface seperately */
+var messageBus = new MessageBus();
+var multiSubber = new MultipleSubscriber();
+messageBus.RegisterSubscriber<string>(multiSubber);
+messageBus.RegisterSubscriber<string[]>(multiSubber);
Methods +
+ + | + Improve this Doc + + + View Source + + +Handle(TMessage)
+Function that should handle the specified type of message in whatever manner it needs to. Called automatically any time a message is sent +over an event bus this subscriber has been registered on.
+Declaration
+void Handle(TMessage message)
+ Parameters
+Type | +Name | +Description | +
---|---|---|
TMessage | +message | +Message that was sent. + |
+