Search code examples
c#.netmasstransit

Is it a bad idea to have a MassTransit consumer that handles multiple message types?


Let's assume there are 3 domain events (messages) in a MassTransit application: IEvent1, IEvent2, IEvent3. Each event captures an occurence of smt. that happened in the domain. There are separate consumers (implementing the IConsumer<> interface) for each event type, and each consumer is responsible for its own part of the business logic.

Would it be appropriate in some situation to have a single consumer class handle three events - i.e. implement three interfaces:

class GenericConsumer: IConsumer<IEvent1>, IConsumer<IEvent2>, IConsumer<IEvent3>

For example, a consumer that performs some kind of audit or logging routine, so that all three messages are handled similarly: all messages are written to another database, log file, sent to an external message bus, etc. Or should it still be 3 different consumer classes each implementing a single IConsumer<> interface?

Will there be any performance or maintenance issues related to the fact that a single consumer implements several interfaces?


Solution

  • It's a great idea, actually. And encouraged when messages are closely related – such as recording audit events into an audit store. In fact, I've seen audit consumers with more than twenty IConsumer<T> methods without any issues.

    Bonus points for using a batch consumer to receive multiple at once to more efficiently write them to storage.