EventBus vs. EventTemplate in sagas

69 views
Skip to first unread message

kor...@midwinter.com

unread,
Feb 5, 2016, 12:20:18 AM2/5/16
to Axon Framework Users
The documentation seems to suggest that I should prefer using EventTemplate to publish events. This works perfectly, but adds a small amount of friction to tests: no EventTemplate is injected by AnnotatedSagaTestFixture, so if I use EventTemplate in a saga class, in my test code I end up having to register a mock instance and test for calls to its publishEvent() method.

If I use the EventBus in my saga instead and handle wrapping my own objects in GenericEventMessage, my saga tests can use the fixture's expectPublishedEvents() method to verify that the right events were sent, which feels cleaner.

Is this just a case of EventTemplate being a recent addition and the test fixture code not having caught up yet, or is the fixture's API intentionally hinting to me that I should be directly calling the EventBus? If the latter, what's the disadvantage of EventTemplate in the context of a saga?

Allard Buijze

unread,
Feb 15, 2016, 3:44:03 AM2/15/16
to Axon Framework Users
Hi,

the EventTemplate has been added later. I guess it never made it to the fixtures as a default resource. You can easily register it as a resource: fixture.registerResource(new EventTemplate(fixture.getEventBus());

The advantage of EventTemplate is that it will synchronize publication of an event with the unit of work (if present). That means the event is raised only when the unit of work is committed (but before any transactions are committed). Also, the EventTemplate doesn't require you to use the EventMessage interface, but just your own payload (and additional meta data).

Cheers,

Allard

--
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.
Reply all
Reply to author
Forward
0 new messages