Naming

Message name is the only condition to match subscribers to publishers.

Name Matching

MessageBus uses only name to match subscribers to publishers.

When publisher registering, each existing subscriber is checked to see whether the name matches the publisher. When subscriber registering, it is checked against each existing publisher for name matching and saved in pool for further publisher registering.

Matching Method

MessageBus supports multiple ways to check the message name.

Each publisher has one name specified exactly when registering. Each subscriber is supported to use multiple way to match one or more publishers by name. This demo presents multiple ways for name matching.

C#
public void TestMethod()
{
    using var bus = new MessageBus();
    var subscriberExact1Ticket = bus.RegisterSubscriber<string>("Hello", SubscriberExact1);
    var subscriberExact2Ticket = bus.RegisterSubscriber<string>("hello", SubscriberExact2);
    var subscriberIgnoreCaseTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithStringComparison("HELLO", StringComparison.OrdinalIgnoreCase), SubscriberIgnoreCase);
    var subscriberAllTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingAll(), SubscriberAll);
    var subscriberRegEx1Ticket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithRegularExpression("[Hh]ello"), SubscriberRegEx1);
    var subscriberRegEx2Ticket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithRegularExpression("hello"), SubscriberRegEx2);

    var text = "Hello World";

    var publisherTicket = bus.RegisterPublisher<string>("Hello", new MessageBusPublisherOptions<string>(true));
    publisherTicket.Executor.Execute(text);

    bus.UnregisterPublisher(publisherTicket);
    bus.UnregisterSubscriber(subscriberExact1Ticket);
    bus.UnregisterSubscriber(subscriberExact2Ticket);
    bus.UnregisterSubscriber(subscriberIgnoreCaseTicket);
    bus.UnregisterSubscriber(subscriberAllTicket);
    bus.UnregisterSubscriber(subscriberRegEx1Ticket);
    bus.UnregisterSubscriber(subscriberRegEx2Ticket);

    //after being executed: _exact1, _ignoreCase, _all and _regex1 are equal to text; _exact2 and _regex2 are not.
}

private string? _exact1, _exact2, _ignoreCase, _all, _regex1 , _regex2;

public void SubscriberExact1(string? argument)
{
    _exact1 = argument;
}

public void SubscriberExact2(string? argument)
{
    _exact2 = argument;
}

public void SubscriberIgnoreCase(string? argument)
{
    _ignoreCase = argument;
}

public void SubscriberAll(string? argument)
{
    _all = argument;
}

public void SubscriberRegEx1(string? argument)
{
    _regex1 = argument;
}

public void SubscriberRegEx2(string? argument)
{
    _regex2 = argument;
}

  Note

In the code above, the publisher is registered with IsAlwaysExecuteAll is on. This forces MessageBus to run through all matched subscribers. Check this topic for details.

Exact Matching

Matches base on the message name comparison exactly. This is the default behaviour.

When subscriber is registered with a message name specified, the string is used to compare with each publisher exactly.

C#
var subscriberExact1Ticket = bus.RegisterSubscriber<string>("Hello", SubscriberExact1);
var subscriberExact2Ticket = bus.RegisterSubscriber<string>("hello", SubscriberExact2);

The comparison is case sensitive. The first subscriber matches Hello, which is equal to the name provided by publisher, allowing to be executed when publisher requests. The second one matching hello, which is not equal to Hello, will not be executed by the publisher in this demo.

String Matching

When subscriber is registered with an instance of MessageNameMatchingWithStringComparison, StringComparison can be specified to be used in comparison. The default value is OrdinalIgnoreCase.

In this case, HELLO is matched with Hello, which is indicated with publisher registering, based on OrdinalIgnoreCase. This subscriber will be executed.

C#
var subscriberIgnoreCaseTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithStringComparison("HELLO", StringComparison.OrdinalIgnoreCase), SubscriberIgnoreCase);

All Matching

When an instance of MessageNameMatchingAll is provided, the subscriber hooks all publishers, including existing and to be registered later.

C#
var subscriberAllTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingAll(), SubscriberAll);

Regular Expression Matching

A regular expression wrapped by MessageNameMatchingWithRegularExpression declares to use expression specified in name matching.

C#
var subscriberRegEx1Ticket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithRegularExpression("[Hh]ello"), SubscriberRegEx1);

Expression [Hh]ello matches the value provided with publisher Hello while expression hello does not.

User Defined

When complex model is required, uses your own class derived from MessageNameMatcherBase. IsComplied is called for each message name to be checked.

See Also