Checking to see if aggregate exists based on properties #955
Replies: 2 comments
-
It depends on how consistent you need the state to be up to date i.e. are you bulk creating the regions or do they come one at a time with at least a few milliseconds between each. If it isn't time sensitive and some inconsistency isn't an issue, then why not rely on the read side to give you this information? If a duplicate region would cause significant issues and/or is likely based on the speed at which regions are created, then an option is to make the AggregateId deterministic based off of the region code. EventFlow already caters for this using the Guid helper methods from bytes. |
Beta Was this translation helpful? Give feedback.
-
For these cases you should use a solution based on a concept called eventual data consistency validation. In your case, you should use the field that you believe should only exist once. It is something that we have faced with my team several times, the concept is very simple but will depend on each case, we have obtained by adding a table in a relational database with a single field which can not be repeated, basically we have made an index, every time you launch a command of a particular type, one of the things that must resolve before even launching the command is to try to add that field which in your case is Region. If it does not allow you, that means that this aggregate already exists. This is a fairly accepted solution. But as I said it depends on each use case. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to check to see if an aggregate exists with a certain unique property (besides the AggregateId) so I know whether to create a new aggregate or not? Essentially I have a Region aggregate with a RegionCode property that must be unique. As I am importing a large data set which includes Region data I need to check to see if the region code associated with the current importing record already exists. If it does exist I just move on but if it doesn't exist I want to call a command (using a saga most likely) to generate the missing region based on data that was passed in.
Beta Was this translation helpful? Give feedback.
All reactions