Is there annotations to add meta data to events/commands? Any example code would be highly appreciated.
There are not annotations to add metadata to a command or event message.
To add metadata to a command you’d dispatch or an event you’d publish, you can do a couple of things:
- Use the CommandMessage or EventMessage objects to wrap your command/event payload in. These interface provide the ‘withMetaData’ and ‘andMetaData’ functions to respectively replace the existing metadata or merge new metadata into the message. Down side of this approach is that you’ll need to do this for every message separately.
- Upon publishing messages from your Aggregates, the AggregateLifecycle.apply function allows you to provide an event and it’s metadata. This however again has the downside you’ll have to do this for every event separately.
- The more robust solution which will cover all your messages, is to use MessageDispatchInterceptors. MessageDispatchInterceptors will intercept your command/event/query message prior to dispatching it over the bus. This process allows you to perform for example structural validation or add MetaData to every message you send. I suggest having a look at the ref. guide pages for command interceptors, event interceptors and query interceptors for more specifics on this.
Hope this helps you out Michael!