MessageHandler

How to design events

E

To design an event, you first need to define its schema, represented as a class in C#.

The name of an event should always be representing a decssion taken in the past.

public class BookingStarted
{

}

Sourcing from events

Events can be used in context of event sourcing, in such a case you derive the event from SourcedEvent, which can be found in the MessageHandler.EventSourcing.Contracts package.

PM> Install-Package MessageHandler.EventSourcing.Contracts
public class BookingStarted : SourcedEvent
{
   
}

The SourcedEvent base class adds a number of technical properties, which the event sourcing framework can use to order, group, split, branch and merge these events in the context of an event stream.

Event carried state

Usually events carry some state as well.

Care should be taken to limit the amount of state carried per event to the minimum.

There are two reasons for this:

  • Event storage and event transport technologies do have their technical size limitations
  • But also, the larger the schema of a state property, the harder it becomes to refactor the consumption of this state over time.

We recommend to add any required state, to the event where the state is received

public class BookingStarted : SourcedEvent
{
    public PurchaseOrder PurchaseOrder { get; set; }
}

And in future events, you merely add a reference to the state.

public class BookingConfirmed : SourcedEvent
{
    public string PurchaseOrderReference { get; set; }
}

Sign up to our newsletter to get notified about new content and releases

You can unsubscribe at any time by clicking the link in the footer of your emails. I use Mailchimp as my marketing platform. By clicking subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.