2
votes

Given the following event, is there a way to subscribe to events that have a specific property value? For example, only subscribe to events with an OperationTypeId=3.

public interface IServerEvent : IEvent
{
    int OperationTypeId { get; set; }
    string SomeValue { get; set; }
}

I am using NServiceBus 6.0.0.

The use case for this is that new OperationType's can be created on the Server so we need a "generic" event structure. I'd like the ability for Subscribers to only receive events for the OperationType's they are interested in and not require the publisher to send all events to all subscribers and make the subscriber filter.

I am hoping the publish side of NServiceBus can handle the filter to reduce the number of events that are published.

3

3 Answers

4
votes

What's the downside of subscribing to the event, acting only on the ones that match your criteria and ignoring the rest?

If you are in control of the publishing side and if those events with a specific value mean something to the business, maybe you need to break that generic event and publish a finer grain ones?

As to why NServiceBus doesn't support content-based routing, maybe this would be a good read from the original author?

TL;DR "because it is a dangerous pattern that should usually be avoided"

1
votes

Not out of the box - but if you are using RabbitMq for your backend you can use IRoutingTopology to route those specific messages with a routing key of OperationTypeId. You can then have the same topology on your subscribers to subscribe to only specific routing keys

1
votes

Brian, The previous answers in correct in that typically you would differentiate the different content by type or interface etc. and subscribe only to that type so the publisher can differentiate.

Another thought might be around whether these are events? If you're stuck with a generic message where the content cannot be strongly typed and can change on the fly, you might want to abandon the built in type based routing and manage the message dispatch on your own. You could then just use commands and dispatch them however you wish using a content based router on the OperationTypeId in your example.

Newer still there are some customer routing capabilities in version 6 that would allow you some potential flexibility here: see this article and example:https://docs.particular.net/samples/routing/custom/

Regards, Joe