This repository has been archived; the project is not maintained anymore.
Sendstorm is a portable pub/sub framework for .NET based solutions.
- Subscribe / Broadcast
- Generic message types
- Weakly referenced subscribers
- Subscription filters
- Execution targets
- .NET 4.5 and above
- Windows 8/8.1/10
- Windows Phone Silverlight 8/8.1
- Windows Phone 8.1
- Xamarin (Android/iOS/iOS Classic)
- .NET Standard 1.0/2.0
When you want to subscribe to an event, you have to implement the IMessageReceiver
interface in your subscriber class.
class Foo : IMessageReceiver<FooMessage>
{
public void Receive(FooMessage message)
{
//do something with the message
}
}
Then you can subscribe your class as a listener to that specific event.
var messagePublisher = new MessagePublisher();
var foo = new Foo();
messagePublisher.Subscribe<FooMessage>(foo);
Now you can broadcast messages to your subscribers.
messagePublisher.Broadcast<FooMessage>(new FooMessage());
You are also able to unsubscribe if you don't want to receive messages anymore.
messagePublisher.UnSubscribe<FooMessage>(foo);
If you want to receive the messages conditionally you can specify a filter for your subscription which's parameter will be the message object.
messagePublisher.Subscribe<FooMessage>(new Foo(), fooMessage => false);
This sample above will completely prevent the
Foo
object from recieving anyFooMessage
, in a real scenario here you can check against some properties of the message object, or the state of your subscriber class.
You can also specify where you want to delegate your messages.
messagePublisher.Subscribe<FooMessage>(new Foo(), fooMessage => false, ExecutionTarget.BackgroundThread);
Available options are:
- BroadcastThread (it'll delegate the receive call to the thread from where the broadcast was called)
- BackgroundThread (it'll create a task and will let the ThreadPool schedule the execution of the receive call)
- Synchronized (it'll delegate the receive call to the captured
SynchronizationContext
)
The Synchronized option only works when the
MessagePublisher
is able to collect a validSynchronizationContext
object for delegating calls via it'sSynchronizationContext.Current
static property. To achieve this you have to instantiate it on a thread where theCurrent
property is properly set.