A toolset for Observer pattern. Interfaces, pipelines, translators.
$ dotnet add package ProSol.MessagingImplements a message broker with ability to build a pipeline of listeners.
The project contains an API for the Observer pattern: IPublisher, ISubscriber, and PipelinePublisher.
IPublisher interface is aimed to provide a layer of absctraction to the client.ISubscriber interface decalres a subscription contract with chained calls support via NextDelegate.
NextDelegate: is a mandatory delegate to trigger if current subscriber wants to bypass a message to next subscriber.PipelinePublisher: a ready-to-use implementation for chained subscriptions.Let's see how works in a console app, by making a publisher for strings, which addresses the message to the StringListener, but there is a StringLimitListener between them.
So, the StringLimitListener will drop the message longer than "Please" string:
using ProSol.Messaging;
var maxLength = "Please".Length + 1;
var broker = new MessageBroker<string>();
// Set up a chain.
broker.Subscribe(new StringLimitListener(maxLength));
broker.Subscribe(new StringListener());
// Push some messages.
broker.Publish("Please");
broker.Publish("STOP");
broker.Publish("The planet");
// OUTPUT:
// Please
// STOP
// Clean up.
broker.Complete();
public class StringListener : ISubscriber<string>
{
public void OnCompleted() { }
public void OnNext(string message, NextDelegate next)
{
Console.WriteLine(message);
if (message != "STOP")
{
next();
}
}
}
public class StringLimitListener(int limit) : ISubscriber<string>
{
private readonly int limit = limit;
public void OnCompleted() { }
public void OnNext(string message, NextDelegate next)
{
if (message.Length < limit)
{
next();
}
}
}3.6K