Update: Postal.NET is now available in Nuget.
Postal.NET allows us to post messages to a “bus”, identified by a channel and topic pair. A channel can have any number of topics, and a topic can be in any number of channels, these are just strings.
We can subscribe to one or more channel/topic pairs and cancel each subscription when we no longer need it.
Messages are either posted synchronously or asynchronously. Each message is delivered “as-is”, but it is wrapped in an envelope that includes a couple of properties, like, the timestamp and the origin channel and topic.
Nothing too complex, yet, I hope you find it useful!
Some usages, first, a subscription and two messages being sent, one synchronously (Publish) and the other asynchronously (PublishAsync):
Notice the using block that wraps a Subscribe call. Subscribe returns an IDisposable, which, when disposed of, terminates the subscription, hence the third message published will not be caught by the subscription.
Now we have two subscriptions, one for * (any) channel and topic, and the other for a concrete one. Both subscriptions will receive the message.
This time we are filtering the subscription: it will only fire if the message payload is an integer.
In this example, we wait for an event to be signaled, which only happens once an asynchronous message is handled.
This final example shows the alternative fluent interface. It offers the exact same functionality.
Let’s start by the Postal class, which merely acts as an holder for an IBox:
The IBox interface, the core of Postal.NET, is defined as:
The included implementation, Box, is as follows:
And finally, the Envelope class:
The code for the fluent extensions will not be covered here, but you can find it in GitHub.
Postal.NET will not:
serialize messages or send them across process boundaries;
apply any scheduling, messages are sent immediately;
apply any transformation to the messages;
keep sent messages internally.
But it will:
keep the order by which messages were sent;
send messages to multiple channels and topics, if the * is used;
send any kind of data, it really doesn’t matter.
In the GitHub repository you will find some other projects:
PostalConventions.NET: using conventions and a fluent interface for automatically inferring the channel and topic from the message itself.
I have a couple of ideas, but, for now, I want to keep this simple. I will update the repository as I make progress. I’d like to hear from you on this.
Postal.NET will hopefully let you build more decoupled applications more easily. I hope you got the idea and you see value in it. As always, I’m always happy to hear your comments!