Check if an aggregate already exists in event store

Hello,

I create an aggregate where ID is set as the current year.
I don’t want to be able to call the command for create another instance of this aggregate with the same year as the one stored previously.

Do you think is a good idea to catch the AggregateNotFound exception for ensure I’m not saving an already existing aggregate?
I cannot access to any method on Axon Repository<?> class for checking if aggregate exists.

Thank you very much for your help.

Bye,
Samuele

Sorry for the missing information, I’m using Axon 2.4.2 with MongoEventStore implementation.

Thank you very much.

Hi,

I don’t its a good practice to have a year as identifier for the aggregate. Can you provide more details about your domain model?

I need to add a rule for being sure to not open the employee list if it already exists in the current year.

Then, I create an EmployeeListAggregate with the year as identifier (by now I don’t want to over-engineering, don’t focus too much on it).
I can call the OpenEmployeeListCommand with year as argument, so i can create the aggregate in event store.

I’m not able to find a way to check if the aggregate exists in event store other than catching the AggregateNotFound exception when repository.load(employeeListId) fails.
But… bleah… this approach is the uglier i ever thought about.

Thank you, bye

I would take a quite different approach regarding the domain model design. There would be an Employee aggregate root which would have a list of EmployeeList value objects or even a hash map of <year, EmployeeList> entries for that employee. Now you can load your aggregate from the Employee repository and call openEmployeeList(year) on it, which could generate EmployeeListOpenedEvent domain event in case the employee has the list opened for that year.

Cheers

Sorry, the event name should be EmployeeListAlreadyOpenedEvent.

It’s completely different from the domain I need to develop, but your idea makes me thinking from a different perspective.

The employee is hired by the Company (which I haven’t described yet).
So, the company should handle the pay rolls by year using an hash map. In this way I can check if I already have an entry in the map so i will be able to do what I want.

Thank you very much, it’s really help me.

RUBBER DUCK RULEZZZZZ :slight_smile: https://en.wikipedia.org/wiki/Rubber_duck_debugging

Bye bye,
Samuele

Hi Samuele,

Nedo does have a point saying that the problem may indicate the model isn’t optimal. However, if you find out that the model with the check-if-exists requirement is the best choice, you have 2 options:

  • Use the query model to find out if an aggregate already exists
  • Pretend the aggregate already exists, but on an exception, create it. That may seem ugly, but keep in mind that ugly is just a sense of taste… :wink: *
  • Axon 3 will probably come with the possibility to check for the existence of an aggregate… My taste also says it’s an ugly solution…

Cheers,

Allard

I’m not able to establish which aggregate create the Company instance. The Company exists by itself and is something connected to the application setup.

I come to this point, but I’m not really confident: maybe should be better to implement a domain service, its only responsibility is to create the Company aggregate instance.
In this way my aggregate doesn’t couple with query model repository and I will be able to do every check before create it.

What is your opinion? Thank you.

Sounds like a good plan.
However, do realize that you’re going to execute a query to check if an aggregate already exists. That check will fails once a year. Do you really think it’s worth it?

No, it’s not :slight_smile: I understand the point and I’m trying to model again my aggregate.

Nedo & Allard, thank you very much.

Did the checkIfAggregateExists() ever make it into Axon 4? I need to determine if it already exists, or whether to create it.

Hi Jonathan,

This is still an outstanding issue we have on our backlog, sadly.
It will be introduced into Axon 4 though, of that I am sure.
We are about to release 4.1 of our software (Framework, Server,), so it wont make it to the 4.1 release.
However requests like this will put it higher up the chain to be added into the next release.

Hope to have given you the needed information, Jonathan, to deduce whether you will create this function yourself or if you would wait for a future release to include it.

Cheers,
Steven

Hi Jonathan,

just a small notice that there is a method on the EventStore interface that you can use for this, if your aggregate is event sourced:
Optional lastSequenceNumberFor(String aggregateIdentifier);

If the aggregate exists, the optional will contain the sequence number of the most recent event. Otherwise, it’s an empty optional.

Cheers,

Allard and Steve,

Thanks for the replies. For now I’ll check out lastSequenceNumberFor().

Cheers,

Jonathan