zarusz/SlimMessageBus

Support for configuring polymorphic messages (inheritance)

Closed this issue · 1 comments

Consider the following message types and inheritance:

public abstract class BaseMessage
{
  public DateTime Created { get; set; }
}

public class CustomerEvent : BaseMessage
{
   public Guid CustomerId { get; set; }
}

public class CustomerCreatedEvent: CustomerEvent { }
public class CustomerChangedEvent: CustomerEvent { }

All of these messages are serialized by Newtonsoft.Json, and thus can be serialized/deserialized into the same topic. The Json serializer is able to infer the proper message type using the $type property.

Now, using SMB I can send these messages:

await bus.Publish(new CustomerCreatedEvent { CustomerId = Guid.NewGuid() });
await bus.Publish(new CustomerChangedEvent { CustomerId = Guid.NewGuid() });

However, I need to configure each message separately:

   MessageBusBuilder mbb = ...;
   mbb
      .Produce<CustomerEvent>(x => x.DefaultTopic("customer-events"))
      .Produce<CustomerCreatedEvent>(x => x.DefaultTopic("customer-events"))
      .Produce<CustomerChangedEvent>(x => x.DefaultTopic("customer-events"))

It would be better, if we could just configure the CustomerEvent and have any subclass of that message follow the same config, eg:

   mbb
      // will apply to CustomerCreatedEvent and CustomerChangedEvent
      .Produce<CustomerEvent>(x => x.DefaultTopic("customer-events"))