akullpp
akullpp

Reputation: 93

Axon and Spring's repository integration

I've read the Axon documentation and looked at all provided sample projects, especially the AxonBank which I'm referencing here, but one thing is still bothering me and is not explained as far as I see:

It is my understanding that in Axon you perform queries against a read database which represents the materialized view, e.g. a H2 that contains the latest BankAccount JPA entity (here). However if you have a Spring repository, e.g. JpaRepository<BankAccount, Long> (here), you also have the save-method which should only be used for commands. Shouldn't you split the repository into a read-only and write-only repository?

Could someone also point me the documentation what how Axon works with this repository? Because for an unitiated developer it looks like a "normal" JPA repository, i.e. the entity seems mutable and always up to date.

But from a theoretical perspective I expect an immutable entity in a zero state where a projection is created by applying all events, does this happen in the background with Axon?

What would happen if I update the entity with JpaRepository#save but not the aggregate? Will they be out of sync?

It seems that we have two sources of truth in this case, which shouldn't be the case theoretically.

Upvotes: 1

Views: 546

Answers (1)

Lucas Campos
Lucas Campos

Reputation: 1920

let me try to help you!

What you are describing is the CQRS pattern - especially the Query side! The Repository you mentioned is usually used on the @EventHandlers to build your projections, which will store the data the way you need it!

Looking at the AxonBank, it should be clearly visible here.

I don't think there is anything on Axon documentation specific about it but indeed this is a regular JPA Repository. Of course you can use whatever you want as your Query side.

What would happen if I update the entity with JpaRepository#save but not the aggregate? Will they be out of sync?

In this case, your view model will be updated based on anything other than events, which is not what you want. This repository should be updated only based on events, which most of the time, are sent by your Aggregates!

It seems that we have two sources of truth in this case, which shouldn't be the case theoretically.

Regarding your question about the source of truth, your Events should always be the source of truth. In the end, you should not update the repository other than using @EventHandlers.

Upvotes: 1

Related Questions