NServiceBus Home

FAQ Contents

How do I define a message?

public class MyMessage : IMessage { }

OR

public interface IMyMessage : IMessage { }

One of the advantages of using interfaces to define messages instead of classes is that you get "multiple inheritance" - one message can extend multiple other messages.

This is useful for solving a specific class of versioning problems.

Say that your business logic represents a state machine with states X and Y. When your system gets into state X, it publishes a message - EnteredStateX. When your system gets into state Y, it publishes a message - EnteredStateY. (For more information on how to publish a message, see below)

In the next version of your system, you add a new state Z, which represents the co-existence of both X and Y. So, you define a message EnteredStateZ which inherits both EnteredStateX and EnteredStateY.

When your system publishes EnteredStateZ, clients subscribed to either (or both) EnteredStateX or EnteredStateY will be notified.

Without the ability to have a message extend multiple others, you'd have to use composition. This would prevent the infrastructure from knowing how to automatically route messages to pre-existing subscribers of the composed messages.

Legal

“NServiceBus” is among the trademarks of NServiceBus Ltd. All other product and company names and marks mentioned are the property of their respective owners and are mentioned for identification purposes only.

© 2010-2012 NServiceBus Ltd. All rights reserved.