Auditing userId and creation/modification date

233 views
Skip to first unread message

Lukáš Vasek

unread,
Feb 8, 2016, 11:44:26 AM2/8/16
to Axon Framework Users
Hello,
I'm thinking about audit attributes in our domain. What we basically need is to have informations about who and when created/modified entities.
I've read about auditing-interceptor but I'm not sure if I understand it correctly.

Let's say I have command in which I don't have any informations about user/date. So my best possibility is to add attributes (userId and currentTimestamp) to my command and fill those informations inside  AuditingInterceptor (I'm using spring so I'd like to get informations from within spring beans)?
Then also extend my events with same attributes and implement MetaDataMutatingUnitOfWorkListenerAdapter?


Or is this approach wrong?

Thanks

René de Waele

unread,
Feb 8, 2016, 12:40:12 PM2/8/16
to axonfr...@googlegroups.com
Hi,

A better approach would be to add contextual data like that as metadata to the CommandMessage. Axon can then copy over the metadata to event messages that result from your command. 

Use a CommandDispatchInterceptor to add the metadata to your command and an AuditDataProvider to copy the metadata to each event that gets published. 

Best,
Rene
--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lukáš Vasek

unread,
Feb 8, 2016, 2:36:50 PM2/8/16
to Axon Framework Users
Hi Rene,
so basically I need to extend each event handler (where I need audit attributes) with MetaData attribute and get those required data from within it?

Thanks

Lukas

Lukáš Vasek

unread,
Feb 8, 2016, 2:41:43 PM2/8/16
to Axon Framework Users
Can I also have MetaData in Sagas?

René de Waele

unread,
Feb 8, 2016, 3:20:18 PM2/8/16
to axonfr...@googlegroups.com
Hi Lukas,

Not sure I'm understanding correctly but if you're asking how the metadata of an event can be read by an event handler then this is the way to go:

In the @EventHandler annotated method add a parameter annotated with @MetaData(key) to retrieve the metadata value mapped to the given key. Alternatively you can obtain the complete MetaData object by adding a MetaData parameter to your event handler method. 

You can also do this for @SagaEventHandler annotated methods in sagas.

Rene
--

Lukáš Vasek

unread,
Feb 9, 2016, 6:48:22 AM2/9/16
to Axon Framework Users
Hi Rene,
I understand that I need to add MetaData attribute to the @EventHandler ( or @EventSourcingHandler or @SagaEventHandler). This way I can get metadata inside this methods. If I don't add this attribute are MetaData persisted anyway? 

for example  
MyEvent is published and catch inside @EventSourcingHandler, @SagaEventHandler and finally @EventHandler. I don't need access to metadata inside @EventSourcingHandler and @SagaEventHandler but I will need it inside @EventHandler (to add attributes to query model).
Will be metadata available inside saga repository,  in event sourcing repository and finally in eventhandler (in @EventHandler I will add
handle(MyEvent event, MetaData metadata)
)?

René de Waele

unread,
Feb 9, 2016, 11:24:51 AM2/9/16
to axonfr...@googlegroups.com
Yes, the MetaData always gets persisted in the domain event entry table. It does not matter if there are handlers for it or not.
--

Lukáš Vasek

unread,
Feb 9, 2016, 1:51:05 PM2/9/16
to Axon Framework Users
OK. Thank you Rene.
Reply all
Reply to author
Forward
0 new messages